[6a3a178] | 1 | import Ajv, {AnySchema, AnyValidateFunction, ErrorObject} from "../core"
|
---|
| 2 | import standaloneCode from "."
|
---|
| 3 | import * as requireFromString from "require-from-string"
|
---|
| 4 |
|
---|
| 5 | export default class AjvPack {
|
---|
| 6 | errors?: ErrorObject[] | null // errors from the last validation
|
---|
| 7 | constructor(readonly ajv: Ajv) {}
|
---|
| 8 |
|
---|
| 9 | validate(schemaKeyRef: AnySchema | string, data: unknown): boolean | Promise<unknown> {
|
---|
| 10 | return Ajv.prototype.validate.call(this, schemaKeyRef, data)
|
---|
| 11 | }
|
---|
| 12 |
|
---|
| 13 | compile<T = unknown>(schema: AnySchema, meta?: boolean): AnyValidateFunction<T> {
|
---|
| 14 | return this.getStandalone(this.ajv.compile<T>(schema, meta))
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 | getSchema<T = unknown>(keyRef: string): AnyValidateFunction<T> | undefined {
|
---|
| 18 | const v = this.ajv.getSchema<T>(keyRef)
|
---|
| 19 | if (!v) return undefined
|
---|
| 20 | return this.getStandalone(v)
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | private getStandalone<T = unknown>(v: AnyValidateFunction<T>): AnyValidateFunction<T> {
|
---|
| 24 | return requireFromString(standaloneCode(this.ajv, v)) as AnyValidateFunction<T>
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | addSchema(...args: Parameters<typeof Ajv.prototype.addSchema>): AjvPack {
|
---|
| 28 | this.ajv.addSchema.call(this.ajv, ...args)
|
---|
| 29 | return this
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | addKeyword(...args: Parameters<typeof Ajv.prototype.addKeyword>): AjvPack {
|
---|
| 33 | this.ajv.addKeyword.call(this.ajv, ...args)
|
---|
| 34 | return this
|
---|
| 35 | }
|
---|
| 36 | }
|
---|