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: ajv resolver to work with unlimited layers of nesting #412

Merged
merged 1 commit into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions ajv/src/__tests__/__fixtures__/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Field, InternalFieldName } from 'react-hook-form';
interface Data {
username: string;
password: string;
deepObject: { data: string };
deepObject: { data: string; twoLayersDeep: { name: string } };
}

export const schema: JSONSchemaType<Data> = {
Expand All @@ -26,8 +26,14 @@ export const schema: JSONSchemaType<Data> = {
nullable: true,
properties: {
data: { type: 'string' },
twoLayersDeep: {
type: 'object',
properties: { name: { type: 'string' } },
additionalProperties: false,
required: ['name'],
},
},
required: ['data'],
required: ['data', 'twoLayersDeep'],
},
},
required: ['username', 'password', 'deepObject'],
Expand All @@ -38,6 +44,9 @@ export const validData: Data = {
username: 'jsun969',
password: 'validPassword',
deepObject: {
twoLayersDeep: {
name: 'deeper',
},
data: 'data',
},
};
Expand All @@ -47,6 +56,7 @@ export const invalidData = {
password: 'invalid-password',
deepObject: {
data: 233,
twoLayersDeep: { name: 123 }
},
};

Expand Down
34 changes: 34 additions & 0 deletions ajv/src/__tests__/__snapshots__/ajv.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ Object {
"type": "must be string",
},
},
"twoLayersDeep": Object {
"name": Object {
"message": "must be string",
"ref": undefined,
"type": "type",
"types": Object {
"type": "must be string",
},
},
},
},
"password": Object {
"message": "One uppercase character",
Expand Down Expand Up @@ -104,6 +114,16 @@ Object {
"type": "must be string",
},
},
"twoLayersDeep": Object {
"name": Object {
"message": "must be string",
"ref": undefined,
"type": "type",
"types": Object {
"type": "must be string",
},
},
},
},
"password": Object {
"message": "One uppercase character",
Expand Down Expand Up @@ -139,6 +159,13 @@ Object {
"ref": undefined,
"type": "type",
},
"twoLayersDeep": Object {
"name": Object {
"message": "must be string",
"ref": undefined,
"type": "type",
},
},
},
"password": Object {
"message": "One uppercase character",
Expand Down Expand Up @@ -168,6 +195,13 @@ Object {
"ref": undefined,
"type": "type",
},
"twoLayersDeep": Object {
"name": Object {
"message": "must be string",
"ref": undefined,
"type": "type",
},
},
},
"password": Object {
"message": "One uppercase character",
Expand Down
2 changes: 1 addition & 1 deletion ajv/src/ajv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const parseErrorSchema = (

return ajvErrors.reduce<Record<string, FieldError>>((previous, error) => {
// `/deepObject/data` -> `deepObject.data`
const path = error.instancePath.substring(1).replace('/', '.');
const path = error.instancePath.substring(1).replace(/\//g, '.');

if (!previous[path]) {
previous[path] = {
Expand Down