@apistudio/apim-cli
Version:
CLI for API Management Products
27 lines (26 loc) • 814 B
JavaScript
/**
* Copyright IBM Corp. 2024, 2025
*/
import { Ajv } from 'ajv';
function validateSchema(data, schema) {
if (typeof schema === 'string')
schema = JSON.parse(schema);
if (typeof schema !== 'object' || schema === null)
throw new Error('Invalid schema');
const ajv = new Ajv();
const validate = ajv.compile(schema);
const valid = validate(data);
return { valid, errors: validate.errors };
}
export function validSchemaAssertion(data, schema) {
const { valid, errors } = validateSchema(data, schema);
if (!valid) {
throw new Error(errors?.[0]?.message ?? 'Invalid schema');
}
}
export function invalidSchemaAssertion(data, schema) {
const { valid } = validateSchema(data, schema);
if (valid) {
throw new Error('Schema is valid');
}
}