import Ajv, {AnySchema, AnyValidateFunction, ErrorObject} from "../core" import standaloneCode from "." import * as requireFromString from "require-from-string" export default class AjvPack { errors?: ErrorObject[] | null // errors from the last validation constructor(readonly ajv: Ajv) {} validate(schemaKeyRef: AnySchema | string, data: unknown): boolean | Promise { return Ajv.prototype.validate.call(this, schemaKeyRef, data) } compile(schema: AnySchema, meta?: boolean): AnyValidateFunction { return this.getStandalone(this.ajv.compile(schema, meta)) } getSchema(keyRef: string): AnyValidateFunction | undefined { const v = this.ajv.getSchema(keyRef) if (!v) return undefined return this.getStandalone(v) } private getStandalone(v: AnyValidateFunction): AnyValidateFunction { return requireFromString(standaloneCode(this.ajv, v)) as AnyValidateFunction } addSchema(...args: Parameters): AjvPack { this.ajv.addSchema.call(this.ajv, ...args) return this } addKeyword(...args: Parameters): AjvPack { this.ajv.addKeyword.call(this.ajv, ...args) return this } }