Skip to content

Commit

Permalink
#
Browse files Browse the repository at this point in the history
Signed-off-by: Theo Truong <[email protected]>
  • Loading branch information
nhtruong committed Jul 9, 2024
1 parent ab286e4 commit 989f2cb
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions tools/src/_utils/JsonSchemaValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const DEFAULT_AJV_OPTS = {
export default class JsonSchemaValidator {
private readonly ajv: AJV
private readonly errors_parser: AjvErrorsParser
private _validate: ValidateFunction | undefined
private readonly _validate: ValidateFunction | undefined
errors: AjvError[] | null | undefined
message: string | undefined

Expand All @@ -46,14 +46,19 @@ export default class JsonSchemaValidator {
if (schema) this._validate = this.ajv.compile(schema)
}

validate(data: any, schema: Record<any, any> | undefined): boolean {
let validate_func: ValidateFunction
if (!schema)
if (!this._validate) throw new Error('No schema provided')
else validate_func = this._validate
else validate_func = this.ajv.compile(schema)
validate_data(data: Record<any, any>, schema: Record<any, any> | undefined): boolean {
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>): boolean {
const validate_func = this.ajv.validateSchema.bind(this.ajv) as ValidateFunction
return this.#validate(validate_func, schema)
}

const valid = validate_func(data)
#validate(validate_func: ValidateFunction, data: Record<any, any>): boolean {
const valid = validate_func(data) as boolean
if (valid) {
this.errors = undefined
this.message = undefined
Expand Down

0 comments on commit 989f2cb

Please sign in to comment.