UNPKG

@open-kappa/myjson

Version:

A simple JSON management library.

133 lines 3.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MyJson = void 0; const myjsonImpl_1 = require("./myjsonImpl"); /** Key for symbol name. */ const KEY_NAME = "_okmj_impl"; /** * @brief Base class for all JSON objects. */ class MyJson { /** * @brief Constructor. * @param {boolean} isMandatory True if the object must appear in the * hierarchy. * @param {string} name The name of the object. */ constructor(isMandatory, name) { const anySelf = this; const props = new myjsonImpl_1.MyJsonProperties(isMandatory, name); anySelf[Symbol.for(KEY_NAME)] = props; } /** * @brief Return the internal properties of the object. * It uses a symbol to hide the properties to "usual" javascript methods. * @private * @return {MyJsonProperties} The object properties. */ _getValidatorProperties() { const anySelf = this; return anySelf[Symbol.for(KEY_NAME)]; } /** * @brief Throw a validation error. * @protected * @param {string} msg Te error message. * @throws {Error} The error. */ _throwValidatorError(msg) { const self = this; const name = self._getValidatorProperties().name; let all = msg; if (name !== "") all = name + ": " + msg; throw new Error(all); } /** * @brief Get if the object is mandatory. * @return {boolean} True if it is mandatory. */ isMandatory() { return this._getValidatorProperties().isMandatory; } /** * @brief Get if the object has been initialized. * @return {boolean} True if initialized. */ isInitialized() { return this._getValidatorProperties().isInitialized; } /** * @brief Set if the object has been initialized. * @param {boolean} initialized True if initialized. */ _setInitialized(initialized) { this._getValidatorProperties().isInitialized = initialized; } /** * @brief Get the name of this json object. * @return {string} The name. */ getJsonName() { return this._getValidatorProperties().name; } /** * @brief Get the lidt of constraints. * @protected * @return {Array<(ref: MyJson) => string>} The list of constraints. */ getConstraints() { return this._getValidatorProperties().constraints; } /** * @brief Set the name of this json object. * @protected * @param {string} The name. */ setJsonName(name) { this._getValidatorProperties().name = name; } /** * @brief Add a constraint which must hold to validate the JSON. * The constraint takes a reference to this, and must return an empty string * if satisfyed, a message otherwise. * @param {foo: (ref: MyJson) => string} foo The constraint. */ addConstraint(foo) { const self = this; self.getConstraints().push(foo); } checkConstraints() { const self = this; function doCheck(constraint) { const ret = constraint(self); if (ret === "") return; self._throwValidatorError(ret); } self.getConstraints().forEach(doCheck); } /** * @brief Parse a JSON object. * @param {any} json The JSON to parse. * @throws {Error} If a validation error occurrs. */ parseJson(json) { const self = this; self.clear(); self._setInitialized(true); self.parseJsonImpl(json); self.checkConstraints(); } /** * @brief Clear the object content. */ clear() { const self = this; self._setInitialized(false); self.clearImpl(); } } exports.MyJson = MyJson; exports.default = MyJson; //# sourceMappingURL=MyJson.js.map