source: trip-planner-front/node_modules/ajv/lib/standalone/instance.ts@ eed0bf8

Last change on this file since eed0bf8 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.2 KB
Line 
1import Ajv, {AnySchema, AnyValidateFunction, ErrorObject} from "../core"
2import standaloneCode from "."
3import * as requireFromString from "require-from-string"
4
5export 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}
Note: See TracBrowser for help on using the repository browser.