UNPKG

@open-kappa/myjson

Version:

A simple JSON management library.

199 lines 7.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MyJsonFactory = void 0; /* eslint-disable class-methods-use-this */ const myjsonImpl_1 = require("./myjsonImpl"); /** * @brief Support factory class. */ class MyJsonFactory { /** * @brief Constructor. */ constructor() { // ntd } /** * @brief Build an array object. * @typeparam Element The element of the object. * @param {Element} element The validator element. * @param {boolean} isMandatory Whether the property is mandatory. * @param {string} name The property name. * @return {MyJsonArray<Element>} The built property. */ makeArray(element, isMandatory, name) { return new myjsonImpl_1.MyJsonArray(element, isMandatory, name); } /** * @brief Build a fixed object. * @param {boolean} isMandatory Whether the property is mandatory. * @param {string} name The property name. * @param {Array<MyJson>} elementsList The list of properties to add. * @return {MyJsonFixed} The built property. */ makeFixed(isMandatory, name, elementsList = []) { const ret = new myjsonImpl_1.MyJsonFixed(isMandatory, name); function doInsert(el) { ret.add(el); } elementsList.forEach(doInsert); return ret; } /** * @brief Build a flex object. * @typeparam Element The element of the object. * @param {Element} element The validator element. * @param {boolean} isMandatory Whether the property is mandatory. * @param {string} name The property name. * @param {Array<MyJson>} elementsList The list of properties to add. * @return {MyJsonFlex<Element>} The built property. */ makeFlex(element, isMandatory, name, elementsList = []) { const ret = new myjsonImpl_1.MyJsonFlex(element, isMandatory, name); function doInsert(el) { ret.add(el); } elementsList.forEach(doInsert); return ret; } /** * @brief Build a map object. * @typeparam Element The element of the object. * @param {Element} element The validator element. * @param {boolean} isMandatory Whether the property is mandatory. * @param {string} name The property name. * @return {MyJsonMap<Element>} The built property. */ makeMap(element, isMandatory, name) { return new myjsonImpl_1.MyJsonMap(element, isMandatory, name); } /** * @brief Build a value object. * @typeparam Element The element of the value. * @param {Element} element The defaut value. * @param {boolean} isMandatory Whether the property is mandatory. * @param {string} name The property name. * @return {MyJsonValue<Element>} The built property. */ makeValue(element, isMandatory, name) { return new myjsonImpl_1.MyJsonValue(element, isMandatory, name); } /** * @brief Build a value string object. * @param {boolean} isMandatory Whether the property is mandatory. * @param {string} name The property name. * @param {string} [value=""] The defaut value. * @return {MyJsonValue<string>} The built property. */ makeString(isMandatory, name, value = "") { return this.makeValue(value, isMandatory, name); } /** * @brief Build a value number object. * @param {boolean} isMandatory Whether the property is mandatory. * @param {string} name The property name. * @param {string} [value=0] The defaut value. * @return {MyJsonValue<number>} The built property. */ makeNumber(isMandatory, name, value = 0) { return this.makeValue(value, isMandatory, name); } /** * @brief Build a value string object. * @param {boolean} isMandatory Whether the property is mandatory. * @param {string} name The property name. * @param {boolean} [value=false] The defaut value. * @return {MyJsonValue<boolean>} The built property. */ makeBoolean(isMandatory, name, value = false) { return this.makeValue(value, isMandatory, name); } /** * @brief Create and return a enum constraint. * @typeparam Element The type of enum values. * @param {Array<Element>} values The enum values. * @return {(ref: MyJson) => string} The contraint method. */ getEnumConstraint(values) { function constraint(ref) { if (!(ref instanceof myjsonImpl_1.MyJsonValue)) { return "Enum constrain not applyed to MyJsonValue"; } const refValue = ref; if (values.indexOf(refValue.getValue()) === -1) { return "Invalid enum value: " + refValue.getValue(); } return ""; } return constraint; } /** * @brief Create and return a field constraint. * If the first field exists and holds the given value, then the fields in * the list must exists. * @typeparam Element The type of the value. * @param {string} field1 The field to check. * @param {Element | null} value1 The reference value. If null, the value is * not checked. * @param {Array<string>} fieldsList The fields to check. * @return {(ref: MyJson) => string} The contraint method. */ getFieldConstraint(field1, value1, fieldsList) { function constraint(ref) { let obj1 = null; let has2 = true; if (ref instanceof myjsonImpl_1.MyJsonFixed) { const obj = ref; if (obj.has(field1)) obj1 = obj.get(field1); // eslint-disable-next-line no-inner-declarations function doHas(f2) { has2 = has2 && obj.has(f2) && obj.get(f2).isInitialized(); } fieldsList.forEach(doHas); } else if (ref instanceof myjsonImpl_1.MyJsonFlex) { const obj = ref; if (obj.has(field1)) obj1 = obj.get(field1); // eslint-disable-next-line no-inner-declarations function doHas(f2) { has2 = has2 && obj.has(f2) && obj.get(f2).isInitialized(); } fieldsList.forEach(doHas); } else if (ref instanceof myjsonImpl_1.MyJsonMap) { const obj = ref; if (obj.has(field1)) obj1 = obj.get(field1); // eslint-disable-next-line no-inner-declarations function doHas(f2) { has2 = has2 && obj.has(f2) && obj.get(f2).isInitialized(); } fieldsList.forEach(doHas); } else { return "Field constrain applyed to invalid object"; } if (obj1 === null) return ""; if (value1 !== null) { if (!(obj1 instanceof myjsonImpl_1.MyJsonValue)) { return "Invalid field matching object."; } const f1 = obj1; if (f1.getValue() !== value1) { return ""; } } if (!has2) { return "Missing required field: " + fieldsList; } return ""; } return constraint; } } exports.MyJsonFactory = MyJsonFactory; exports.default = MyJsonFactory; //# sourceMappingURL=MyJsonFactory.js.map