| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071 |
- "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_service5 = require("@e22m4u/js-service");
- // src/data-validator.js
- var import_js_service3 = 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 extends import_js_service.Service {
- static {
- __name(this, "DataSchemaRegistry");
- }
- /**
- * Definitions.
- *
- * @type {Map<string, object>}
- */
- _definitions = /* @__PURE__ */ new Map();
- /**
- * Define schema.
- *
- * @param {object} schemaDef
- * @returns {this}
- */
- defineSchema(schemaDef) {
- validateDataSchemaDefinition(schemaDef);
- 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;
- }
- };
- // src/data-schema-resolver.js
- var DataSchemaResolver = class extends import_js_service2.Service {
- static {
- __name(this, "DataSchemaResolver");
- }
- /**
- * 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;
- }
- };
- // 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 extends import_js_format5.InvalidArgumentError {
- static {
- __name(this, "DataParsingError");
- }
- /**
- * 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;
- }
- };
- // src/errors/data-validation-error.js
- var import_js_format6 = require("@e22m4u/js-format");
- var DataValidationError = class extends import_js_format6.InvalidArgumentError {
- static {
- __name(this, "DataValidationError");
- }
- };
- // src/data-validators/array-type-validator.js
- function arrayTypeValidator(value, schema, options) {
- if (schema.type !== DataType.ARRAY) {
- return;
- }
- if (value == null) {
- 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
- function objectTypeValidator(value, schema, options) {
- if (schema.type !== DataType.OBJECT) {
- return;
- }
- if (value == null) {
- 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
- function stringTypeValidator(value, schema, options) {
- if (schema.type !== DataType.STRING) {
- return;
- }
- if (value == null) {
- 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
- function numberTypeValidator(value, schema, options) {
- if (schema.type !== DataType.NUMBER) {
- return;
- }
- if (value == null) {
- 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
- function booleanTypeValidator(value, schema, options) {
- if (schema.type !== DataType.BOOLEAN) {
- return;
- }
- if (value == null) {
- 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
- function requiredValueValidator(value, schema, options) {
- if (schema.required !== true) {
- return;
- }
- if (value != null) {
- 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 extends import_js_service3.Service {
- static {
- __name(this, "DataValidator");
- }
- /**
- * 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);
- });
- }
- }
- };
- // src/data-parser.js
- var import_js_format8 = require("@e22m4u/js-format");
- // src/data-parsers/array-type-parser.js
- function arrayTypeParser(value, schema, options) {
- 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;
- }
- }
- if (value == null) {
- return value;
- }
- if (!options || !options.noParsingErrors) {
- const sourcePath = options && options.sourcePath;
- const dataType = schema.type || DataType.ANY;
- throw new DataParsingError(value, dataType, sourcePath);
- }
- return value;
- }
- __name(arrayTypeParser, "arrayTypeParser");
- // src/data-parsers/string-type-parser.js
- function stringTypeParser(value, schema, options) {
- if (schema.type !== DataType.STRING) {
- return value;
- }
- if (typeof value === "string") {
- return value;
- }
- if (typeof value === "number") {
- return String(value);
- }
- if (value == null) {
- return value;
- }
- if (!options || !options.noParsingErrors) {
- const sourcePath = options && options.sourcePath;
- const dataType = schema.type || DataType.ANY;
- throw new DataParsingError(value, dataType, sourcePath);
- }
- return value;
- }
- __name(stringTypeParser, "stringTypeParser");
- // src/data-parsers/number-type-parser.js
- function numberTypeParser(value, schema, options) {
- if (schema.type !== DataType.NUMBER) {
- return value;
- }
- if (typeof value === "number") {
- return value;
- }
- if (typeof value === "string" && value.trim() !== "") {
- if (value.length <= 20) {
- const newValue = Number(value.trim());
- if (!isNaN(newValue)) {
- return newValue;
- }
- }
- }
- if (value == null) {
- return value;
- }
- if (!options || !options.noParsingErrors) {
- const sourcePath = options && options.sourcePath;
- const dataType = schema.type || DataType.ANY;
- throw new DataParsingError(value, dataType, sourcePath);
- }
- return value;
- }
- __name(numberTypeParser, "numberTypeParser");
- // src/data-parsers/object-type-parser.js
- function objectTypeParser(value, schema, options) {
- 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;
- }
- }
- if (value == null) {
- return value;
- }
- if (!options || !options.noParsingErrors) {
- const sourcePath = options && options.sourcePath;
- const dataType = schema.type || DataType.ANY;
- throw new DataParsingError(value, dataType, sourcePath);
- }
- return value;
- }
- __name(objectTypeParser, "objectTypeParser");
- // src/data-parsers/boolean-type-parser.js
- function booleanTypeParser(value, schema, options) {
- 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;
- }
- if (value == null) {
- return value;
- }
- if (!options || !options.noParsingErrors) {
- const sourcePath = options && options.sourcePath;
- const dataType = schema.type || DataType.ANY;
- throw new DataParsingError(value, dataType, sourcePath);
- }
- return value;
- }
- __name(booleanTypeParser, "booleanTypeParser");
- // src/data-parsers/default-value-setter.js
- var import_js_service4 = require("@e22m4u/js-service");
- function defaultValueSetter(value, schema, options, container) {
- if (options && options.noDefaultValues) {
- return value;
- }
- if (schema.default === void 0) {
- return value;
- }
- if (value != null) {
- return value;
- }
- if (typeof schema.default === "function") {
- return schema.default(container);
- }
- return schema.default && typeof schema.default === "object" ? structuredClone(schema.default) : schema.default;
- }
- __name(defaultValueSetter, "defaultValueSetter");
- // src/data-parser.js
- var DataParser = class extends import_js_service5.Service {
- static {
- __name(this, "DataParser");
- }
- /**
- * 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
- );
- }
- }
- if (options.keepUnknownProperties !== void 0) {
- if (typeof options.keepUnknownProperties !== "boolean") {
- throw new import_js_format8.InvalidArgumentError(
- 'Option "keepUnknownProperties" must be a Boolean, but %v was given.',
- options.keepUnknownProperties
- );
- }
- }
- }
- const sourcePath = options && options.sourcePath || void 0;
- const shallowMode = Boolean(options && options.shallowMode);
- const noParsingErrors = Boolean(options && options.noParsingErrors);
- const keepUnknownProperties = Boolean(
- options && options.keepUnknownProperties
- );
- 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;
- 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 || {};
- }
- let newValue = keepUnknownProperties ? { ...value } : {};
- 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 (propName in value || value[propName] !== newPropValue) {
- newValue[propName] = newPropValue;
- }
- });
- value = newValue;
- }
- }
- if (!noParsingErrors) {
- const validator = this.getService(DataValidator);
- validator.validate(value, schema, { shallowMode: true, sourcePath });
- }
- return value;
- }
- };
- // 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
- });
|