Skip to content

Commit a21688e

Browse files
committed
feat: custom validations enabled
1 parent f37295a commit a21688e

File tree

4 files changed

+44
-10
lines changed

4 files changed

+44
-10
lines changed

src/Form.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const UploadContext = React.createContext('upload');
2424
class Form extends React.Component {
2525
state = {
2626
data: this.props.formData,
27-
validation: getValidationResult(this.props.schema, this.props.uiSchema, this.props.formData),
27+
validation: getValidationResult(this.props.schema, this.props.uiSchema, this.props.formData, this.props.validations),
2828
id: this.props.prefixId || generate(),
2929
}
3030

@@ -34,7 +34,7 @@ class Form extends React.Component {
3434
validation = {};
3535
}
3636
else {
37-
validation = getValidationResult(this.props.schema, this.props.uiSchema, nextProps.formData);
37+
validation = getValidationResult(nextProps.schema, nextProps.uiSchema, nextProps.formData, this.props.validations);
3838
}
3939
this.setState({
4040
validation,
@@ -46,7 +46,7 @@ class Form extends React.Component {
4646
const data = updateFormData(this.state.data, field, value);
4747
this.setState({
4848
data,
49-
validation: getValidationResult(this.props.schema, this.props.uiSchema, data),
49+
validation: getValidationResult(this.props.schema, this.props.uiSchema, data, this.props.validations),
5050
}, this.notifyChange);
5151
}
5252

src/demo/body/Example.jsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ const FormComponent = React.memo(
6161
<CustomRating onChange={onChange} formData={formData} {...rest} />
6262
),
6363
}}
64+
validations={{
65+
confirmPassword: (givenSchema, givenUISchema, value) => value !== formData.pass1 && ({
66+
message: givenUISchema['ui:validations'].confirmPassword.message,
67+
inline: true,
68+
}),
69+
}}
6470
submitOnEnter
6571
activityIndicatorEnabled
6672
/>

src/demo/examples/validation/ui-schema.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
"ui:widget": "password"
44
},
55
"pass2": {
6-
"ui:widget": "password"
6+
"ui:widget": "password",
7+
"ui:validations": {
8+
"confirmPassword": {
9+
"message": "Both passwords should be the same (2)",
10+
"inline": false
11+
}
12+
}
713
}
814
}
Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
// import update from 'immutability-helper';
22
import forOwn from 'lodash/forOwn';
33
import mapValues from 'lodash/mapValues';
4+
import flatten from 'lodash/flatten';
5+
import difference from 'lodash/difference';
46
import rules from './rules';
57

6-
const validationResult = (schema, uiSchema, value) => {
8+
const isSubset = (source, target) => !difference(flatten(source), flatten(target)).length;
9+
const validationResult = (schema, uiSchema, value, customValidations) => {
710
const rv = [];
811
forOwn(rules, (rule, ruleId) => {
912
const result = rule(schema, uiSchema, value);
@@ -14,20 +17,39 @@ const validationResult = (schema, uiSchema, value) => {
1417
});
1518
}
1619
});
20+
if (
21+
uiSchema
22+
&& uiSchema['ui:validations']
23+
&& customValidations
24+
&& isSubset(
25+
Object.keys(uiSchema['ui:validations']),
26+
Object.keys(customValidations),
27+
)
28+
) {
29+
forOwn(customValidations, (rule, ruleId) => {
30+
const result = rule(schema, uiSchema, value);
31+
if (result) {
32+
rv.push({
33+
rule: ruleId,
34+
...result,
35+
});
36+
}
37+
});
38+
}
1739
return rv;
1840
};
1941

20-
const getFieldSpec = (schema, uiSchema, value) => {
42+
const getFieldSpec = (schema, uiSchema, value, customValidations) => {
2143
if (value === null) {
2244
return [];
2345
}
2446
if (typeof value === 'number' || typeof value === 'string') {
25-
return validationResult(schema, uiSchema, value);
47+
return validationResult(schema, uiSchema, value, customValidations);
2648
}
27-
return mapValues(schema.properties, (s, p) => getFieldSpec(s, uiSchema[p], value[p]));
49+
return mapValues(schema.properties, (s, p) => getFieldSpec(s, uiSchema[p], value[p], customValidations));
2850
};
2951

30-
export default (schema, uiSchema, data) => {
31-
const spec = getFieldSpec(schema, uiSchema, data);
52+
export default (schema, uiSchema, data, customValidations) => {
53+
const spec = getFieldSpec(schema, uiSchema, data, customValidations);
3254
return { ...spec };
3355
};

0 commit comments

Comments
 (0)