feat: added the ability to grab the schema for the field definition in the validator

Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
blam
2023-02-21 12:31:25 +01:00
parent 2b65a84775
commit be99a4ddd6
3 changed files with 96 additions and 4 deletions
@@ -56,6 +56,79 @@ describe('createAsyncValidators', () => {
expect(validators.AddressField).toHaveBeenCalled();
});
it('should call the validator function with the correct schema in the context', async () => {
const schema: JsonObject = {
type: 'object',
properties: {
name: {
type: 'string',
'ui:options': {
bob: true,
},
pattern: 'lols',
'ui:field': 'NameField',
},
address: {
type: 'object',
'ui:field': 'AddressField',
properties: {
street: {
type: 'string',
},
postcode: {
type: 'string',
},
},
},
},
};
const validators = { NameField: jest.fn(), AddressField: jest.fn() };
const validate = createAsyncValidators(schema, validators, {
apiHolder: { get: jest.fn() },
});
await validate({
name: 'asd',
address: { street: 'street', postcode: 'postcode' },
});
expect(validators.NameField).toHaveBeenCalledWith(
'asd',
expect.anything(),
expect.objectContaining({
schema: {
type: 'string',
'ui:options': {
bob: true,
},
pattern: 'lols',
'ui:field': 'NameField',
},
}),
);
expect(validators.AddressField).toHaveBeenCalledWith(
{ street: 'street', postcode: 'postcode' },
expect.anything(),
expect.objectContaining({
schema: {
type: 'object',
'ui:field': 'AddressField',
properties: {
street: {
type: 'string',
},
postcode: {
type: 'string',
},
},
},
}),
);
});
it('should return the correct errors to the frontend', async () => {
const schema: JsonObject = {
type: 'object',
@@ -48,12 +48,17 @@ export const createAsyncValidators = (
validatorName: string,
key: string,
value: JsonValue | undefined,
schema: JsonObject,
) => {
const validator = validators[validatorName];
if (validator) {
const fieldValidation = createFieldValidation();
try {
await validator(value, fieldValidation, { ...context, formData });
await validator(value, fieldValidation, {
...context,
formData,
schema,
});
} catch (ex) {
fieldValidation.addError(ex.message);
}
@@ -67,7 +72,12 @@ export const createAsyncValidators = (
if (definitionInSchema && 'ui:field' in definitionInSchema) {
if ('ui:field' in definitionInSchema) {
await validateForm(definitionInSchema['ui:field'], key, value);
await validateForm(
definitionInSchema['ui:field'],
key,
value,
definitionInSchema,
);
}
} else if (
definitionInSchema &&
@@ -75,7 +85,12 @@ export const createAsyncValidators = (
'ui:field' in definitionInSchema.items
) {
if ('ui:field' in definitionInSchema.items) {
await validateForm(definitionInSchema.items['ui:field'], key, value);
await validateForm(
definitionInSchema.items['ui:field'],
key,
value,
definitionInSchema.items,
);
}
} else if (isObject(value)) {
formValidation[key] = await validate(formData, path, value);
@@ -46,7 +46,11 @@ export interface NextFieldExtensionComponentProps<
export type NextCustomFieldValidator<TFieldReturnValue> = (
data: TFieldReturnValue,
field: FieldValidationV5,
context: { apiHolder: ApiHolder; formData: JsonObject },
context: {
apiHolder: ApiHolder;
formData: JsonObject;
schema?: JsonObject;
},
) => void | Promise<void>;
/**