@sprucelabs/schema
Version:
Static and dynamic binding plus runtime validation and transformation to ensure your app is sound. 🤓
30 lines (29 loc) • 930 B
JavaScript
import SpruceError from '../errors/SpruceError.js';
export default function validateSchema(schema) {
var _a;
const errors = [];
if (!schema) {
errors.push('definition_empty');
}
else {
if (!schema.id) {
errors.push('id_missing');
}
else if (!(typeof schema.id === 'string')) {
errors.push('id_not_string');
}
if (schema.name && !(typeof schema.name === 'string')) {
errors.push('name_not_string');
}
if (!schema.fields && !schema.dynamicFieldSignature) {
errors.push('needs_fields_or_dynamic_field_signature');
}
}
if (errors.length > 0) {
throw new SpruceError({
code: 'INVALID_SCHEMA',
schemaId: (_a = schema === null || schema === void 0 ? void 0 : schema.id) !== null && _a !== void 0 ? _a : 'ID MISSING',
errors,
});
}
}