"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.js var index_exports = {}; __export(index_exports, { DATA_TYPE_LIST: () => DATA_TYPE_LIST, DataParser: () => DataParser, DataParsingError: () => DataParsingError, DataSchemaRegistry: () => DataSchemaRegistry, DataSchemaResolver: () => DataSchemaResolver, DataType: () => DataType, DataValidationError: () => DataValidationError, DataValidator: () => DataValidator, arrayTypeParser: () => arrayTypeParser, arrayTypeValidator: () => arrayTypeValidator, booleanTypeParser: () => booleanTypeParser, booleanTypeValidator: () => booleanTypeValidator, getDataTypeFromValue: () => getDataTypeFromValue, numberTypeParser: () => numberTypeParser, numberTypeValidator: () => numberTypeValidator, objectTypeParser: () => objectTypeParser, objectTypeValidator: () => objectTypeValidator, requiredValueValidator: () => requiredValueValidator, stringTypeParser: () => stringTypeParser, stringTypeValidator: () => stringTypeValidator, validateDataSchema: () => validateDataSchema, validateDataSchemaDefinition: () => validateDataSchemaDefinition }); module.exports = __toCommonJS(index_exports); // src/data-type.js var DataType = { ANY: "any", STRING: "string", NUMBER: "number", BOOLEAN: "boolean", ARRAY: "array", OBJECT: "object" }; var DATA_TYPE_LIST = Object.values(DataType); function getDataTypeFromValue(value) { if (value == null) return DataType.ANY; const baseType = typeof value; if (baseType === "string") return DataType.STRING; if (baseType === "number") return DataType.NUMBER; if (baseType === "boolean") return DataType.BOOLEAN; if (Array.isArray(value)) return DataType.ARRAY; if (baseType === "object") return DataType.OBJECT; return DataType.ANY; } __name(getDataTypeFromValue, "getDataTypeFromValue"); // src/data-parser.js var import_js_service15 = require("@e22m4u/js-service"); // src/data-validator.js var import_js_service9 = require("@e22m4u/js-service"); var import_js_format7 = require("@e22m4u/js-format"); // src/data-schema-resolver.js var import_js_service2 = require("@e22m4u/js-service"); var import_js_format4 = require("@e22m4u/js-format"); // src/validate-data-schema.js var import_js_format = require("@e22m4u/js-format"); function validateDataSchema(schema, shallowMode = false, validatedSchemas = /* @__PURE__ */ new Set()) { if (typeof shallowMode !== "boolean") { throw new import_js_format.InvalidArgumentError( 'Argument "shallowMode" must be a Boolean, but %v was given.', shallowMode ); } if (!(validatedSchemas instanceof Set)) { throw new import_js_format.InvalidArgumentError( 'Argument "validatedSchemas" must be an instance of Set, but %v was given.', validatedSchemas ); } if (validatedSchemas.has(schema)) { return; } if (!schema || typeof schema !== "object" && typeof schema !== "function" && typeof schema !== "string" || Array.isArray(schema)) { throw new import_js_format.InvalidArgumentError( "Data schema must be an Object, a Function or a non-empty String, but %v was given.", schema ); } if (typeof schema !== "object") { return; } validatedSchemas.add(schema); if (schema.type !== void 0) { if (!schema.type || !DATA_TYPE_LIST.includes(schema.type)) { throw new import_js_format.InvalidArgumentError( 'Schema option "type" must be one of values: %l, but %v was given.', DATA_TYPE_LIST, schema.type ); } } if (schema.items !== void 0) { if (!schema.items || typeof schema.items !== "object" && typeof schema.items !== "function" && typeof schema.items !== "string" || Array.isArray(schema.items)) { throw new import_js_format.InvalidArgumentError( 'Schema option "items" must be an Object, a Function or a non-empty String, but %v was given.', schema.items ); } if (schema.type !== DataType.ARRAY) { throw new import_js_format.InvalidArgumentError( 'Schema option "items" is only allowed for the "array" type, but %v was given.', schema.type ); } if (!shallowMode && typeof schema.items === "object") { validateDataSchema(schema.items, shallowMode, validatedSchemas); } } if (schema.properties !== void 0) { if (!schema.properties || typeof schema.properties !== "object" && typeof schema.properties !== "function" && typeof schema.properties !== "string" || Array.isArray(schema.properties)) { throw new import_js_format.InvalidArgumentError( 'Schema option "properties" must be an Object, a Function or a non-empty String, but %v was given.', schema.properties ); } if (schema.type !== DataType.OBJECT) { throw new import_js_format.InvalidArgumentError( 'Schema option "properties" is only allowed for the "object" type, but %v was given.', schema.type ); } if (typeof schema.properties === "object") { Object.values(schema.properties).forEach((propSchema) => { if (propSchema === void 0) { return; } if (!propSchema || typeof propSchema !== "object" && typeof propSchema !== "function" && typeof propSchema !== "string" || Array.isArray(propSchema)) { throw new import_js_format.InvalidArgumentError( "Property schema must be an Object, a Function or a non-empty String, but %v was given.", propSchema ); } if (!shallowMode && typeof propSchema === "object") { validateDataSchema(propSchema, shallowMode, validatedSchemas); } }); } } if (schema.required !== void 0 && typeof schema.required !== "boolean") { throw new import_js_format.InvalidArgumentError( 'Schema option "required" must be a Boolean, but %v was given.', schema.required ); } } __name(validateDataSchema, "validateDataSchema"); // src/data-schema-registry.js var import_js_service = require("@e22m4u/js-service"); var import_js_format3 = require("@e22m4u/js-format"); // src/validate-data-schema-definition.js var import_js_format2 = require("@e22m4u/js-format"); function validateDataSchemaDefinition(schemaDef) { if (!schemaDef || typeof schemaDef !== "object" || Array.isArray(schemaDef)) { throw new import_js_format2.InvalidArgumentError( "Schema definition must be an Object, but %v was given.", schemaDef ); } if (!schemaDef.name || typeof schemaDef.name !== "string") { throw new import_js_format2.InvalidArgumentError( 'Definition option "name" must be a non-empty String, but %v was given.', schemaDef.name ); } if (!schemaDef.schema || typeof schemaDef.schema !== "object" && typeof schemaDef.schema !== "function" && typeof schemaDef.schema !== "string" || Array.isArray(schemaDef.schema)) { throw new import_js_format2.InvalidArgumentError( 'Definition option "schema" must be an Object, a Function or a non-empty String, but %v was given.', schemaDef.schema ); } validateDataSchema(schemaDef.schema); } __name(validateDataSchemaDefinition, "validateDataSchemaDefinition"); // src/data-schema-registry.js var _DataSchemaRegistry = class _DataSchemaRegistry extends import_js_service.Service { /** * Definitions. * * @type {Map} */ _definitions = /* @__PURE__ */ new Map(); /** * Define schema. * * @param {object} schemaDef * @returns {this} */ defineSchema(schemaDef) { validateDataSchemaDefinition(schemaDef); if (this._definitions.has(schemaDef.name)) { throw new import_js_format3.InvalidArgumentError( "Data schema %v is already registered.", schemaDef.name ); } this._definitions.set(schemaDef.name, schemaDef); return this; } /** * Has schema. * * @param {string} schemaName * @returns {boolean} */ hasSchema(schemaName) { return this._definitions.has(schemaName); } /** * Get schema. * * @param {string} schemaName * @returns {object} */ getSchema(schemaName) { const schemaDef = this._definitions.get(schemaName); if (!schemaDef) { throw new import_js_format3.InvalidArgumentError( "Data schema %v is not found.", schemaName ); } return schemaDef.schema; } /** * Get definition. * * @param {string} schemaName * @returns {object} */ getDefinition(schemaName) { const schemaDef = this._definitions.get(schemaName); if (!schemaDef) { throw new import_js_format3.InvalidArgumentError( "Schema definition %v is not found.", schemaName ); } return schemaDef; } }; __name(_DataSchemaRegistry, "DataSchemaRegistry"); var DataSchemaRegistry = _DataSchemaRegistry; // src/data-schema-resolver.js var _DataSchemaResolver = class _DataSchemaResolver extends import_js_service2.Service { /** * Resolve schema. * * @param {object|Function|string} schema * @returns {object} */ resolve(schema) { if (typeof schema === "function") { schema = schema(this.container); if (!schema || typeof schema !== "object" && typeof schema !== "string" || Array.isArray(schema)) { throw new import_js_format4.InvalidArgumentError( "Schema factory must return an Object or a non-empty String, but %v was given.", schema ); } } if (schema && typeof schema === "string") { schema = this.getService(DataSchemaRegistry).getSchema(schema); if (!schema || typeof schema !== "object" && typeof schema !== "function" && typeof schema !== "string" || Array.isArray(schema)) { throw new import_js_format4.InvalidArgumentError( "Named schema must be an Object, a Function or a non-empty String, but %v was given.", schema ); } if (typeof schema === "string" || typeof schema === "function") { return this.resolve(schema); } } validateDataSchema(schema, true); return schema; } }; __name(_DataSchemaResolver, "DataSchemaResolver"); var DataSchemaResolver = _DataSchemaResolver; // src/data-validators/array-type-validator.js var import_js_service3 = require("@e22m4u/js-service"); // src/utils/to-pascal-case.js function toPascalCase(input) { if (!input) return ""; return input.replace(/([a-z])([A-Z])/g, "$1 $2").replace(/([0-9])([a-zA-Z])/g, "$1 $2").replace(/[-_]+|[^\p{L}\p{N}]/gu, " ").toLowerCase().replace(new RegExp("(?:^|\\s)(\\p{L})", "gu"), (_, letter) => letter.toUpperCase()).replace(/\s+/g, ""); } __name(toPascalCase, "toPascalCase"); // src/errors/data-parsing-error.js var import_js_format5 = require("@e22m4u/js-format"); var _DataParsingError = class _DataParsingError extends import_js_format5.InvalidArgumentError { /** * Value. * * @type {*} */ value; /** * Target type. * * @type {string} */ targetType; /** * Source path. * * @type {string|undefined} */ sourcePath; /** * Constructor. * * @param {*} value * @param {string} targetType * @param {string} [sourcePath] */ constructor(value, targetType, sourcePath) { const targetTypePc = toPascalCase(targetType); let message = ""; if (sourcePath) { message = (0, import_js_format5.format)( "Unable to parse %v from %v as %s.", value, sourcePath, targetTypePc ); } else { message = (0, import_js_format5.format)("Unable to parse %v as %s.", value, targetTypePc); } super(message); this.value = value; this.targetType = targetType; this.sourcePath = sourcePath; } }; __name(_DataParsingError, "DataParsingError"); var DataParsingError = _DataParsingError; // src/errors/data-validation-error.js var import_js_format6 = require("@e22m4u/js-format"); var _DataValidationError = class _DataValidationError extends import_js_format6.InvalidArgumentError { }; __name(_DataValidationError, "DataValidationError"); var DataValidationError = _DataValidationError; // src/data-validators/array-type-validator.js var import_js_empty_values = require("@e22m4u/js-empty-values"); function arrayTypeValidator(value, schema, options, container) { if (schema.type !== DataType.ARRAY) { return; } const emptyValues = container.get(import_js_empty_values.EmptyValuesService); const dataType = schema.type || DataType.ANY; if (emptyValues.isEmptyByType(dataType, value)) { return; } if (Array.isArray(value)) { return; } const sourcePath = options && options.sourcePath; if (sourcePath) { throw new DataValidationError( "Value of %v must be an Array, but %v was given.", sourcePath, value ); } else { throw new DataValidationError( "Value must be an Array, but %v was given.", value ); } } __name(arrayTypeValidator, "arrayTypeValidator"); // src/data-validators/object-type-validator.js var import_js_service4 = require("@e22m4u/js-service"); var import_js_empty_values2 = require("@e22m4u/js-empty-values"); function objectTypeValidator(value, schema, options, container) { if (schema.type !== DataType.OBJECT) { return; } const emptyValues = container.get(import_js_empty_values2.EmptyValuesService); const dataType = schema.type || DataType.ANY; if (emptyValues.isEmptyByType(dataType, value)) { return; } if (value !== null && typeof value === "object" && !Array.isArray(value)) { return; } const sourcePath = options && options.sourcePath; if (sourcePath) { throw new DataValidationError( "Value of %v must be an Object, but %v was given.", sourcePath, value ); } else { throw new DataValidationError( "Value must be an Object, but %v was given.", value ); } } __name(objectTypeValidator, "objectTypeValidator"); // src/data-validators/string-type-validator.js var import_js_service5 = require("@e22m4u/js-service"); var import_js_empty_values3 = require("@e22m4u/js-empty-values"); function stringTypeValidator(value, schema, options, container) { if (schema.type !== DataType.STRING) { return; } const emptyValues = container.get(import_js_empty_values3.EmptyValuesService); const dataType = schema.type || DataType.ANY; if (emptyValues.isEmptyByType(dataType, value)) { return; } if (typeof value === "string") { return; } const sourcePath = options && options.sourcePath; if (sourcePath) { throw new DataValidationError( "Value of %v must be a String, but %v was given.", sourcePath, value ); } else { throw new DataValidationError( "Value must be a String, but %v was given.", value ); } } __name(stringTypeValidator, "stringTypeValidator"); // src/data-validators/number-type-validator.js var import_js_service6 = require("@e22m4u/js-service"); var import_js_empty_values4 = require("@e22m4u/js-empty-values"); function numberTypeValidator(value, schema, options, container) { if (schema.type !== DataType.NUMBER) { return; } const emptyValues = container.get(import_js_empty_values4.EmptyValuesService); const dataType = schema.type || DataType.ANY; if (emptyValues.isEmptyByType(dataType, value)) { return; } if (typeof value === "number") { return; } const sourcePath = options && options.sourcePath; if (sourcePath) { throw new DataValidationError( "Value of %v must be a Number, but %v was given.", sourcePath, value ); } else { throw new DataValidationError( "Value must be a Number, but %v was given.", value ); } } __name(numberTypeValidator, "numberTypeValidator"); // src/data-validators/boolean-type-validator.js var import_js_service7 = require("@e22m4u/js-service"); var import_js_empty_values5 = require("@e22m4u/js-empty-values"); function booleanTypeValidator(value, schema, options, container) { if (schema.type !== DataType.BOOLEAN) { return; } const emptyValues = container.get(import_js_empty_values5.EmptyValuesService); const dataType = schema.type || DataType.ANY; if (emptyValues.isEmptyByType(dataType, value)) { return; } if (typeof value === "boolean") { return; } const sourcePath = options && options.sourcePath; if (sourcePath) { throw new DataValidationError( "Value of %v must be a Boolean, but %v was given.", sourcePath, value ); } else { throw new DataValidationError( "Value must be a Boolean, but %v was given.", value ); } } __name(booleanTypeValidator, "booleanTypeValidator"); // src/data-validators/required-value-validator.js var import_js_service8 = require("@e22m4u/js-service"); var import_js_empty_values6 = require("@e22m4u/js-empty-values"); function requiredValueValidator(value, schema, options, container) { if (schema.required !== true) { return; } const emptyValues = container.get(import_js_empty_values6.EmptyValuesService); const dataType = schema.type || DataType.ANY; if (!emptyValues.isEmptyByType(dataType, value)) { return; } const sourcePath = options && options.sourcePath; if (sourcePath) { throw new DataValidationError( "Value of %v is required, but %v was given.", sourcePath, value ); } else { throw new DataValidationError( "Value is required, but %v was given.", value ); } } __name(requiredValueValidator, "requiredValueValidator"); // src/data-validator.js var _DataValidator = class _DataValidator extends import_js_service9.Service { /** * Validators. * * @type {Function[]} */ _validators = [ stringTypeValidator, booleanTypeValidator, numberTypeValidator, objectTypeValidator, arrayTypeValidator, requiredValueValidator ]; /** * Get validators. * * @returns {Function[]} */ getValidators() { return [...this._validators]; } /** * Set validators. * * @param {Function[]} list * @returns {this} */ setValidators(list) { if (!Array.isArray(list)) { throw new import_js_format7.InvalidArgumentError( "Data validators must be an Array, but %v was given.", list ); } list.forEach((validator) => { if (typeof validator !== "function") { throw new import_js_format7.InvalidArgumentError( "Data validator must be a Function, but %v was given.", validator ); } }); this._validators = [...list]; return this; } /** * Define schema. * * @param {object} schemaDef * @returns {this} */ defineSchema(schemaDef) { this.getService(DataSchemaRegistry).defineSchema(schemaDef); return this; } /** * Has schema. * * @param {string} schemaName * @returns {boolean} */ hasSchema(schemaName) { return this.getService(DataSchemaRegistry).hasSchema(schemaName); } /** * Get schema. * * @param {string} schemaName * @returns {object} */ getSchema(schemaName) { return this.getService(DataSchemaRegistry).getSchema(schemaName); } /** * Validate. * * @param {*} value * @param {object|Function|string} schema * @param {object} [options] */ validate(value, schema, options) { if (options !== void 0) { if (options === null || typeof options !== "object" || Array.isArray(options)) { throw new import_js_format7.InvalidArgumentError( "Validation options must be an Object, but %v was given.", options ); } if (options.sourcePath !== void 0) { if (!options.sourcePath || typeof options.sourcePath !== "string") { throw new import_js_format7.InvalidArgumentError( 'Option "sourcePath" must be a non-empty String, but %v was given.', options.sourcePath ); } } if (options.shallowMode !== void 0) { if (typeof options.shallowMode !== "boolean") { throw new import_js_format7.InvalidArgumentError( 'Option "shallowMode" must be a Boolean, but %v was given.', options.shallowMode ); } } } const sourcePath = options && options.sourcePath || void 0; const shallowMode = Boolean(options && options.shallowMode); validateDataSchema(schema, true); const schemaResolver = this.getService(DataSchemaResolver); if (typeof schema !== "object") { schema = schemaResolver.resolve(schema); } this._validators.forEach((validate) => { validate(value, schema, options, this.container); }); if (shallowMode) { return; } if (Array.isArray(value) && schema.items !== void 0) { value.forEach((item, index) => { const itemPath = (sourcePath || "array") + `[${index}]`; const itemOptions = { ...options, sourcePath: itemPath }; this.validate(item, schema.items, itemOptions); }); } else if (value !== null && typeof value === "object" && schema.properties !== void 0) { let propsSchema = schema.properties; if (typeof propsSchema !== "object") { const resolvedSchema = schemaResolver.resolve(propsSchema); if (resolvedSchema.type !== DataType.OBJECT) { throw new import_js_format7.InvalidArgumentError( 'Unable to get the "properties" option from the data schema of %v type.', resolvedSchema.type || DataType.ANY ); } propsSchema = resolvedSchema.properties || {}; } Object.keys(propsSchema).forEach((propName) => { const propSchema = propsSchema[propName]; if (propSchema === void 0) { return; } const propValue = value[propName]; const propPath = sourcePath ? sourcePath + `.${propName}` : propName; const propOptions = { ...options, sourcePath: propPath }; this.validate(propValue, propSchema, propOptions); }); } } }; __name(_DataValidator, "DataValidator"); var DataValidator = _DataValidator; // src/data-parser.js var import_js_format8 = require("@e22m4u/js-format"); // src/data-parsers/array-type-parser.js var import_js_service10 = require("@e22m4u/js-service"); var import_js_empty_values7 = require("@e22m4u/js-empty-values"); function arrayTypeParser(value, schema, options, container) { if (schema.type !== DataType.ARRAY) { return value; } if (Array.isArray(value)) { return value; } if (typeof value === "string") { value = value.trim(); let newValue; try { newValue = JSON.parse(value); } catch { } if (Array.isArray(newValue)) { return newValue; } } const dataType = schema.type || DataType.ANY; const emptyValues = container.get(import_js_empty_values7.EmptyValuesService); if (emptyValues.isEmptyByType(dataType, value)) { return value; } if (!options || !options.noParsingErrors) { const sourcePath = options && options.sourcePath; throw new DataParsingError(value, dataType, sourcePath); } return value; } __name(arrayTypeParser, "arrayTypeParser"); // src/data-parsers/string-type-parser.js var import_js_service11 = require("@e22m4u/js-service"); var import_js_empty_values8 = require("@e22m4u/js-empty-values"); function stringTypeParser(value, schema, options, container) { if (schema.type !== DataType.STRING) { return value; } if (typeof value === "string") { return value; } if (typeof value === "number") { return String(value); } const dataType = schema.type || DataType.ANY; const emptyValues = container.get(import_js_empty_values8.EmptyValuesService); if (emptyValues.isEmptyByType(dataType, value)) { return value; } if (!options || !options.noParsingErrors) { const sourcePath = options && options.sourcePath; throw new DataParsingError(value, dataType, sourcePath); } return value; } __name(stringTypeParser, "stringTypeParser"); // src/data-parsers/number-type-parser.js var import_js_service12 = require("@e22m4u/js-service"); var import_js_empty_values9 = require("@e22m4u/js-empty-values"); function numberTypeParser(value, schema, options, container) { if (schema.type !== DataType.NUMBER) { return value; } if (typeof value === "number") { return value; } if (value && typeof value === "string") { if (value.length <= 20) { const newValue = Number(value.trim()); if (!isNaN(newValue)) { return newValue; } } } const dataType = schema.type || DataType.ANY; const emptyValues = container.get(import_js_empty_values9.EmptyValuesService); if (emptyValues.isEmptyByType(dataType, value)) { return value; } if (!options || !options.noParsingErrors) { const sourcePath = options && options.sourcePath; throw new DataParsingError(value, dataType, sourcePath); } return value; } __name(numberTypeParser, "numberTypeParser"); // src/data-parsers/object-type-parser.js var import_js_service13 = require("@e22m4u/js-service"); var import_js_empty_values10 = require("@e22m4u/js-empty-values"); function objectTypeParser(value, schema, options, container) { if (schema.type !== DataType.OBJECT) { return value; } if (value !== null && typeof value === "object" && !Array.isArray(value)) { return value; } if (typeof value === "string") { value = value.trim(); let newValue; try { newValue = JSON.parse(value); } catch { } if (newValue !== null && typeof newValue === "object" && !Array.isArray(newValue)) { return newValue; } } const dataType = schema.type || DataType.ANY; const emptyValues = container.get(import_js_empty_values10.EmptyValuesService); if (emptyValues.isEmptyByType(dataType, value)) { return value; } if (!options || !options.noParsingErrors) { const sourcePath = options && options.sourcePath; throw new DataParsingError(value, dataType, sourcePath); } return value; } __name(objectTypeParser, "objectTypeParser"); // src/data-parsers/boolean-type-parser.js var import_js_service14 = require("@e22m4u/js-service"); var import_js_empty_values11 = require("@e22m4u/js-empty-values"); function booleanTypeParser(value, schema, options, container) { if (schema.type !== DataType.BOOLEAN) { return value; } if (typeof value === "boolean") { return value; } if (typeof value === "string") { value = value.trim(); if (value === "1") return true; if (value === "0") return false; if (value === "true") return true; if (value === "false") return false; } else if (typeof value === "number") { if (value === 1) return true; if (value === 0) return false; } const dataType = schema.type || DataType.ANY; const emptyValues = container.get(import_js_empty_values11.EmptyValuesService); if (emptyValues.isEmptyByType(dataType, value)) { return value; } if (!options || !options.noParsingErrors) { const sourcePath = options && options.sourcePath; throw new DataParsingError(value, dataType, sourcePath); } return value; } __name(booleanTypeParser, "booleanTypeParser"); // src/data-parser.js var _DataParser = class _DataParser extends import_js_service15.Service { /** * Parsers. * * @type {Function[]} */ _parsers = [ stringTypeParser, booleanTypeParser, numberTypeParser, arrayTypeParser, objectTypeParser ]; /** * Get parsers. * * @returns {Function[]} */ getParsers() { return [...this._parsers]; } /** * Set parsers. * * @param {Function[]} list * @returns {this} */ setParsers(list) { if (!Array.isArray(list)) { throw new import_js_format8.InvalidArgumentError( "Data parsers must be an Array, but %v was given.", list ); } list.forEach((parser) => { if (typeof parser !== "function") { throw new import_js_format8.InvalidArgumentError( "Data parser must be a Function, but %v was given.", parser ); } }); this._parsers = [...list]; return this; } /** * Define schema. * * @param {object} schemaDef * @returns {this} */ defineSchema(schemaDef) { this.getService(DataSchemaRegistry).defineSchema(schemaDef); return this; } /** * Has schema. * * @param {string} schemaName * @returns {boolean} */ hasSchema(schemaName) { return this.getService(DataSchemaRegistry).hasSchema(schemaName); } /** * Get schema. * * @param {string} schemaName * @returns {object} */ getSchema(schemaName) { return this.getService(DataSchemaRegistry).getSchema(schemaName); } /** * Parse. * * @param {*} value * @param {object|Function|string} schema * @param {object} [options] * @returns {*} */ parse(value, schema, options) { if (options !== void 0) { if (options === null || typeof options !== "object" || Array.isArray(options)) { throw new import_js_format8.InvalidArgumentError( "Parsing options must be an Object, but %v was given.", options ); } if (options.sourcePath !== void 0) { if (!options.sourcePath || typeof options.sourcePath !== "string") { throw new import_js_format8.InvalidArgumentError( 'Option "sourcePath" must be a non-empty String, but %v was given.', options.sourcePath ); } } if (options.shallowMode !== void 0) { if (typeof options.shallowMode !== "boolean") { throw new import_js_format8.InvalidArgumentError( 'Option "shallowMode" must be a Boolean, but %v was given.', options.shallowMode ); } } if (options.noParsingErrors !== void 0) { if (typeof options.noParsingErrors !== "boolean") { throw new import_js_format8.InvalidArgumentError( 'Option "noParsingErrors" must be a Boolean, but %v was given.', options.noParsingErrors ); } } } const sourcePath = options && options.sourcePath || void 0; const shallowMode = Boolean(options && options.shallowMode); const noParsingErrors = Boolean(options && options.noParsingErrors); validateDataSchema(schema, true); const schemaResolver = this.getService(DataSchemaResolver); if (typeof schema !== "object") { schema = schemaResolver.resolve(schema); } value = this._parsers.reduce((input, parser) => { return parser(input, schema, options, this.container); }, value); if (!shallowMode) { if (Array.isArray(value) && schema.items !== void 0) { value = [...value]; value.forEach((item, index) => { const itemPath = (sourcePath || "array") + `[${index}]`; const itemOptions = { ...options, sourcePath: itemPath }; value[index] = this.parse(item, schema.items, itemOptions); }); } else if (value !== null && typeof value === "object" && schema.properties !== void 0) { let propsSchema = schema.properties; value = { ...value }; if (typeof propsSchema !== "object") { const resolvedSchema = schemaResolver.resolve(propsSchema); if (resolvedSchema.type !== DataType.OBJECT) { throw new import_js_format8.InvalidArgumentError( 'Unable to get the "properties" option from the data schema of %v type.', resolvedSchema.type || DataType.ANY ); } propsSchema = resolvedSchema.properties || {}; } Object.keys(propsSchema).forEach((propName) => { const propSchema = propsSchema[propName]; if (propSchema === void 0) { return; } const propValue = value[propName]; const propPath = sourcePath ? sourcePath + `.${propName}` : propName; const propOptions = { ...options, sourcePath: propPath }; const newPropValue = this.parse(propValue, propSchema, propOptions); if (value[propName] !== newPropValue) { value[propName] = newPropValue; } }); } } if (!noParsingErrors) { const validator = this.getService(DataValidator); validator.validate(value, schema, { shallowMode: true }); } return value; } }; __name(_DataParser, "DataParser"); var DataParser = _DataParser; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { DATA_TYPE_LIST, DataParser, DataParsingError, DataSchemaRegistry, DataSchemaResolver, DataType, DataValidationError, DataValidator, arrayTypeParser, arrayTypeValidator, booleanTypeParser, booleanTypeValidator, getDataTypeFromValue, numberTypeParser, numberTypeValidator, objectTypeParser, objectTypeValidator, requiredValueValidator, stringTypeParser, stringTypeValidator, validateDataSchema, validateDataSchemaDefinition });