index.cjs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
  7. var __export = (target, all) => {
  8. for (var name in all)
  9. __defProp(target, name, { get: all[name], enumerable: true });
  10. };
  11. var __copyProps = (to, from, except, desc) => {
  12. if (from && typeof from === "object" || typeof from === "function") {
  13. for (let key of __getOwnPropNames(from))
  14. if (!__hasOwnProp.call(to, key) && key !== except)
  15. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  16. }
  17. return to;
  18. };
  19. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  20. // src/index.js
  21. var index_exports = {};
  22. __export(index_exports, {
  23. DATA_TYPE_LIST: () => DATA_TYPE_LIST,
  24. DataParser: () => DataParser,
  25. DataParsingError: () => DataParsingError,
  26. DataSchemaRegistry: () => DataSchemaRegistry,
  27. DataSchemaResolver: () => DataSchemaResolver,
  28. DataType: () => DataType,
  29. DataValidationError: () => DataValidationError,
  30. DataValidator: () => DataValidator,
  31. arrayTypeParser: () => arrayTypeParser,
  32. arrayTypeValidator: () => arrayTypeValidator,
  33. booleanTypeParser: () => booleanTypeParser,
  34. booleanTypeValidator: () => booleanTypeValidator,
  35. defaultValueSetter: () => defaultValueSetter,
  36. getDataTypeFromValue: () => getDataTypeFromValue,
  37. numberTypeParser: () => numberTypeParser,
  38. numberTypeValidator: () => numberTypeValidator,
  39. objectTypeParser: () => objectTypeParser,
  40. objectTypeValidator: () => objectTypeValidator,
  41. requiredValueValidator: () => requiredValueValidator,
  42. stringTypeParser: () => stringTypeParser,
  43. stringTypeValidator: () => stringTypeValidator,
  44. validateDataSchema: () => validateDataSchema,
  45. validateDataSchemaDefinition: () => validateDataSchemaDefinition
  46. });
  47. module.exports = __toCommonJS(index_exports);
  48. // src/data-type.js
  49. var DataType = {
  50. ANY: "any",
  51. STRING: "string",
  52. NUMBER: "number",
  53. BOOLEAN: "boolean",
  54. ARRAY: "array",
  55. OBJECT: "object"
  56. };
  57. var DATA_TYPE_LIST = Object.values(DataType);
  58. function getDataTypeFromValue(value) {
  59. if (value == null) return DataType.ANY;
  60. const baseType = typeof value;
  61. if (baseType === "string") return DataType.STRING;
  62. if (baseType === "number") return DataType.NUMBER;
  63. if (baseType === "boolean") return DataType.BOOLEAN;
  64. if (Array.isArray(value)) return DataType.ARRAY;
  65. if (baseType === "object") return DataType.OBJECT;
  66. return DataType.ANY;
  67. }
  68. __name(getDataTypeFromValue, "getDataTypeFromValue");
  69. // src/data-parser.js
  70. var import_js_service5 = require("@e22m4u/js-service");
  71. // src/data-validator.js
  72. var import_js_service3 = require("@e22m4u/js-service");
  73. var import_js_format7 = require("@e22m4u/js-format");
  74. // src/data-schema-resolver.js
  75. var import_js_service2 = require("@e22m4u/js-service");
  76. var import_js_format4 = require("@e22m4u/js-format");
  77. // src/validate-data-schema.js
  78. var import_js_format = require("@e22m4u/js-format");
  79. function validateDataSchema(schema, shallowMode = false, validatedSchemas = /* @__PURE__ */ new Set()) {
  80. if (typeof shallowMode !== "boolean") {
  81. throw new import_js_format.InvalidArgumentError(
  82. 'Argument "shallowMode" must be a Boolean, but %v was given.',
  83. shallowMode
  84. );
  85. }
  86. if (!(validatedSchemas instanceof Set)) {
  87. throw new import_js_format.InvalidArgumentError(
  88. 'Argument "validatedSchemas" must be an instance of Set, but %v was given.',
  89. validatedSchemas
  90. );
  91. }
  92. if (validatedSchemas.has(schema)) {
  93. return;
  94. }
  95. if (!schema || typeof schema !== "object" && typeof schema !== "function" && typeof schema !== "string" || Array.isArray(schema)) {
  96. throw new import_js_format.InvalidArgumentError(
  97. "Data schema must be an Object, a Function or a non-empty String, but %v was given.",
  98. schema
  99. );
  100. }
  101. if (typeof schema !== "object") {
  102. return;
  103. }
  104. validatedSchemas.add(schema);
  105. if (schema.type !== void 0) {
  106. if (!schema.type || !DATA_TYPE_LIST.includes(schema.type)) {
  107. throw new import_js_format.InvalidArgumentError(
  108. 'Schema option "type" must be one of values: %l, but %v was given.',
  109. DATA_TYPE_LIST,
  110. schema.type
  111. );
  112. }
  113. }
  114. if (schema.items !== void 0) {
  115. if (!schema.items || typeof schema.items !== "object" && typeof schema.items !== "function" && typeof schema.items !== "string" || Array.isArray(schema.items)) {
  116. throw new import_js_format.InvalidArgumentError(
  117. 'Schema option "items" must be an Object, a Function or a non-empty String, but %v was given.',
  118. schema.items
  119. );
  120. }
  121. if (schema.type !== DataType.ARRAY) {
  122. throw new import_js_format.InvalidArgumentError(
  123. 'Schema option "items" is only allowed for the "array" type, but %v was given.',
  124. schema.type
  125. );
  126. }
  127. if (!shallowMode && typeof schema.items === "object") {
  128. validateDataSchema(schema.items, shallowMode, validatedSchemas);
  129. }
  130. }
  131. if (schema.properties !== void 0) {
  132. if (!schema.properties || typeof schema.properties !== "object" && typeof schema.properties !== "function" && typeof schema.properties !== "string" || Array.isArray(schema.properties)) {
  133. throw new import_js_format.InvalidArgumentError(
  134. 'Schema option "properties" must be an Object, a Function or a non-empty String, but %v was given.',
  135. schema.properties
  136. );
  137. }
  138. if (schema.type !== DataType.OBJECT) {
  139. throw new import_js_format.InvalidArgumentError(
  140. 'Schema option "properties" is only allowed for the "object" type, but %v was given.',
  141. schema.type
  142. );
  143. }
  144. if (typeof schema.properties === "object") {
  145. Object.values(schema.properties).forEach((propSchema) => {
  146. if (propSchema === void 0) {
  147. return;
  148. }
  149. if (!propSchema || typeof propSchema !== "object" && typeof propSchema !== "function" && typeof propSchema !== "string" || Array.isArray(propSchema)) {
  150. throw new import_js_format.InvalidArgumentError(
  151. "Property schema must be an Object, a Function or a non-empty String, but %v was given.",
  152. propSchema
  153. );
  154. }
  155. if (!shallowMode && typeof propSchema === "object") {
  156. validateDataSchema(propSchema, shallowMode, validatedSchemas);
  157. }
  158. });
  159. }
  160. }
  161. if (schema.required !== void 0 && typeof schema.required !== "boolean") {
  162. throw new import_js_format.InvalidArgumentError(
  163. 'Schema option "required" must be a Boolean, but %v was given.',
  164. schema.required
  165. );
  166. }
  167. }
  168. __name(validateDataSchema, "validateDataSchema");
  169. // src/data-schema-registry.js
  170. var import_js_service = require("@e22m4u/js-service");
  171. var import_js_format3 = require("@e22m4u/js-format");
  172. // src/validate-data-schema-definition.js
  173. var import_js_format2 = require("@e22m4u/js-format");
  174. function validateDataSchemaDefinition(schemaDef) {
  175. if (!schemaDef || typeof schemaDef !== "object" || Array.isArray(schemaDef)) {
  176. throw new import_js_format2.InvalidArgumentError(
  177. "Schema definition must be an Object, but %v was given.",
  178. schemaDef
  179. );
  180. }
  181. if (!schemaDef.name || typeof schemaDef.name !== "string") {
  182. throw new import_js_format2.InvalidArgumentError(
  183. 'Definition option "name" must be a non-empty String, but %v was given.',
  184. schemaDef.name
  185. );
  186. }
  187. if (!schemaDef.schema || typeof schemaDef.schema !== "object" && typeof schemaDef.schema !== "function" && typeof schemaDef.schema !== "string" || Array.isArray(schemaDef.schema)) {
  188. throw new import_js_format2.InvalidArgumentError(
  189. 'Definition option "schema" must be an Object, a Function or a non-empty String, but %v was given.',
  190. schemaDef.schema
  191. );
  192. }
  193. validateDataSchema(schemaDef.schema);
  194. }
  195. __name(validateDataSchemaDefinition, "validateDataSchemaDefinition");
  196. // src/data-schema-registry.js
  197. var DataSchemaRegistry = class extends import_js_service.Service {
  198. static {
  199. __name(this, "DataSchemaRegistry");
  200. }
  201. /**
  202. * Definitions.
  203. *
  204. * @type {Map<string, object>}
  205. */
  206. _definitions = /* @__PURE__ */ new Map();
  207. /**
  208. * Define schema.
  209. *
  210. * @param {object} schemaDef
  211. * @returns {this}
  212. */
  213. defineSchema(schemaDef) {
  214. validateDataSchemaDefinition(schemaDef);
  215. if (this._definitions.has(schemaDef.name)) {
  216. throw new import_js_format3.InvalidArgumentError(
  217. "Data schema %v is already registered.",
  218. schemaDef.name
  219. );
  220. }
  221. this._definitions.set(schemaDef.name, schemaDef);
  222. return this;
  223. }
  224. /**
  225. * Has schema.
  226. *
  227. * @param {string} schemaName
  228. * @returns {boolean}
  229. */
  230. hasSchema(schemaName) {
  231. return this._definitions.has(schemaName);
  232. }
  233. /**
  234. * Get schema.
  235. *
  236. * @param {string} schemaName
  237. * @returns {object}
  238. */
  239. getSchema(schemaName) {
  240. const schemaDef = this._definitions.get(schemaName);
  241. if (!schemaDef) {
  242. throw new import_js_format3.InvalidArgumentError(
  243. "Data schema %v is not found.",
  244. schemaName
  245. );
  246. }
  247. return schemaDef.schema;
  248. }
  249. /**
  250. * Get definition.
  251. *
  252. * @param {string} schemaName
  253. * @returns {object}
  254. */
  255. getDefinition(schemaName) {
  256. const schemaDef = this._definitions.get(schemaName);
  257. if (!schemaDef) {
  258. throw new import_js_format3.InvalidArgumentError(
  259. "Schema definition %v is not found.",
  260. schemaName
  261. );
  262. }
  263. return schemaDef;
  264. }
  265. };
  266. // src/data-schema-resolver.js
  267. var DataSchemaResolver = class extends import_js_service2.Service {
  268. static {
  269. __name(this, "DataSchemaResolver");
  270. }
  271. /**
  272. * Resolve schema.
  273. *
  274. * @param {object|Function|string} schema
  275. * @returns {object}
  276. */
  277. resolve(schema) {
  278. if (typeof schema === "function") {
  279. schema = schema(this.container);
  280. if (!schema || typeof schema !== "object" && typeof schema !== "string" || Array.isArray(schema)) {
  281. throw new import_js_format4.InvalidArgumentError(
  282. "Schema factory must return an Object or a non-empty String, but %v was given.",
  283. schema
  284. );
  285. }
  286. }
  287. if (schema && typeof schema === "string") {
  288. schema = this.getService(DataSchemaRegistry).getSchema(schema);
  289. if (!schema || typeof schema !== "object" && typeof schema !== "function" && typeof schema !== "string" || Array.isArray(schema)) {
  290. throw new import_js_format4.InvalidArgumentError(
  291. "Named schema must be an Object, a Function or a non-empty String, but %v was given.",
  292. schema
  293. );
  294. }
  295. if (typeof schema === "string" || typeof schema === "function") {
  296. return this.resolve(schema);
  297. }
  298. }
  299. validateDataSchema(schema, true);
  300. return schema;
  301. }
  302. };
  303. // src/utils/to-pascal-case.js
  304. function toPascalCase(input) {
  305. if (!input) return "";
  306. 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, "");
  307. }
  308. __name(toPascalCase, "toPascalCase");
  309. // src/errors/data-parsing-error.js
  310. var import_js_format5 = require("@e22m4u/js-format");
  311. var DataParsingError = class extends import_js_format5.InvalidArgumentError {
  312. static {
  313. __name(this, "DataParsingError");
  314. }
  315. /**
  316. * Value.
  317. *
  318. * @type {*}
  319. */
  320. value;
  321. /**
  322. * Target type.
  323. *
  324. * @type {string}
  325. */
  326. targetType;
  327. /**
  328. * Source path.
  329. *
  330. * @type {string|undefined}
  331. */
  332. sourcePath;
  333. /**
  334. * Constructor.
  335. *
  336. * @param {*} value
  337. * @param {string} targetType
  338. * @param {string} [sourcePath]
  339. */
  340. constructor(value, targetType, sourcePath) {
  341. const targetTypePc = toPascalCase(targetType);
  342. let message = "";
  343. if (sourcePath) {
  344. message = (0, import_js_format5.format)(
  345. "Unable to parse %v from %v as %s.",
  346. value,
  347. sourcePath,
  348. targetTypePc
  349. );
  350. } else {
  351. message = (0, import_js_format5.format)("Unable to parse %v as %s.", value, targetTypePc);
  352. }
  353. super(message);
  354. this.value = value;
  355. this.targetType = targetType;
  356. this.sourcePath = sourcePath;
  357. }
  358. };
  359. // src/errors/data-validation-error.js
  360. var import_js_format6 = require("@e22m4u/js-format");
  361. var DataValidationError = class extends import_js_format6.InvalidArgumentError {
  362. static {
  363. __name(this, "DataValidationError");
  364. }
  365. };
  366. // src/data-validators/array-type-validator.js
  367. function arrayTypeValidator(value, schema, options) {
  368. if (schema.type !== DataType.ARRAY) {
  369. return;
  370. }
  371. if (value == null) {
  372. return;
  373. }
  374. if (Array.isArray(value)) {
  375. return;
  376. }
  377. const sourcePath = options && options.sourcePath;
  378. if (sourcePath) {
  379. throw new DataValidationError(
  380. "Value of %v must be an Array, but %v was given.",
  381. sourcePath,
  382. value
  383. );
  384. } else {
  385. throw new DataValidationError(
  386. "Value must be an Array, but %v was given.",
  387. value
  388. );
  389. }
  390. }
  391. __name(arrayTypeValidator, "arrayTypeValidator");
  392. // src/data-validators/object-type-validator.js
  393. function objectTypeValidator(value, schema, options) {
  394. if (schema.type !== DataType.OBJECT) {
  395. return;
  396. }
  397. if (value == null) {
  398. return;
  399. }
  400. if (value !== null && typeof value === "object" && !Array.isArray(value)) {
  401. return;
  402. }
  403. const sourcePath = options && options.sourcePath;
  404. if (sourcePath) {
  405. throw new DataValidationError(
  406. "Value of %v must be an Object, but %v was given.",
  407. sourcePath,
  408. value
  409. );
  410. } else {
  411. throw new DataValidationError(
  412. "Value must be an Object, but %v was given.",
  413. value
  414. );
  415. }
  416. }
  417. __name(objectTypeValidator, "objectTypeValidator");
  418. // src/data-validators/string-type-validator.js
  419. function stringTypeValidator(value, schema, options) {
  420. if (schema.type !== DataType.STRING) {
  421. return;
  422. }
  423. if (value == null) {
  424. return;
  425. }
  426. if (typeof value === "string") {
  427. return;
  428. }
  429. const sourcePath = options && options.sourcePath;
  430. if (sourcePath) {
  431. throw new DataValidationError(
  432. "Value of %v must be a String, but %v was given.",
  433. sourcePath,
  434. value
  435. );
  436. } else {
  437. throw new DataValidationError(
  438. "Value must be a String, but %v was given.",
  439. value
  440. );
  441. }
  442. }
  443. __name(stringTypeValidator, "stringTypeValidator");
  444. // src/data-validators/number-type-validator.js
  445. function numberTypeValidator(value, schema, options) {
  446. if (schema.type !== DataType.NUMBER) {
  447. return;
  448. }
  449. if (value == null) {
  450. return;
  451. }
  452. if (typeof value === "number") {
  453. return;
  454. }
  455. const sourcePath = options && options.sourcePath;
  456. if (sourcePath) {
  457. throw new DataValidationError(
  458. "Value of %v must be a Number, but %v was given.",
  459. sourcePath,
  460. value
  461. );
  462. } else {
  463. throw new DataValidationError(
  464. "Value must be a Number, but %v was given.",
  465. value
  466. );
  467. }
  468. }
  469. __name(numberTypeValidator, "numberTypeValidator");
  470. // src/data-validators/boolean-type-validator.js
  471. function booleanTypeValidator(value, schema, options) {
  472. if (schema.type !== DataType.BOOLEAN) {
  473. return;
  474. }
  475. if (value == null) {
  476. return;
  477. }
  478. if (typeof value === "boolean") {
  479. return;
  480. }
  481. const sourcePath = options && options.sourcePath;
  482. if (sourcePath) {
  483. throw new DataValidationError(
  484. "Value of %v must be a Boolean, but %v was given.",
  485. sourcePath,
  486. value
  487. );
  488. } else {
  489. throw new DataValidationError(
  490. "Value must be a Boolean, but %v was given.",
  491. value
  492. );
  493. }
  494. }
  495. __name(booleanTypeValidator, "booleanTypeValidator");
  496. // src/data-validators/required-value-validator.js
  497. function requiredValueValidator(value, schema, options) {
  498. if (schema.required !== true) {
  499. return;
  500. }
  501. if (value != null) {
  502. return;
  503. }
  504. const sourcePath = options && options.sourcePath;
  505. if (sourcePath) {
  506. throw new DataValidationError(
  507. "Value of %v is required, but %v was given.",
  508. sourcePath,
  509. value
  510. );
  511. } else {
  512. throw new DataValidationError(
  513. "Value is required, but %v was given.",
  514. value
  515. );
  516. }
  517. }
  518. __name(requiredValueValidator, "requiredValueValidator");
  519. // src/data-validator.js
  520. var DataValidator = class extends import_js_service3.Service {
  521. static {
  522. __name(this, "DataValidator");
  523. }
  524. /**
  525. * Validators.
  526. *
  527. * @type {Function[]}
  528. */
  529. _validators = [
  530. stringTypeValidator,
  531. booleanTypeValidator,
  532. numberTypeValidator,
  533. objectTypeValidator,
  534. arrayTypeValidator,
  535. requiredValueValidator
  536. ];
  537. /**
  538. * Get validators.
  539. *
  540. * @returns {Function[]}
  541. */
  542. getValidators() {
  543. return [...this._validators];
  544. }
  545. /**
  546. * Set validators.
  547. *
  548. * @param {Function[]} list
  549. * @returns {this}
  550. */
  551. setValidators(list) {
  552. if (!Array.isArray(list)) {
  553. throw new import_js_format7.InvalidArgumentError(
  554. "Data validators must be an Array, but %v was given.",
  555. list
  556. );
  557. }
  558. list.forEach((validator) => {
  559. if (typeof validator !== "function") {
  560. throw new import_js_format7.InvalidArgumentError(
  561. "Data validator must be a Function, but %v was given.",
  562. validator
  563. );
  564. }
  565. });
  566. this._validators = [...list];
  567. return this;
  568. }
  569. /**
  570. * Define schema.
  571. *
  572. * @param {object} schemaDef
  573. * @returns {this}
  574. */
  575. defineSchema(schemaDef) {
  576. this.getService(DataSchemaRegistry).defineSchema(schemaDef);
  577. return this;
  578. }
  579. /**
  580. * Has schema.
  581. *
  582. * @param {string} schemaName
  583. * @returns {boolean}
  584. */
  585. hasSchema(schemaName) {
  586. return this.getService(DataSchemaRegistry).hasSchema(schemaName);
  587. }
  588. /**
  589. * Get schema.
  590. *
  591. * @param {string} schemaName
  592. * @returns {object}
  593. */
  594. getSchema(schemaName) {
  595. return this.getService(DataSchemaRegistry).getSchema(schemaName);
  596. }
  597. /**
  598. * Validate.
  599. *
  600. * @param {*} value
  601. * @param {object|Function|string} schema
  602. * @param {object} [options]
  603. */
  604. validate(value, schema, options) {
  605. if (options !== void 0) {
  606. if (options === null || typeof options !== "object" || Array.isArray(options)) {
  607. throw new import_js_format7.InvalidArgumentError(
  608. "Validation options must be an Object, but %v was given.",
  609. options
  610. );
  611. }
  612. if (options.sourcePath !== void 0) {
  613. if (!options.sourcePath || typeof options.sourcePath !== "string") {
  614. throw new import_js_format7.InvalidArgumentError(
  615. 'Option "sourcePath" must be a non-empty String, but %v was given.',
  616. options.sourcePath
  617. );
  618. }
  619. }
  620. if (options.shallowMode !== void 0) {
  621. if (typeof options.shallowMode !== "boolean") {
  622. throw new import_js_format7.InvalidArgumentError(
  623. 'Option "shallowMode" must be a Boolean, but %v was given.',
  624. options.shallowMode
  625. );
  626. }
  627. }
  628. }
  629. const sourcePath = options && options.sourcePath || void 0;
  630. const shallowMode = Boolean(options && options.shallowMode);
  631. validateDataSchema(schema, true);
  632. const schemaResolver = this.getService(DataSchemaResolver);
  633. if (typeof schema !== "object") {
  634. schema = schemaResolver.resolve(schema);
  635. }
  636. this._validators.forEach((validate) => {
  637. validate(value, schema, options, this.container);
  638. });
  639. if (shallowMode) {
  640. return;
  641. }
  642. if (Array.isArray(value) && schema.items !== void 0) {
  643. value.forEach((item, index) => {
  644. const itemPath = (sourcePath || "array") + `[${index}]`;
  645. const itemOptions = { ...options, sourcePath: itemPath };
  646. this.validate(item, schema.items, itemOptions);
  647. });
  648. } else if (value !== null && typeof value === "object" && schema.properties !== void 0) {
  649. let propsSchema = schema.properties;
  650. if (typeof propsSchema !== "object") {
  651. const resolvedSchema = schemaResolver.resolve(propsSchema);
  652. if (resolvedSchema.type !== DataType.OBJECT) {
  653. throw new import_js_format7.InvalidArgumentError(
  654. 'Unable to get the "properties" option from the data schema of %v type.',
  655. resolvedSchema.type || DataType.ANY
  656. );
  657. }
  658. propsSchema = resolvedSchema.properties || {};
  659. }
  660. Object.keys(propsSchema).forEach((propName) => {
  661. const propSchema = propsSchema[propName];
  662. if (propSchema === void 0) {
  663. return;
  664. }
  665. const propValue = value[propName];
  666. const propPath = sourcePath ? sourcePath + `.${propName}` : propName;
  667. const propOptions = { ...options, sourcePath: propPath };
  668. this.validate(propValue, propSchema, propOptions);
  669. });
  670. }
  671. }
  672. };
  673. // src/data-parser.js
  674. var import_js_format8 = require("@e22m4u/js-format");
  675. // src/data-parsers/array-type-parser.js
  676. function arrayTypeParser(value, schema, options) {
  677. if (schema.type !== DataType.ARRAY) {
  678. return value;
  679. }
  680. if (Array.isArray(value)) {
  681. return value;
  682. }
  683. if (typeof value === "string") {
  684. value = value.trim();
  685. let newValue;
  686. try {
  687. newValue = JSON.parse(value);
  688. } catch {
  689. }
  690. if (Array.isArray(newValue)) {
  691. return newValue;
  692. }
  693. }
  694. if (value == null) {
  695. return value;
  696. }
  697. if (!options || !options.noParsingErrors) {
  698. const sourcePath = options && options.sourcePath;
  699. const dataType = schema.type || DataType.ANY;
  700. throw new DataParsingError(value, dataType, sourcePath);
  701. }
  702. return value;
  703. }
  704. __name(arrayTypeParser, "arrayTypeParser");
  705. // src/data-parsers/string-type-parser.js
  706. function stringTypeParser(value, schema, options) {
  707. if (schema.type !== DataType.STRING) {
  708. return value;
  709. }
  710. if (typeof value === "string") {
  711. return value;
  712. }
  713. if (typeof value === "number") {
  714. return String(value);
  715. }
  716. if (value == null) {
  717. return value;
  718. }
  719. if (!options || !options.noParsingErrors) {
  720. const sourcePath = options && options.sourcePath;
  721. const dataType = schema.type || DataType.ANY;
  722. throw new DataParsingError(value, dataType, sourcePath);
  723. }
  724. return value;
  725. }
  726. __name(stringTypeParser, "stringTypeParser");
  727. // src/data-parsers/number-type-parser.js
  728. function numberTypeParser(value, schema, options) {
  729. if (schema.type !== DataType.NUMBER) {
  730. return value;
  731. }
  732. if (typeof value === "number") {
  733. return value;
  734. }
  735. if (typeof value === "string" && value.trim() !== "") {
  736. if (value.length <= 20) {
  737. const newValue = Number(value.trim());
  738. if (!isNaN(newValue)) {
  739. return newValue;
  740. }
  741. }
  742. }
  743. if (value == null) {
  744. return value;
  745. }
  746. if (!options || !options.noParsingErrors) {
  747. const sourcePath = options && options.sourcePath;
  748. const dataType = schema.type || DataType.ANY;
  749. throw new DataParsingError(value, dataType, sourcePath);
  750. }
  751. return value;
  752. }
  753. __name(numberTypeParser, "numberTypeParser");
  754. // src/data-parsers/object-type-parser.js
  755. function objectTypeParser(value, schema, options) {
  756. if (schema.type !== DataType.OBJECT) {
  757. return value;
  758. }
  759. if (value !== null && typeof value === "object" && !Array.isArray(value)) {
  760. return value;
  761. }
  762. if (typeof value === "string") {
  763. value = value.trim();
  764. let newValue;
  765. try {
  766. newValue = JSON.parse(value);
  767. } catch {
  768. }
  769. if (newValue !== null && typeof newValue === "object" && !Array.isArray(newValue)) {
  770. return newValue;
  771. }
  772. }
  773. if (value == null) {
  774. return value;
  775. }
  776. if (!options || !options.noParsingErrors) {
  777. const sourcePath = options && options.sourcePath;
  778. const dataType = schema.type || DataType.ANY;
  779. throw new DataParsingError(value, dataType, sourcePath);
  780. }
  781. return value;
  782. }
  783. __name(objectTypeParser, "objectTypeParser");
  784. // src/data-parsers/boolean-type-parser.js
  785. function booleanTypeParser(value, schema, options) {
  786. if (schema.type !== DataType.BOOLEAN) {
  787. return value;
  788. }
  789. if (typeof value === "boolean") {
  790. return value;
  791. }
  792. if (typeof value === "string") {
  793. value = value.trim();
  794. if (value === "1") return true;
  795. if (value === "0") return false;
  796. if (value === "true") return true;
  797. if (value === "false") return false;
  798. } else if (typeof value === "number") {
  799. if (value === 1) return true;
  800. if (value === 0) return false;
  801. }
  802. if (value == null) {
  803. return value;
  804. }
  805. if (!options || !options.noParsingErrors) {
  806. const sourcePath = options && options.sourcePath;
  807. const dataType = schema.type || DataType.ANY;
  808. throw new DataParsingError(value, dataType, sourcePath);
  809. }
  810. return value;
  811. }
  812. __name(booleanTypeParser, "booleanTypeParser");
  813. // src/data-parsers/default-value-setter.js
  814. var import_js_service4 = require("@e22m4u/js-service");
  815. function defaultValueSetter(value, schema, options, container) {
  816. if (options && options.noDefaultValues) {
  817. return value;
  818. }
  819. if (schema.default === void 0) {
  820. return value;
  821. }
  822. if (value != null) {
  823. return value;
  824. }
  825. if (typeof schema.default === "function") {
  826. return schema.default(container);
  827. }
  828. return schema.default && typeof schema.default === "object" ? structuredClone(schema.default) : schema.default;
  829. }
  830. __name(defaultValueSetter, "defaultValueSetter");
  831. // src/data-parser.js
  832. var DataParser = class extends import_js_service5.Service {
  833. static {
  834. __name(this, "DataParser");
  835. }
  836. /**
  837. * Parsers.
  838. *
  839. * @type {Function[]}
  840. */
  841. _parsers = [
  842. stringTypeParser,
  843. booleanTypeParser,
  844. numberTypeParser,
  845. arrayTypeParser,
  846. objectTypeParser,
  847. defaultValueSetter
  848. ];
  849. /**
  850. * Get parsers.
  851. *
  852. * @returns {Function[]}
  853. */
  854. getParsers() {
  855. return [...this._parsers];
  856. }
  857. /**
  858. * Set parsers.
  859. *
  860. * @param {Function[]} list
  861. * @returns {this}
  862. */
  863. setParsers(list) {
  864. if (!Array.isArray(list)) {
  865. throw new import_js_format8.InvalidArgumentError(
  866. "Data parsers must be an Array, but %v was given.",
  867. list
  868. );
  869. }
  870. list.forEach((parser) => {
  871. if (typeof parser !== "function") {
  872. throw new import_js_format8.InvalidArgumentError(
  873. "Data parser must be a Function, but %v was given.",
  874. parser
  875. );
  876. }
  877. });
  878. this._parsers = [...list];
  879. return this;
  880. }
  881. /**
  882. * Define schema.
  883. *
  884. * @param {object} schemaDef
  885. * @returns {this}
  886. */
  887. defineSchema(schemaDef) {
  888. this.getService(DataSchemaRegistry).defineSchema(schemaDef);
  889. return this;
  890. }
  891. /**
  892. * Has schema.
  893. *
  894. * @param {string} schemaName
  895. * @returns {boolean}
  896. */
  897. hasSchema(schemaName) {
  898. return this.getService(DataSchemaRegistry).hasSchema(schemaName);
  899. }
  900. /**
  901. * Get schema.
  902. *
  903. * @param {string} schemaName
  904. * @returns {object}
  905. */
  906. getSchema(schemaName) {
  907. return this.getService(DataSchemaRegistry).getSchema(schemaName);
  908. }
  909. /**
  910. * Parse.
  911. *
  912. * @param {*} value
  913. * @param {object|Function|string} schema
  914. * @param {object} [options]
  915. * @returns {*}
  916. */
  917. parse(value, schema, options) {
  918. if (options !== void 0) {
  919. if (options === null || typeof options !== "object" || Array.isArray(options)) {
  920. throw new import_js_format8.InvalidArgumentError(
  921. "Parsing options must be an Object, but %v was given.",
  922. options
  923. );
  924. }
  925. if (options.sourcePath !== void 0) {
  926. if (!options.sourcePath || typeof options.sourcePath !== "string") {
  927. throw new import_js_format8.InvalidArgumentError(
  928. 'Option "sourcePath" must be a non-empty String, but %v was given.',
  929. options.sourcePath
  930. );
  931. }
  932. }
  933. if (options.shallowMode !== void 0) {
  934. if (typeof options.shallowMode !== "boolean") {
  935. throw new import_js_format8.InvalidArgumentError(
  936. 'Option "shallowMode" must be a Boolean, but %v was given.',
  937. options.shallowMode
  938. );
  939. }
  940. }
  941. if (options.noDefaultValues !== void 0) {
  942. if (typeof options.noDefaultValues !== "boolean") {
  943. throw new import_js_format8.InvalidArgumentError(
  944. 'Option "noDefaultValues" must be a Boolean, but %v was given.',
  945. options.noDefaultValues
  946. );
  947. }
  948. }
  949. if (options.noParsingErrors !== void 0) {
  950. if (typeof options.noParsingErrors !== "boolean") {
  951. throw new import_js_format8.InvalidArgumentError(
  952. 'Option "noParsingErrors" must be a Boolean, but %v was given.',
  953. options.noParsingErrors
  954. );
  955. }
  956. }
  957. if (options.keepUnknownProperties !== void 0) {
  958. if (typeof options.keepUnknownProperties !== "boolean") {
  959. throw new import_js_format8.InvalidArgumentError(
  960. 'Option "keepUnknownProperties" must be a Boolean, but %v was given.',
  961. options.keepUnknownProperties
  962. );
  963. }
  964. }
  965. }
  966. const sourcePath = options && options.sourcePath || void 0;
  967. const shallowMode = Boolean(options && options.shallowMode);
  968. const noParsingErrors = Boolean(options && options.noParsingErrors);
  969. const keepUnknownProperties = Boolean(
  970. options && options.keepUnknownProperties
  971. );
  972. validateDataSchema(schema, true);
  973. const schemaResolver = this.getService(DataSchemaResolver);
  974. if (typeof schema !== "object") {
  975. schema = schemaResolver.resolve(schema);
  976. }
  977. value = this._parsers.reduce((input, parser) => {
  978. return parser(input, schema, options, this.container);
  979. }, value);
  980. if (!shallowMode) {
  981. if (Array.isArray(value) && schema.items !== void 0) {
  982. value = [...value];
  983. value.forEach((item, index) => {
  984. const itemPath = (sourcePath || "array") + `[${index}]`;
  985. const itemOptions = { ...options, sourcePath: itemPath };
  986. value[index] = this.parse(item, schema.items, itemOptions);
  987. });
  988. } else if (value !== null && typeof value === "object" && schema.properties !== void 0) {
  989. let propsSchema = schema.properties;
  990. if (typeof propsSchema !== "object") {
  991. const resolvedSchema = schemaResolver.resolve(propsSchema);
  992. if (resolvedSchema.type !== DataType.OBJECT) {
  993. throw new import_js_format8.InvalidArgumentError(
  994. 'Unable to get the "properties" option from the data schema of %v type.',
  995. resolvedSchema.type || DataType.ANY
  996. );
  997. }
  998. propsSchema = resolvedSchema.properties || {};
  999. }
  1000. let newValue = keepUnknownProperties ? { ...value } : {};
  1001. Object.keys(propsSchema).forEach((propName) => {
  1002. const propSchema = propsSchema[propName];
  1003. if (propSchema === void 0) {
  1004. return;
  1005. }
  1006. const propValue = value[propName];
  1007. const propPath = sourcePath ? sourcePath + `.${propName}` : propName;
  1008. const propOptions = { ...options, sourcePath: propPath };
  1009. const newPropValue = this.parse(propValue, propSchema, propOptions);
  1010. if (propName in value || value[propName] !== newPropValue) {
  1011. newValue[propName] = newPropValue;
  1012. }
  1013. });
  1014. value = newValue;
  1015. }
  1016. }
  1017. if (!noParsingErrors) {
  1018. const validator = this.getService(DataValidator);
  1019. validator.validate(value, schema, { shallowMode: true, sourcePath });
  1020. }
  1021. return value;
  1022. }
  1023. };
  1024. // Annotate the CommonJS export names for ESM import in node:
  1025. 0 && (module.exports = {
  1026. DATA_TYPE_LIST,
  1027. DataParser,
  1028. DataParsingError,
  1029. DataSchemaRegistry,
  1030. DataSchemaResolver,
  1031. DataType,
  1032. DataValidationError,
  1033. DataValidator,
  1034. arrayTypeParser,
  1035. arrayTypeValidator,
  1036. booleanTypeParser,
  1037. booleanTypeValidator,
  1038. defaultValueSetter,
  1039. getDataTypeFromValue,
  1040. numberTypeParser,
  1041. numberTypeValidator,
  1042. objectTypeParser,
  1043. objectTypeValidator,
  1044. requiredValueValidator,
  1045. stringTypeParser,
  1046. stringTypeValidator,
  1047. validateDataSchema,
  1048. validateDataSchemaDefinition
  1049. });