Skip to content

Commit

Permalink
fix(ajv): remove field empty keyword to make it native
Browse files Browse the repository at this point in the history
  • Loading branch information
jsun969 committed Jun 1, 2022
1 parent b102ac1 commit 3cb2b88
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 33 deletions.
12 changes: 10 additions & 2 deletions ajv/src/__tests__/Form-native-validation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@ type FormData = { username: string; password: string };
const schema: JSONSchemaType<FormData> = {
type: 'object',
properties: {
username: { type: 'string' },
password: { type: 'string' },
username: {
type: 'string',
minLength: 1,
errorMessage: { minLength: USERNAME_REQUIRED_MESSAGE },
},
password: {
type: 'string',
minLength: 1,
errorMessage: { minLength: PASSWORD_REQUIRED_MESSAGE },
},
},
required: ['username', 'password'],
additionalProperties: false,
Expand Down
12 changes: 10 additions & 2 deletions ajv/src/__tests__/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ type FormData = { username: string; password: string };
const schema: JSONSchemaType<FormData> = {
type: 'object',
properties: {
username: { type: 'string' },
password: { type: 'string' },
username: {
type: 'string',
minLength: 1,
errorMessage: { minLength: 'username field is required' },
},
password: {
type: 'string',
minLength: 1,
errorMessage: { minLength: 'password field is required' },
},
},
required: ['username', 'password'],
additionalProperties: false,
Expand Down
30 changes: 1 addition & 29 deletions ajv/src/ajv.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { toNestError, validateFieldsNatively } from '@hookform/resolvers';
import Ajv, { DefinedError, KeywordCxt, _ as genCode } from 'ajv';
import Ajv, { DefinedError } from 'ajv';
import ajvErrors from 'ajv-errors';
import { appendErrors, FieldError } from 'react-hook-form';
import { Resolver } from './types';
Expand Down Expand Up @@ -56,34 +56,6 @@ export const ajvResolver: Resolver =

ajvErrors(ajv);

// If the field is not require in form, the value is empty (value: "")
// But Ajv `required` cannot check empty, it can only check undefined
// This keyword is for checking empty, but not available for nest fields
// https://stackoverflow.com/a/72374873/15246747
ajv.addKeyword({
keyword: 'fieldRequired',
schemaType: 'boolean',
type: 'string',
code(cxt: KeywordCxt) {
const { data, schema } = cxt;
if (schema) {
cxt.fail(genCode`${data}.trim() === ''`);
}
},
});

if (schema.required.length !== 0) {
(schema.required as string[]).forEach((field) => {
if (schema.properties[field].type === 'string') {
schema.properties[field].fieldRequired = true;
schema.properties[field].errorMessage = Object.assign(
schema.properties[field].errorMessage || {},
{ fieldRequired: `${field} field is required` },
);
}
});
}

const validate = ajv.compile(
Object.assign({ $async: resolverOptions?.mode === 'async' }, schema),
);
Expand Down

0 comments on commit 3cb2b88

Please sign in to comment.