Skip to content

Commit 21d12f2

Browse files
committed
Add support for warnings in standaone validation
1 parent 3f1146d commit 21d12f2

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

packages/react-form-renderer/src/tests/validation/validation.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,32 @@ describe('validation', () => {
122122
['some-value', options.values, {}]
123123
]);
124124
});
125+
126+
it('handle warnings', async () => {
127+
schema = {
128+
fields: [
129+
{ name: 'warning', component: 'checkbox', validate: [{ type: 'required', warning: true }], useWarnings: true },
130+
{ name: 'error', component: 'select', validate: [{ type: 'required' }] }
131+
]
132+
};
133+
options = {};
134+
135+
const results = await validation(schema, options);
136+
137+
expect(results).toEqual({ error: 'Required', warning: { error: 'Required', type: 'warning' } });
138+
});
139+
140+
it('omit warnings', async () => {
141+
schema = {
142+
fields: [
143+
{ name: 'warning', component: 'checkbox', validate: [{ type: 'required', warning: true }], useWarnings: true },
144+
{ name: 'error', component: 'select', validate: [{ type: 'required' }] }
145+
]
146+
};
147+
options = { omitWarnings: true };
148+
149+
const results = await validation(schema, options);
150+
151+
expect(results).toEqual({ error: 'Required' });
152+
});
125153
});

packages/react-form-renderer/src/validation/validation.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
import { Schema, AnyObject } from "../common-types";
1+
import { Schema, AnyObject, ComponentMapper, SchemaValidatorMapper } from "../common-types";
2+
import { ActionMapper } from "../form-renderer";
3+
import { ValidatorMapper } from "../validator-mapper";
24

35
export interface ValidationOptions {
46
values: AnyObject;
7+
componentMapper?: ComponentMapper;
8+
validatorMapper?: ValidatorMapper;
9+
actionMapper?: ActionMapper;
10+
schemaValidatorMapper?: SchemaValidatorMapper;
11+
omitWarnings?: boolean;
512
}
613

714
declare function validation(schema: Schema, options: ValidationOptions): AnyObject;

packages/react-form-renderer/src/validation/validation.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const validation = async (schema, options) => {
3232
throw new Error(`options argument has to be type of object, provided: ${typeof options}`);
3333
}
3434

35-
const { values, componentMapper, validatorMapper, actionMapper, schemaValidatorMapper } = options;
35+
const { values, componentMapper, validatorMapper, actionMapper, schemaValidatorMapper, omitWarnings } = options;
3636

3737
const validatorMapperMerged = { ...defaultValidatorMapper, ...validatorMapper };
3838

@@ -59,8 +59,14 @@ const validation = async (schema, options) => {
5959
let index = 0;
6060

6161
while (!error && index + 1 <= validates[name].length) {
62-
const validateFn = composeValidators(getValidate(validates[name][index], schema.dataType, validatorMapperMerged));
63-
error = await validateFn(get(values, name), values, {});
62+
const validateFn = composeValidators(getValidate(validates[name][index], undefined, validatorMapperMerged));
63+
64+
const fieldError = await validateFn(get(values, name), values, {});
65+
66+
if (fieldError?.type !== 'warning' || (fieldError?.type === 'warning' && !omitWarnings)) {
67+
error = fieldError;
68+
}
69+
6470
index = index + 1;
6571
}
6672

0 commit comments

Comments
 (0)