@poseidat/schemas
Version:
The core schemas for the PoseiDAT data interchange formats
53 lines • 2.03 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateSchema = void 0;
var ajv_1 = __importDefault(require("ajv"));
var ajv_formats_1 = __importDefault(require("ajv-formats"));
var ajv_keywords_1 = __importDefault(require("ajv-keywords"));
var schemas_1 = require("./schemas");
// We will pre-load all the Journal related schemas into our validator
// We need to take namespaces into account (core.equipment, etc)
// Extracting schemas can be done by looking for presence of $schema property
//
var findSchemas = function (schemaSection) {
var schemas = schemaSection.filter(function (schema) { return schema.$schema !== undefined; });
var namespaces = schemaSection.filter(function (schema) { return schema.$schema === undefined; });
// Recursively check namespace children
//
for (var _i = 0, namespaces_1 = namespaces; _i < namespaces_1.length; _i++) {
var namespace = namespaces_1[_i];
schemas.push.apply(schemas, findSchemas(Object.values(namespace)));
}
return schemas;
};
var jsonSchemas = findSchemas(Object.values(schemas_1.schemas));
// Our validator instance
//
var validator = new ajv_1.default({
$data: true,
allErrors: true,
schemas: jsonSchemas,
});
// Extend AJV with formats and keyword validation
//
(0, ajv_formats_1.default)(validator);
(0, ajv_keywords_1.default)(validator);
/**
* Validate a single object with a provided schema
*
* @export
* @param {*} object The object to validate
* @param {*} schema The JSON schema to use for validation. Defaults to journal schema
* @returns {Ajv.ErrorObject[]}
*/
function validateSchema(_a) {
var object = _a.object, schema = _a.schema;
var validate = validator.compile(schema);
validate(object);
return validate.errors || [];
}
exports.validateSchema = validateSchema;
//# sourceMappingURL=schema-validator.js.map