UNPKG

@ceramicnetwork/stream-model-instance-handler

Version:

Ceramic Model Instance Document stream handler

31 lines 954 B
import Ajv from 'ajv/dist/2020.js'; import addFormats from 'ajv-formats'; function buildAjv() { const validator = new Ajv({ strict: true, allErrors: true, allowMatchingProperties: false, ownProperties: false, unevaluated: false, }); addFormats(validator); return validator; } export class SchemaValidation { constructor() { this.ajv = buildAjv(); } validateSchema(content, schema, schemaId) { let existingSchema = this.ajv.getSchema(schemaId); if (!existingSchema) { this.ajv.addSchema(schema, schemaId); existingSchema = this.ajv.getSchema(schemaId); } const isValid = existingSchema(content); if (!isValid) { const errorMessages = this.ajv.errorsText(existingSchema.errors); throw new Error(`Validation Error: ${errorMessages}`); } } } //# sourceMappingURL=schema-utils.js.map