Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: typescript schema to allow nullable fields #4579

Merged
merged 2 commits into from
Mar 27, 2023

Conversation

johannesjo
Copy link
Contributor

Otherwise any schema with a type like:

   myfield: {
      type: ["string", "null"],
    },

will produce an error.

This PR contains:

Describe the problem you have without this PR

Todos

  • Tests
  • Documentation
  • Typings
  • Changelog

Otherwise any schema with a type like:
```
   myfield: {
      type: ["string", "null"],
    },
```
will produce an error.
@pubkey
Copy link
Owner

pubkey commented Mar 21, 2023

I am sorry but this is wrong at many points.(string & {}) is allowed which is not reflected in your array. Also there can be more then 2 types which is not reflected by that change.

Also there is no test. Below the codes states type?: JsonSchemaTypes | JsonSchemaTypes[]; so what your are trying to do should work already.

Notice that it is recommended to use undefined for optional fields, not a mixed type with null. By default all fields in json-schema are optional.

@johannesjo
Copy link
Contributor Author

johannesjo commented Mar 24, 2023

Sorry for the wrong code.

Here is the minimal case to reproduce the underlying problem:

export const TEST_SCHEMA_LITERAL = {
  title: "test",
  version: 0,
  primaryKey: "id",
  type: "object",
  properties: {
    id: {
      type: "string",
    },
    completion_date: {
      type: ["string", "null"],
    },
  },
  required: [
    "id",
  ],
  indexes: [],
} as const;

const SCHEMA_TYPED = toTypedRxJsonSchema(TEST_SCHEMA_LITERAL);
export type RxSubtaskDocumentType = ExtractDocumentTypeFromTypedRxJsonSchema<
  typeof SCHEMA_TYPED
>;

export const TEST_SCHEMA: RxJsonSchema<RxSubtaskDocumentType> = TEST_SCHEMA_LITERAL;
//  => Throws TS Error

Which throws the error  The type 'readonly ["string", "null"]' is 'readonly' and cannot be assigned to the mutable type 'JsonSchemaTypes[]':

TS2322: Type '{ readonly title: "test"; readonly version: 0; readonly primaryKey: "id"; readonly type: "object"; readonly properties: { readonly id: { readonly type: "string"; }; readonly completion_date: { readonly type: readonly ["string", "null"]; }; }; readonly required: readonly [...]; readonly indexes: readonly []; }' is not assignable to type 'RxJsonSchema<{ id: string; completion_date?: string | null | undefined; }>'.   The types of 'properties.completion_date.type' are incompatible between these types.     Type 'readonly ["string", "null"]' is not assignable to type 'JsonSchemaTypes | JsonSchemaTypes[] | undefined'.       The type 'readonly ["string", "null"]' is 'readonly' and cannot be assigned to the mutable type 'JsonSchemaTypes[]'

A possible workaround is casting the type:

["string", "null"] as JsonSchemaTypes[]
// or
["string", "null"] as string[]

@pubkey pubkey merged commit 94d8238 into pubkey:master Mar 27, 2023
@pubkey
Copy link
Owner

pubkey commented Mar 27, 2023

Looks good now. Merged!

pubkey added a commit that referenced this pull request Mar 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants