Skip to content

Commit

Permalink
fix: validation consistency within admin
Browse files Browse the repository at this point in the history
  • Loading branch information
jmikrut committed Mar 24, 2021
1 parent 5f9a5c8 commit 50b9937
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/admin/components/forms/Form/buildStateFromSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const buildStateFromSchema = async (fieldSchema: FieldSchema[], fullData: Data =
value,
initialValue: value,
valid: true,
validate: field.validate,
};

validationPromises.push(buildValidationPromise(fieldState, field));
Expand Down
11 changes: 2 additions & 9 deletions src/admin/components/forms/field-types/Text/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback } from 'react';
import React from 'react';
import useFieldType from '../../useFieldType';
import withCondition from '../../withCondition';
import Label from '../../Label';
Expand All @@ -21,20 +21,13 @@ const Text: React.FC<Props> = (props) => {
style,
width,
} = {},
minLength,
maxLength,
} = props;

const path = pathFromProps || name;

const memoizedValidate = useCallback((value) => {
const validationResult = validate(value, { minLength, maxLength, required });
return validationResult;
}, [validate, maxLength, minLength, required]);

const fieldType = useFieldType<string>({
path,
validate: memoizedValidate,
validate,
enableDebouncedValue: true,
});

Expand Down
3 changes: 1 addition & 2 deletions src/fields/config/sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ const sanitizeFields = (fields, validRelationships) => {

if (typeof field.validate === 'undefined') {
const defaultValidate = validations[field.type];
const noValidate = () => true;
field.validate = defaultValidate || noValidate;
field.validate = (val) => defaultValidate(val, field);
}

if (!field.hooks) field.hooks = {};
Expand Down

0 comments on commit 50b9937

Please sign in to comment.