UNPKG

@sprucelabs/schema

Version:

Static and dynamic binding plus runtime validation and transformation to ensure your app is sound. 🤓

35 lines (34 loc) • 1.1 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = validateSchema; const SpruceError_1 = __importDefault(require("../errors/SpruceError")); function validateSchema(schema) { 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_1.default({ code: 'INVALID_SCHEMA', schemaId: schema?.id ?? 'ID MISSING', errors, }); } }