Skip to content

Commit 32623e5

Browse files
- Fixed bug in custom validation, including adding a test to detect it
1 parent e890c42 commit 32623e5

File tree

2 files changed

+60
-30
lines changed

2 files changed

+60
-30
lines changed

packages/validator-ajv6/src/validator.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ export default class AJV6Validator<T = any> implements ValidatorType<T> {
210210
): ValidationData<T> {
211211
// Include form data with undefined values, which is required for validation.
212212
const rootSchema = schema;
213-
const newFormData = getDefaultFormState(this, schema, formData, rootSchema, true);
213+
const newFormData = getDefaultFormState(this, schema, formData, rootSchema, true) as T;
214214

215215
let validationError: Error | null = null;
216216
try {
@@ -257,7 +257,7 @@ export default class AJV6Validator<T = any> implements ValidatorType<T> {
257257
return { errors, errorSchema };
258258
}
259259

260-
const errorHandler = customValidate(formData, this.createErrorHandler(formData));
260+
const errorHandler = customValidate(newFormData, this.createErrorHandler(newFormData));
261261
const userErrorSchema = this.unwrapErrorHandler(errorHandler);
262262
const newErrorSchema: ErrorSchema<T> = mergeObjects(errorSchema, userErrorSchema, true) as ErrorSchema<T>;
263263
// XXX: The errors list produced is not fully compliant with the format

packages/validator-ajv6/test/validator.test.ts

+58-28
Original file line numberDiff line numberDiff line change
@@ -247,36 +247,66 @@ describe('AJV6Validator', () => {
247247
describe('Custom validate function', () => {
248248
let errors: RJSFValidationError[];
249249
let errorSchema: ErrorSchema;
250+
describe('formData is provided', () => {
251+
beforeAll(() => {
252+
const schema: RJSFSchema = {
253+
type: 'object',
254+
required: ['pass1', 'pass2'],
255+
properties: {
256+
pass1: { type: 'string' },
257+
pass2: { type: 'string' },
258+
foo: { type: 'array', items: { type: 'string' } } // Adding an array for test coverage
259+
},
260+
};
250261

251-
beforeAll(() => {
252-
const schema: RJSFSchema = {
253-
type: 'object',
254-
required: ['pass1', 'pass2'],
255-
properties: {
256-
pass1: { type: 'string' },
257-
pass2: { type: 'string' },
258-
foo: { type: 'array', items: { type: 'string' } } // Adding an array for test coverage
259-
},
260-
};
261-
262-
const validate = (formData: any, errors: FormValidation<any>) => {
263-
if (formData.pass1 !== formData.pass2) {
264-
errors.pass2!.addError('passwords don`t match.');
265-
}
266-
return errors;
267-
};
268-
const formData = { pass1: 'a', pass2: 'b', foo: ['a'] };
269-
const result = validator.validateFormData(formData, schema, validate);
270-
errors = result.errors;
271-
errorSchema = result.errorSchema;
272-
});
273-
it('should return an error list', () => {
274-
expect(errors).toHaveLength(1);
275-
expect(errors[0].stack).toEqual('pass2: passwords don`t match.');
262+
const validate = (formData: any, errors: FormValidation<any>) => {
263+
if (formData.pass1 !== formData.pass2) {
264+
errors.pass2!.addError('passwords don`t match.');
265+
}
266+
return errors;
267+
};
268+
const formData = { pass1: 'a', pass2: 'b', foo: ['a'] };
269+
const result = validator.validateFormData(formData, schema, validate);
270+
errors = result.errors;
271+
errorSchema = result.errorSchema;
272+
});
273+
it('should return an error list', () => {
274+
expect(errors).toHaveLength(1);
275+
expect(errors[0].stack).toEqual('pass2: passwords don`t match.');
276+
});
277+
it('should return an errorSchema', () => {
278+
expect(errorSchema.pass2!.__errors).toHaveLength(1);
279+
expect(errorSchema.pass2!.__errors![0]).toEqual('passwords don`t match.');
280+
});
276281
});
277-
it('should return an errorSchema', () => {
278-
expect(errorSchema.pass2!.__errors).toHaveLength(1);
279-
expect(errorSchema.pass2!.__errors![0]).toEqual('passwords don`t match.');
282+
describe('formData is missing data', () => {
283+
beforeAll(() => {
284+
const schema: RJSFSchema = {
285+
type: 'object',
286+
properties: {
287+
pass1: { type: 'string' },
288+
pass2: { type: 'string' },
289+
},
290+
};
291+
const validate = (formData: any, errors: FormValidation<any>) => {
292+
if (formData.pass1 !== formData.pass2) {
293+
errors.pass2!.addError('passwords don`t match.');
294+
}
295+
return errors;
296+
};
297+
const formData = { pass1: 'a' };
298+
const result = validator.validateFormData(formData, schema, validate);
299+
errors = result.errors;
300+
errorSchema = result.errorSchema;
301+
});
302+
it('should return an error list', () => {
303+
expect(errors).toHaveLength(1);
304+
expect(errors[0].stack).toEqual('pass2: passwords don`t match.');
305+
});
306+
it('should return an errorSchema', () => {
307+
expect(errorSchema.pass2!.__errors).toHaveLength(1);
308+
expect(errorSchema.pass2!.__errors![0]).toEqual('passwords don`t match.');
309+
});
280310
});
281311
});
282312
describe('Data-Url validation', () => {

0 commit comments

Comments
 (0)