-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Wrapper for AJV to streamline Json validation logic. - Collapse multiple `required` errors into one - Collapse multiple `additionalProperties` and `unevaluatedProperties` errors into one, and print out which properties are prohibited. - Make `enum` errors print out the supported enum values. Signed-off-by: Theo Truong <[email protected]>
- Loading branch information
Showing
16 changed files
with
275 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,5 +40,4 @@ properties: | |
type: string | ||
required: | ||
- title | ||
- version | ||
- $schema | ||
- version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
import { Ajv2019 as AJV, ErrorsTextOptions, ErrorObject } from 'ajv/dist/2019' | ||
import _ from 'lodash' | ||
|
||
export default class AjvErrorsParser { | ||
private readonly ajv: AJV | ||
private readonly options: ErrorsTextOptions | ||
|
||
constructor(ajv: AJV, options: ErrorsTextOptions = {}) { | ||
this.ajv = ajv | ||
this.options = { separator: ' --- ', ...options } | ||
} | ||
|
||
parse(errors: ErrorObject[] | undefined | null): string { | ||
const error_groups = this.#group_errors(errors ?? []) | ||
const parsed_errors = [ | ||
this.#prohibited_property_error(error_groups.prohibited), | ||
this.#required_property_error(error_groups.required), | ||
this.#enum_error(error_groups.enum), | ||
...error_groups.others | ||
].filter(e => e != null) as ErrorObject[] | ||
return this.ajv.errorsText(parsed_errors, this.options) | ||
} | ||
|
||
#group_errors(errors: ErrorObject[]): { required: ErrorObject[], prohibited: ErrorObject[], enum: ErrorObject[], others: ErrorObject[] } { | ||
const categories = { | ||
required: [] as ErrorObject[], | ||
prohibited: [] as ErrorObject[], | ||
enum: [] as ErrorObject[], | ||
others: [] as ErrorObject[] | ||
} | ||
_.values(_.groupBy(errors, 'instancePath')).forEach((path_errors) => { | ||
for (const error of path_errors) { | ||
switch (error.keyword) { | ||
case 'required': | ||
categories.required.push(error) | ||
break | ||
case 'unevaluatedProperties': | ||
case 'additionalProperties': | ||
categories.prohibited.push(error) | ||
break | ||
case 'enum': | ||
categories.enum.push(error) | ||
break | ||
default: | ||
categories.others.push(error) | ||
} | ||
} | ||
}) | ||
return categories | ||
} | ||
|
||
#prohibited_property_error(errors: ErrorObject[]): ErrorObject | undefined { | ||
if (errors.length === 0) return | ||
const properties = errors.map((error) => error.params.additionalProperty ?? error.params.unevaluatedProperty).join(', ') | ||
return { | ||
keyword: 'prohibited', | ||
instancePath: errors[0].instancePath, | ||
schemaPath: errors[0].schemaPath, | ||
params: {}, | ||
message: `contains unsupported properties: ${properties}` | ||
} | ||
} | ||
|
||
#required_property_error(errors: ErrorObject[]): ErrorObject | undefined { | ||
if (errors.length === 0) return | ||
const properties = errors.map((error) => error.params.missingProperty).join(', ') | ||
return { | ||
keyword: 'required', | ||
instancePath: errors[0].instancePath, | ||
schemaPath: errors[0].schemaPath, | ||
params: {}, | ||
message: `MUST contain the missing properties: ${properties}` | ||
} | ||
} | ||
|
||
#enum_error(errors: ErrorObject[]): ErrorObject | undefined { | ||
if (errors.length === 0) return | ||
const allowed_values = errors[0].params.allowedValues.join(', ') | ||
return { | ||
keyword: 'enum', | ||
instancePath: errors[0].instancePath, | ||
schemaPath: errors[0].schemaPath, | ||
params: {}, | ||
message: `MUST be equal to one of the allowed values: ${allowed_values}` | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
import { Ajv2019 as AJV, ErrorsTextOptions, Options as AjvOptions, ValidateFunction } from 'ajv/dist/2019' | ||
import ajv_errors, { ErrorMessageOptions } from 'ajv-errors' | ||
import addFormats from 'ajv-formats'; | ||
import AjvErrorsParser from './AjvErrorsParser'; | ||
|
||
interface JsonSchemaValidatorOpts { | ||
ajv_opts?: AjvOptions, | ||
ajv_errors_opts?: ErrorMessageOptions, | ||
errors_text_opts?: ErrorsTextOptions | ||
additional_keywords?: string[], | ||
reference_schemas?: Record<string, Record<any, any>> | ||
} | ||
|
||
const DEFAULT_AJV_OPTS = { | ||
strict: true, | ||
allErrors: true | ||
} | ||
|
||
// Wrapper for AJV | ||
export default class JsonSchemaValidator { | ||
private readonly ajv: AJV | ||
private readonly errors_parser: AjvErrorsParser | ||
private readonly _validate: ValidateFunction | undefined | ||
message: string | undefined | ||
|
||
constructor(default_schema?: Record<any, any>, options: JsonSchemaValidatorOpts = {}) { | ||
this.ajv = new AJV({ ...DEFAULT_AJV_OPTS, ...options.ajv_opts }) | ||
addFormats(this.ajv); | ||
if (options.ajv_errors_opts != null) ajv_errors(this.ajv, options.ajv_errors_opts) | ||
for (const keyword of options.additional_keywords ?? []) this.ajv.addKeyword(keyword) | ||
Object.entries(options.reference_schemas ?? {}).forEach(([key, schema]) => this.ajv.addSchema(schema, key)) | ||
this.errors_parser = new AjvErrorsParser(this.ajv, options.errors_text_opts) | ||
if (default_schema) this._validate = this.ajv.compile(default_schema) | ||
} | ||
|
||
validate_data(data: any, schema?: Record<any, any>): string | undefined { | ||
if (schema) return this.#validate(this.ajv.compile(schema), data) | ||
if (this._validate) return this.#validate(this._validate, data) | ||
throw new Error('No schema provided') | ||
} | ||
|
||
validate_schema(schema: Record<any, any>): string | undefined { | ||
const validate_func = this.ajv.validateSchema.bind(this.ajv) as ValidateFunction | ||
return this.#validate(validate_func, schema, true) | ||
} | ||
|
||
#validate(validate_func: ValidateFunction, data: any, is_schema: boolean = false): string | undefined { | ||
const valid = validate_func(data) as boolean | ||
const errors = is_schema ? this.ajv.errors : validate_func.errors | ||
return valid ? undefined : this.errors_parser.parse(errors) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.