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

Unknown attributes AJV and typescript #2082

Closed
vongohren opened this issue Sep 5, 2022 · 4 comments
Closed

Unknown attributes AJV and typescript #2082

vongohren opened this issue Sep 5, 2022 · 4 comments

Comments

@vongohren
Copy link

vongohren commented Sep 5, 2022

What version of Ajv are you using? Does the issue happen if you use the latest version?
8.11

Your typescript code

 type Typez = string | string[];
  interface Test {
    typez: Typez,
    [otherAttributes: string]: any;
  }

  const schema2: JSONSchemaType<Test> = {
    type: "object",
    properties: {
      typez: {
        anyOf: [{ type: "string"}, {type: "array", items: {type:"string"}}],
      }
    },
    additionalProperties: true,
    required:["typez"]
  }
  console.log(schema2)
}

Typescript compiler error messages

Type '{ type: "object"; properties: { typez: { anyOf: ({ type: "string"; } | { type: "array"; items: { type: "string"; }; })[]; }; }; additionalProperties: true; required: string[]; }' is not assignable to type 'UncheckedJSONSchemaType<Test, false>'.
  Types of property 'properties' are incompatible.
    Type '{ typez: { anyOf: ({ type: "string"; } | { type: "array"; items: { type: "string"; }; })[]; }; }' is not assignable to type 'UncheckedPropertiesSchema<Test>'.
      Property 'typez' is incompatible with index signature.
        Type '{ anyOf: ({ type: "string"; } | { type: "array"; items: { type: "string"; }; })[]; }' is not assignable to type '{ $ref: string; } | (UncheckedJSONSchemaType<any, false> & { nullable: true; const?: null | undefined; enum?: readonly any[] | undefined; default?: any; })'.
          Type '{ anyOf: ({ type: "string"; } | { type: "array"; items: { type: "string"; }; })[]; }' is not assignable to type '{ type: "null"; nullable: true; } & { allOf?: readonly UncheckedPartialSchema<any>[] | undefined; anyOf?: readonly UncheckedPartialSchema<any>[] | undefined; ... 4 more ...; not?: UncheckedPartialSchema<...> | undefined; } & { ...; } & { ...; }'.
            Type '{ anyOf: ({ type: "string"; } | { type: "array"; items: { type: "string"; }; })[]; }' is missing the following properties from type '{ type: "null"; nullable: true; }': type, nullablets(2322)

Describe the change that should be made to address the issue?
How shall I be able to represent the current type with ajv?
I did not find a solution in this issue: #1521

Are you going to resolve the issue?
I will resolve when I get some feedback

@vongohren vongohren changed the title Unknown attrbiutes AJV and typescript Unknown attributes AJV and typescript Sep 5, 2022
@epoberezkin
Copy link
Member

JSONSchemaType doesn't support catch-all index syntax. Even with typescript, I think [otherAttributes: string]: any would allow any property to have any value, including typez, so it's probably not your intention... Although I might be wrong here.

@vongohren
Copy link
Author

vongohren commented Sep 5, 2022

@epoberezkin typescript does what I intend to do :) They give me warning that something is required, but lots of other things can be in the same object.

image

So im happy with the TS implementation, but would love to be able to validate this as well somehow?
I did think additionalProperties might work?

And it is enforcing typez to be the right value:
image

@epoberezkin
Copy link
Member

What I meant is that if you add types: 5 in the object typescript would probably be happy about it.

In general, even though JavaScript and TypeScript allow mixing records and dictionaries in the same object, it's usually better to keep them separate, limiting records to known properties only (without any unknown additional properties).

If the intention is to allow additional properties in JSON, it's ok, but you don't have to declare them in the type - you can just add additionalProperties: true in the schema (for forward compatibility), but you only need them in the type if your code is adding such properties, and your code shouldn't really be doing that.

@vongohren
Copy link
Author

@epoberezkin thanks for quick reply! But I had updated my message with that specific worry you had. It catches that as well. But thanks for the feedback.

I will have to think about it. Will close tomorrow!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants