(
instancePath: string,
schema: JsonSchema,
matchPath: (path: string) => boolean
)
| 74 | |
| 75 | export const errorsAt = |
| 76 | ( |
| 77 | instancePath: string, |
| 78 | schema: JsonSchema, |
| 79 | matchPath: (path: string) => boolean |
| 80 | ) => |
| 81 | (errors: ErrorObject[]): ErrorObject[] => { |
| 82 | // Get data paths of oneOf and anyOf errors to later determine whether an error occurred inside a subschema of oneOf or anyOf. |
| 83 | const combinatorPaths = filter( |
| 84 | errors, |
| 85 | (error) => error.keyword === 'oneOf' || error.keyword === 'anyOf' |
| 86 | ).map((error) => getControlPath(error)); |
| 87 | |
| 88 | return filter(errors, (error) => { |
| 89 | // Filter errors that match any keyword that we don't want to show in the UI |
| 90 | // but keep the errors for oneOf enums |
| 91 | if ( |
| 92 | filteredErrorKeywords.indexOf(error.keyword) !== -1 && |
| 93 | !isOneOfEnumSchema(error.parentSchema) |
| 94 | ) { |
| 95 | return false; |
| 96 | } |
| 97 | const controlPath = getControlPath(error); |
| 98 | let result = matchPath(controlPath); |
| 99 | // In anyOf and oneOf blocks with "primitive" (i.e. string, number etc.) or array subschemas, |
| 100 | // we want to make sure that errors are only shown for the correct subschema. |
| 101 | // Therefore, we compare the error's parent schema with the property's schema. |
| 102 | // In the primitive case the error's data path is the same for all subschemas: |
| 103 | // It directly points to the property defining the anyOf/oneOf. |
| 104 | // The same holds true for errors on the array level (e.g. min item amount). |
| 105 | // In contrast, this comparison must not be done for errors whose parent schema defines an object or a oneOf enum, |
| 106 | // because the parent schema can never match the property schema (e.g. for 'required' checks). |
| 107 | const parentSchema: JsonSchema | undefined = error.parentSchema; |
| 108 | if ( |
| 109 | result && |
| 110 | !isObjectSchema(parentSchema) && |
| 111 | !isOneOfEnumSchema(parentSchema) && |
| 112 | combinatorPaths.findIndex((p) => instancePath.startsWith(p)) !== -1 |
| 113 | ) { |
| 114 | result = result && isEqual(parentSchema, schema); |
| 115 | } |
| 116 | return result; |
| 117 | }); |
| 118 | }; |
| 119 | |
| 120 | /** |
| 121 | * @returns true if the schema describes an object. |
no test coverage detected
searching dependent graphs…