/** * this plugin validates documents before they can be inserted into the RxCollection. * It's using ajv as jsonschema-validator * @link https://github.com/epoberezkin/ajv */ import Ajv from 'ajv'; import type { RxDocumentData, RxJsonSchema } from 'nxdb-old/src/types'; import { wrappedValidateStorageFactory } from 'nxdb-old/src/plugin-helpers'; const ajv = new Ajv({ strict: false }); export function getValidator( schema: RxJsonSchema ) { const validator = ajv.compile(schema); return (docData: RxDocumentData) => { const isValid = validator(docData); if (isValid) { return []; } else { return validator.errors as any; } }; } export const wrappedValidateAjvStorage = wrappedValidateStorageFactory( getValidator, 'ajv' );