|
1 | 1 | import { ErrorObject } from 'ajv'; |
2 | 2 | import get from 'lodash/get'; |
3 | 3 | import { |
| 4 | + ANY_OF_KEY, |
4 | 5 | createErrorHandler, |
5 | 6 | CustomValidator, |
6 | 7 | ErrorTransformer, |
7 | 8 | FormContextType, |
8 | 9 | getDefaultFormState, |
9 | 10 | getUiOptions, |
| 11 | + ONE_OF_KEY, |
10 | 12 | PROPERTIES_KEY, |
11 | 13 | RJSFSchema, |
12 | 14 | RJSFValidationError, |
@@ -34,7 +36,7 @@ export function transformRJSFValidationErrors< |
34 | 36 | S extends StrictRJSFSchema = RJSFSchema, |
35 | 37 | F extends FormContextType = any, |
36 | 38 | >(errors: ErrorObject[] = [], uiSchema?: UiSchema<T, S, F>): RJSFValidationError[] { |
37 | | - return errors.map((e: ErrorObject) => { |
| 39 | + const errorList = errors.map((e: ErrorObject) => { |
38 | 40 | const { instancePath, keyword, params, schemaPath, parentSchema, ...rest } = e; |
39 | 41 | let { message = '' } = rest; |
40 | 42 | let property = instancePath.replace(/\//g, '.'); |
@@ -105,6 +107,28 @@ export function transformRJSFValidationErrors< |
105 | 107 | title: uiTitle, |
106 | 108 | }; |
107 | 109 | }); |
| 110 | + // Filter out duplicates around anyOf/oneOf messages |
| 111 | + return errorList.reduce((acc: RJSFValidationError[], err: RJSFValidationError) => { |
| 112 | + const { message, schemaPath } = err; |
| 113 | + const anyOfIndex = schemaPath?.indexOf(`/${ANY_OF_KEY}/`); |
| 114 | + const oneOfIndex = schemaPath?.indexOf(`/${ONE_OF_KEY}/`); |
| 115 | + let schemaPrefix: string | undefined; |
| 116 | + // Look specifically for `/anyOr/` or `/oneOf/` within the schemaPath information |
| 117 | + if (anyOfIndex && anyOfIndex >= 0) { |
| 118 | + schemaPrefix = schemaPath?.substring(0, anyOfIndex); |
| 119 | + } else if (oneOfIndex && oneOfIndex >= 0) { |
| 120 | + schemaPrefix = schemaPath?.substring(0, oneOfIndex); |
| 121 | + } |
| 122 | + // If there is a schemaPrefix, then search for a duplicate message with the same prefix, otherwise undefined |
| 123 | + const dup = schemaPrefix |
| 124 | + ? acc.find((e: RJSFValidationError) => e.message === message && e.schemaPath?.startsWith(schemaPrefix)) |
| 125 | + : undefined; |
| 126 | + if (!dup) { |
| 127 | + // Only push an error that is not a duplicate |
| 128 | + acc.push(err); |
| 129 | + } |
| 130 | + return acc; |
| 131 | + }, [] as RJSFValidationError[]); |
108 | 132 | } |
109 | 133 |
|
110 | 134 | /** This function processes the `formData` with an optional user contributed `customValidate` function, which receives |
|
0 commit comments