@imbricate/core
Version:
Imbricate Core, Notebook for Engineers
109 lines (108 loc) • 4.31 kB
JavaScript
;
/**
* @author WMXPY
* @namespace Database
* @description Schema
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateImbricateSchema = exports.validateImbricateSchemaProperty = void 0;
const type_1 = require("../property/type");
/**
* Validate a schema property
*
* @param property property to validate
*
* @returns a string error message if validation failed
* null if validation passed
*/
const validateImbricateSchemaProperty = (property) => {
if (typeof property.propertyIdentifier !== "string") {
return "Property identifier must be a string";
}
if (typeof property.propertyName !== "string") {
return "Property name must be a string";
}
if (!Object.values(type_1.IMBRICATE_PROPERTY_TYPE).includes(property.propertyType)) {
return "Property type must be a valid type";
}
// IMBRICATE_PROPERTY_TYPE SWITCH
switch (property.propertyType) {
case type_1.IMBRICATE_PROPERTY_TYPE.BINARY: {
if (typeof property.propertyOptions !== "object") {
return "Property options must be an object";
}
const propertyOptions = property.propertyOptions;
if (typeof propertyOptions.allowMultiple !== "boolean") {
return "Property options allowMultiple must be a boolean";
}
break;
}
case type_1.IMBRICATE_PROPERTY_TYPE.LABEL: {
if (typeof property.propertyOptions !== "object") {
return "Property options must be an object";
}
const propertyOptions = property.propertyOptions;
if (typeof propertyOptions.allowMultiple !== "boolean") {
return "Property options allowMultiple must be a boolean";
}
if (!Array.isArray(propertyOptions.labelOptions)) {
return "Property options labelOptions must be an array";
}
for (const labelOption of propertyOptions.labelOptions) {
if (typeof labelOption.labelIdentifier !== "string") {
return "Property options labelOptions labelIdentifier must be a string";
}
}
break;
}
case type_1.IMBRICATE_PROPERTY_TYPE.REFERENCE: {
if (typeof property.propertyOptions !== "object") {
return "Property options must be an object";
}
const propertyOptions = property.propertyOptions;
if (typeof propertyOptions.allowMultiple !== "boolean") {
return "Property options allowMultiple must be a boolean";
}
if (!Array.isArray(propertyOptions.databases)) {
return "Property options databases must be an array";
}
for (const database of propertyOptions.databases) {
if (typeof database.originUniqueIdentifier !== "string") {
return "Database originUniqueIdentifier must be a string";
}
if (typeof database.databaseUniqueIdentifier !== "string") {
return "Database databaseUniqueIdentifier must be a string";
}
}
break;
}
}
return null;
};
exports.validateImbricateSchemaProperty = validateImbricateSchemaProperty;
/**
* Validate a schema
*
* @param schema database schema to validate
*
* @returns a string error message if validation failed
* null if validation passed
*/
const validateImbricateSchema = (schema) => {
if (!Array.isArray(schema.properties)) {
return "Properties must be an array";
}
const propertyNames = new Set();
for (const property of schema.properties) {
const propertyValidationResult = (0, exports.validateImbricateSchemaProperty)(property);
if (typeof propertyValidationResult === "string") {
return `Invalid property ${property.propertyName}, ${propertyValidationResult}`;
}
if (propertyNames.has(property.propertyName)) {
return `Duplicated property name ${property.propertyName}`;
}
propertyNames.add(property.propertyName);
}
return null;
};
exports.validateImbricateSchema = validateImbricateSchema;