react-hook-form / resolvers

📋 Validation resolvers: Yup, Zod, Superstruct, Joi, Vest, Class Validator, io-ts, Nope, computed-types, typanion, Ajv, TypeBox, ArkType, Valibot, effect-ts and VineJS
https://react-hook-form.com/
MIT License
1.74k stars 159 forks source link

typescript problem in AJV resolver #658

Open MshHooman opened 8 months ago

MshHooman commented 8 months ago

Describe the bug Hi, I'm using ajvResolver in my project but face a typescript error that is wrong.

here is my packages:

"@hookform/resolvers": "3.3.4",
"react-hook-form": "^7.0.0",
"typescript": "~5.2.2",
"vite": "^5.0.0",
"@types/react": "^18.2.46",
"@types/react-dom": "^18.2.18",

and this is an example of code that I used:

  const schema = {
    type: 'object',
    properties: {
      password1: {
        type: 'string',
        minLength: 3,
        errorMessage: { minLength: 'password1 field is required' },
      },
      password2: {
        type: 'string',
        minLength: 3,
        errorMessage: { minLength: 'password2 field is required' },
      },
    },
    required: ['password1', 'password2'],
    additionalProperties: false,
  };

  const validation = ajvResolver(schema);

but I face this error from ajvResolver:

Property 'oneOf' is missing in type '{ type: string; properties: { password1: { type: string; minLength: number; errorMessage: { minLength: string; }; }; password2: { type: string; minLength: number; errorMessage: { minLength: string; }; }; }; required: string[]; additionalProperties: boolean; }' but required in type '{ oneOf: readonly UncheckedJSONSchemaType<FormType, false>[]; }'.ts(2345)

so when I add oneOf property the type problem will be solved but as I understand and test in AJV directly ,this property is not required !

m4dhead commented 8 months ago

also getting this issue

Rozamo commented 3 months ago

Can you try assigning the type JSONSchemaType to your schema as mentioned here. It solved the issue for me.

jorisre commented 3 months ago

Can you please provide a minimal reproducible example (e.g. a Codesandbox)?