| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103 |
- "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,
- defaultValueSetter: () => defaultValueSetter,
- 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_service16 = 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<string, object>}
- */
- _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.isEmptyOf(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.isEmptyOf(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.isEmptyOf(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.isEmptyOf(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.isEmptyOf(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.isEmptyOf(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.isEmptyOf(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.isEmptyOf(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.isEmptyOf(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.isEmptyOf(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.isEmptyOf(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-parsers/default-value-setter.js
- var import_js_service15 = require("@e22m4u/js-service");
- var import_js_empty_values12 = require("@e22m4u/js-empty-values");
- function defaultValueSetter(value, schema, options, container) {
- if (options && options.noDefaultValues) {
- return value;
- }
- if (schema.default === void 0) {
- return value;
- }
- const emptyValues = container.get(import_js_empty_values12.EmptyValuesService);
- const dataType = schema.type || DataType.ANY;
- if (!emptyValues.isEmptyOf(dataType, value)) {
- return value;
- }
- if (typeof schema.default === "function") {
- return schema.default(container);
- }
- return structuredClone(schema.default);
- }
- __name(defaultValueSetter, "defaultValueSetter");
- // src/data-parser.js
- var _DataParser = class _DataParser extends import_js_service16.Service {
- /**
- * Parsers.
- *
- * @type {Function[]}
- */
- _parsers = [
- stringTypeParser,
- booleanTypeParser,
- numberTypeParser,
- arrayTypeParser,
- objectTypeParser,
- defaultValueSetter
- ];
- /**
- * 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.noDefaultValues !== void 0) {
- if (typeof options.noDefaultValues !== "boolean") {
- throw new import_js_format8.InvalidArgumentError(
- 'Option "noDefaultValues" must be a Boolean, but %v was given.',
- options.noDefaultValues
- );
- }
- }
- 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,
- defaultValueSetter,
- getDataTypeFromValue,
- numberTypeParser,
- numberTypeValidator,
- objectTypeParser,
- objectTypeValidator,
- requiredValueValidator,
- stringTypeParser,
- stringTypeValidator,
- validateDataSchema,
- validateDataSchemaDefinition
- });
|