UNPKG

unleash-server

Version:

Unleash is an enterprise ready feature flag service. It provides different strategies for handling feature flags.

41 lines 1.34 kB
import { Ajv } from 'ajv'; import { schemas } from './index.js'; import { omitKeys } from '../util/index.js'; import { fromOpenApiValidationErrors } from '../error/bad-data-error.js'; const ajv = new Ajv({ schemas: Object.values(schemas).map((schema) => omitKeys(schema, 'components')), // example was superseded by examples in openapi 3.1, but we're still on 3.0, so // let's add it back in! keywords: ['example', 'x-enforcer-exception-skip-codes'], formats: { 'date-time': true, date: true, uri: true, }, code: { esm: true, }, }); export const addAjvSchema = (schemaObjects) => { const newSchemas = schemaObjects.filter((schema) => !ajv.getSchema(schema.$id)); return ajv.addSchema(newSchemas); }; export const validateSchema = (schema, data) => { if (!ajv.validate(schema, data)) { return { schema, errors: ajv.errors ?? [], }; } }; export const throwOnInvalidSchema = (schema, data) => { const validationErrors = validateSchema(schema, data); if (validationErrors) { const [firstError, ...remainingErrors] = validationErrors.errors; throw fromOpenApiValidationErrors(data, [ firstError, ...remainingErrors, ]); } }; //# sourceMappingURL=validate.js.map