index.cjs 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  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_service16 = require("@e22m4u/js-service");
  71. // src/data-validator.js
  72. var import_js_service9 = 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/data-validators/array-type-validator.js
  304. var import_js_service3 = require("@e22m4u/js-service");
  305. // src/utils/to-pascal-case.js
  306. function toPascalCase(input) {
  307. if (!input) return "";
  308. 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, "");
  309. }
  310. __name(toPascalCase, "toPascalCase");
  311. // src/errors/data-parsing-error.js
  312. var import_js_format5 = require("@e22m4u/js-format");
  313. var DataParsingError = class extends import_js_format5.InvalidArgumentError {
  314. static {
  315. __name(this, "DataParsingError");
  316. }
  317. /**
  318. * Value.
  319. *
  320. * @type {*}
  321. */
  322. value;
  323. /**
  324. * Target type.
  325. *
  326. * @type {string}
  327. */
  328. targetType;
  329. /**
  330. * Source path.
  331. *
  332. * @type {string|undefined}
  333. */
  334. sourcePath;
  335. /**
  336. * Constructor.
  337. *
  338. * @param {*} value
  339. * @param {string} targetType
  340. * @param {string} [sourcePath]
  341. */
  342. constructor(value, targetType, sourcePath) {
  343. const targetTypePc = toPascalCase(targetType);
  344. let message = "";
  345. if (sourcePath) {
  346. message = (0, import_js_format5.format)(
  347. "Unable to parse %v from %v as %s.",
  348. value,
  349. sourcePath,
  350. targetTypePc
  351. );
  352. } else {
  353. message = (0, import_js_format5.format)("Unable to parse %v as %s.", value, targetTypePc);
  354. }
  355. super(message);
  356. this.value = value;
  357. this.targetType = targetType;
  358. this.sourcePath = sourcePath;
  359. }
  360. };
  361. // src/errors/data-validation-error.js
  362. var import_js_format6 = require("@e22m4u/js-format");
  363. var DataValidationError = class extends import_js_format6.InvalidArgumentError {
  364. static {
  365. __name(this, "DataValidationError");
  366. }
  367. };
  368. // src/data-validators/array-type-validator.js
  369. var import_js_empty_values = require("@e22m4u/js-empty-values");
  370. function arrayTypeValidator(value, schema, options, container) {
  371. if (schema.type !== DataType.ARRAY) {
  372. return;
  373. }
  374. const emptyValues = container.get(import_js_empty_values.EmptyValuesService);
  375. const dataType = schema.type || DataType.ANY;
  376. if (emptyValues.isEmptyOf(dataType, value)) {
  377. return;
  378. }
  379. if (Array.isArray(value)) {
  380. return;
  381. }
  382. const sourcePath = options && options.sourcePath;
  383. if (sourcePath) {
  384. throw new DataValidationError(
  385. "Value of %v must be an Array, but %v was given.",
  386. sourcePath,
  387. value
  388. );
  389. } else {
  390. throw new DataValidationError(
  391. "Value must be an Array, but %v was given.",
  392. value
  393. );
  394. }
  395. }
  396. __name(arrayTypeValidator, "arrayTypeValidator");
  397. // src/data-validators/object-type-validator.js
  398. var import_js_service4 = require("@e22m4u/js-service");
  399. var import_js_empty_values2 = require("@e22m4u/js-empty-values");
  400. function objectTypeValidator(value, schema, options, container) {
  401. if (schema.type !== DataType.OBJECT) {
  402. return;
  403. }
  404. const emptyValues = container.get(import_js_empty_values2.EmptyValuesService);
  405. const dataType = schema.type || DataType.ANY;
  406. if (emptyValues.isEmptyOf(dataType, value)) {
  407. return;
  408. }
  409. if (value !== null && typeof value === "object" && !Array.isArray(value)) {
  410. return;
  411. }
  412. const sourcePath = options && options.sourcePath;
  413. if (sourcePath) {
  414. throw new DataValidationError(
  415. "Value of %v must be an Object, but %v was given.",
  416. sourcePath,
  417. value
  418. );
  419. } else {
  420. throw new DataValidationError(
  421. "Value must be an Object, but %v was given.",
  422. value
  423. );
  424. }
  425. }
  426. __name(objectTypeValidator, "objectTypeValidator");
  427. // src/data-validators/string-type-validator.js
  428. var import_js_service5 = require("@e22m4u/js-service");
  429. var import_js_empty_values3 = require("@e22m4u/js-empty-values");
  430. function stringTypeValidator(value, schema, options, container) {
  431. if (schema.type !== DataType.STRING) {
  432. return;
  433. }
  434. const emptyValues = container.get(import_js_empty_values3.EmptyValuesService);
  435. const dataType = schema.type || DataType.ANY;
  436. if (emptyValues.isEmptyOf(dataType, value)) {
  437. return;
  438. }
  439. if (typeof value === "string") {
  440. return;
  441. }
  442. const sourcePath = options && options.sourcePath;
  443. if (sourcePath) {
  444. throw new DataValidationError(
  445. "Value of %v must be a String, but %v was given.",
  446. sourcePath,
  447. value
  448. );
  449. } else {
  450. throw new DataValidationError(
  451. "Value must be a String, but %v was given.",
  452. value
  453. );
  454. }
  455. }
  456. __name(stringTypeValidator, "stringTypeValidator");
  457. // src/data-validators/number-type-validator.js
  458. var import_js_service6 = require("@e22m4u/js-service");
  459. var import_js_empty_values4 = require("@e22m4u/js-empty-values");
  460. function numberTypeValidator(value, schema, options, container) {
  461. if (schema.type !== DataType.NUMBER) {
  462. return;
  463. }
  464. const emptyValues = container.get(import_js_empty_values4.EmptyValuesService);
  465. const dataType = schema.type || DataType.ANY;
  466. if (emptyValues.isEmptyOf(dataType, value)) {
  467. return;
  468. }
  469. if (typeof value === "number") {
  470. return;
  471. }
  472. const sourcePath = options && options.sourcePath;
  473. if (sourcePath) {
  474. throw new DataValidationError(
  475. "Value of %v must be a Number, but %v was given.",
  476. sourcePath,
  477. value
  478. );
  479. } else {
  480. throw new DataValidationError(
  481. "Value must be a Number, but %v was given.",
  482. value
  483. );
  484. }
  485. }
  486. __name(numberTypeValidator, "numberTypeValidator");
  487. // src/data-validators/boolean-type-validator.js
  488. var import_js_service7 = require("@e22m4u/js-service");
  489. var import_js_empty_values5 = require("@e22m4u/js-empty-values");
  490. function booleanTypeValidator(value, schema, options, container) {
  491. if (schema.type !== DataType.BOOLEAN) {
  492. return;
  493. }
  494. const emptyValues = container.get(import_js_empty_values5.EmptyValuesService);
  495. const dataType = schema.type || DataType.ANY;
  496. if (emptyValues.isEmptyOf(dataType, value)) {
  497. return;
  498. }
  499. if (typeof value === "boolean") {
  500. return;
  501. }
  502. const sourcePath = options && options.sourcePath;
  503. if (sourcePath) {
  504. throw new DataValidationError(
  505. "Value of %v must be a Boolean, but %v was given.",
  506. sourcePath,
  507. value
  508. );
  509. } else {
  510. throw new DataValidationError(
  511. "Value must be a Boolean, but %v was given.",
  512. value
  513. );
  514. }
  515. }
  516. __name(booleanTypeValidator, "booleanTypeValidator");
  517. // src/data-validators/required-value-validator.js
  518. var import_js_service8 = require("@e22m4u/js-service");
  519. var import_js_empty_values6 = require("@e22m4u/js-empty-values");
  520. function requiredValueValidator(value, schema, options, container) {
  521. if (schema.required !== true) {
  522. return;
  523. }
  524. const emptyValues = container.get(import_js_empty_values6.EmptyValuesService);
  525. const dataType = schema.type || DataType.ANY;
  526. if (!emptyValues.isEmptyOf(dataType, value)) {
  527. return;
  528. }
  529. const sourcePath = options && options.sourcePath;
  530. if (sourcePath) {
  531. throw new DataValidationError(
  532. "Value of %v is required, but %v was given.",
  533. sourcePath,
  534. value
  535. );
  536. } else {
  537. throw new DataValidationError(
  538. "Value is required, but %v was given.",
  539. value
  540. );
  541. }
  542. }
  543. __name(requiredValueValidator, "requiredValueValidator");
  544. // src/data-validator.js
  545. var DataValidator = class extends import_js_service9.Service {
  546. static {
  547. __name(this, "DataValidator");
  548. }
  549. /**
  550. * Validators.
  551. *
  552. * @type {Function[]}
  553. */
  554. _validators = [
  555. stringTypeValidator,
  556. booleanTypeValidator,
  557. numberTypeValidator,
  558. objectTypeValidator,
  559. arrayTypeValidator,
  560. requiredValueValidator
  561. ];
  562. /**
  563. * Get validators.
  564. *
  565. * @returns {Function[]}
  566. */
  567. getValidators() {
  568. return [...this._validators];
  569. }
  570. /**
  571. * Set validators.
  572. *
  573. * @param {Function[]} list
  574. * @returns {this}
  575. */
  576. setValidators(list) {
  577. if (!Array.isArray(list)) {
  578. throw new import_js_format7.InvalidArgumentError(
  579. "Data validators must be an Array, but %v was given.",
  580. list
  581. );
  582. }
  583. list.forEach((validator) => {
  584. if (typeof validator !== "function") {
  585. throw new import_js_format7.InvalidArgumentError(
  586. "Data validator must be a Function, but %v was given.",
  587. validator
  588. );
  589. }
  590. });
  591. this._validators = [...list];
  592. return this;
  593. }
  594. /**
  595. * Define schema.
  596. *
  597. * @param {object} schemaDef
  598. * @returns {this}
  599. */
  600. defineSchema(schemaDef) {
  601. this.getService(DataSchemaRegistry).defineSchema(schemaDef);
  602. return this;
  603. }
  604. /**
  605. * Has schema.
  606. *
  607. * @param {string} schemaName
  608. * @returns {boolean}
  609. */
  610. hasSchema(schemaName) {
  611. return this.getService(DataSchemaRegistry).hasSchema(schemaName);
  612. }
  613. /**
  614. * Get schema.
  615. *
  616. * @param {string} schemaName
  617. * @returns {object}
  618. */
  619. getSchema(schemaName) {
  620. return this.getService(DataSchemaRegistry).getSchema(schemaName);
  621. }
  622. /**
  623. * Validate.
  624. *
  625. * @param {*} value
  626. * @param {object|Function|string} schema
  627. * @param {object} [options]
  628. */
  629. validate(value, schema, options) {
  630. if (options !== void 0) {
  631. if (options === null || typeof options !== "object" || Array.isArray(options)) {
  632. throw new import_js_format7.InvalidArgumentError(
  633. "Validation options must be an Object, but %v was given.",
  634. options
  635. );
  636. }
  637. if (options.sourcePath !== void 0) {
  638. if (!options.sourcePath || typeof options.sourcePath !== "string") {
  639. throw new import_js_format7.InvalidArgumentError(
  640. 'Option "sourcePath" must be a non-empty String, but %v was given.',
  641. options.sourcePath
  642. );
  643. }
  644. }
  645. if (options.shallowMode !== void 0) {
  646. if (typeof options.shallowMode !== "boolean") {
  647. throw new import_js_format7.InvalidArgumentError(
  648. 'Option "shallowMode" must be a Boolean, but %v was given.',
  649. options.shallowMode
  650. );
  651. }
  652. }
  653. }
  654. const sourcePath = options && options.sourcePath || void 0;
  655. const shallowMode = Boolean(options && options.shallowMode);
  656. validateDataSchema(schema, true);
  657. const schemaResolver = this.getService(DataSchemaResolver);
  658. if (typeof schema !== "object") {
  659. schema = schemaResolver.resolve(schema);
  660. }
  661. this._validators.forEach((validate) => {
  662. validate(value, schema, options, this.container);
  663. });
  664. if (shallowMode) {
  665. return;
  666. }
  667. if (Array.isArray(value) && schema.items !== void 0) {
  668. value.forEach((item, index) => {
  669. const itemPath = (sourcePath || "array") + `[${index}]`;
  670. const itemOptions = { ...options, sourcePath: itemPath };
  671. this.validate(item, schema.items, itemOptions);
  672. });
  673. } else if (value !== null && typeof value === "object" && schema.properties !== void 0) {
  674. let propsSchema = schema.properties;
  675. if (typeof propsSchema !== "object") {
  676. const resolvedSchema = schemaResolver.resolve(propsSchema);
  677. if (resolvedSchema.type !== DataType.OBJECT) {
  678. throw new import_js_format7.InvalidArgumentError(
  679. 'Unable to get the "properties" option from the data schema of %v type.',
  680. resolvedSchema.type || DataType.ANY
  681. );
  682. }
  683. propsSchema = resolvedSchema.properties || {};
  684. }
  685. Object.keys(propsSchema).forEach((propName) => {
  686. const propSchema = propsSchema[propName];
  687. if (propSchema === void 0) {
  688. return;
  689. }
  690. const propValue = value[propName];
  691. const propPath = sourcePath ? sourcePath + `.${propName}` : propName;
  692. const propOptions = { ...options, sourcePath: propPath };
  693. this.validate(propValue, propSchema, propOptions);
  694. });
  695. }
  696. }
  697. };
  698. // src/data-parser.js
  699. var import_js_format8 = require("@e22m4u/js-format");
  700. // src/data-parsers/array-type-parser.js
  701. var import_js_service10 = require("@e22m4u/js-service");
  702. var import_js_empty_values7 = require("@e22m4u/js-empty-values");
  703. function arrayTypeParser(value, schema, options, container) {
  704. if (schema.type !== DataType.ARRAY) {
  705. return value;
  706. }
  707. if (Array.isArray(value)) {
  708. return value;
  709. }
  710. if (typeof value === "string") {
  711. value = value.trim();
  712. let newValue;
  713. try {
  714. newValue = JSON.parse(value);
  715. } catch {
  716. }
  717. if (Array.isArray(newValue)) {
  718. return newValue;
  719. }
  720. }
  721. const dataType = schema.type || DataType.ANY;
  722. const emptyValues = container.get(import_js_empty_values7.EmptyValuesService);
  723. if (emptyValues.isEmptyOf(dataType, value)) {
  724. return value;
  725. }
  726. if (!options || !options.noParsingErrors) {
  727. const sourcePath = options && options.sourcePath;
  728. throw new DataParsingError(value, dataType, sourcePath);
  729. }
  730. return value;
  731. }
  732. __name(arrayTypeParser, "arrayTypeParser");
  733. // src/data-parsers/string-type-parser.js
  734. var import_js_service11 = require("@e22m4u/js-service");
  735. var import_js_empty_values8 = require("@e22m4u/js-empty-values");
  736. function stringTypeParser(value, schema, options, container) {
  737. if (schema.type !== DataType.STRING) {
  738. return value;
  739. }
  740. if (typeof value === "string") {
  741. return value;
  742. }
  743. if (typeof value === "number") {
  744. return String(value);
  745. }
  746. const dataType = schema.type || DataType.ANY;
  747. const emptyValues = container.get(import_js_empty_values8.EmptyValuesService);
  748. if (emptyValues.isEmptyOf(dataType, value)) {
  749. return value;
  750. }
  751. if (!options || !options.noParsingErrors) {
  752. const sourcePath = options && options.sourcePath;
  753. throw new DataParsingError(value, dataType, sourcePath);
  754. }
  755. return value;
  756. }
  757. __name(stringTypeParser, "stringTypeParser");
  758. // src/data-parsers/number-type-parser.js
  759. var import_js_service12 = require("@e22m4u/js-service");
  760. var import_js_empty_values9 = require("@e22m4u/js-empty-values");
  761. function numberTypeParser(value, schema, options, container) {
  762. if (schema.type !== DataType.NUMBER) {
  763. return value;
  764. }
  765. if (typeof value === "number") {
  766. return value;
  767. }
  768. if (value && typeof value === "string") {
  769. if (value.length <= 20) {
  770. const newValue = Number(value.trim());
  771. if (!isNaN(newValue)) {
  772. return newValue;
  773. }
  774. }
  775. }
  776. const dataType = schema.type || DataType.ANY;
  777. const emptyValues = container.get(import_js_empty_values9.EmptyValuesService);
  778. if (emptyValues.isEmptyOf(dataType, value)) {
  779. return value;
  780. }
  781. if (!options || !options.noParsingErrors) {
  782. const sourcePath = options && options.sourcePath;
  783. throw new DataParsingError(value, dataType, sourcePath);
  784. }
  785. return value;
  786. }
  787. __name(numberTypeParser, "numberTypeParser");
  788. // src/data-parsers/object-type-parser.js
  789. var import_js_service13 = require("@e22m4u/js-service");
  790. var import_js_empty_values10 = require("@e22m4u/js-empty-values");
  791. function objectTypeParser(value, schema, options, container) {
  792. if (schema.type !== DataType.OBJECT) {
  793. return value;
  794. }
  795. if (value !== null && typeof value === "object" && !Array.isArray(value)) {
  796. return value;
  797. }
  798. if (typeof value === "string") {
  799. value = value.trim();
  800. let newValue;
  801. try {
  802. newValue = JSON.parse(value);
  803. } catch {
  804. }
  805. if (newValue !== null && typeof newValue === "object" && !Array.isArray(newValue)) {
  806. return newValue;
  807. }
  808. }
  809. const dataType = schema.type || DataType.ANY;
  810. const emptyValues = container.get(import_js_empty_values10.EmptyValuesService);
  811. if (emptyValues.isEmptyOf(dataType, value)) {
  812. return value;
  813. }
  814. if (!options || !options.noParsingErrors) {
  815. const sourcePath = options && options.sourcePath;
  816. throw new DataParsingError(value, dataType, sourcePath);
  817. }
  818. return value;
  819. }
  820. __name(objectTypeParser, "objectTypeParser");
  821. // src/data-parsers/boolean-type-parser.js
  822. var import_js_service14 = require("@e22m4u/js-service");
  823. var import_js_empty_values11 = require("@e22m4u/js-empty-values");
  824. function booleanTypeParser(value, schema, options, container) {
  825. if (schema.type !== DataType.BOOLEAN) {
  826. return value;
  827. }
  828. if (typeof value === "boolean") {
  829. return value;
  830. }
  831. if (typeof value === "string") {
  832. value = value.trim();
  833. if (value === "1") return true;
  834. if (value === "0") return false;
  835. if (value === "true") return true;
  836. if (value === "false") return false;
  837. } else if (typeof value === "number") {
  838. if (value === 1) return true;
  839. if (value === 0) return false;
  840. }
  841. const dataType = schema.type || DataType.ANY;
  842. const emptyValues = container.get(import_js_empty_values11.EmptyValuesService);
  843. if (emptyValues.isEmptyOf(dataType, value)) {
  844. return value;
  845. }
  846. if (!options || !options.noParsingErrors) {
  847. const sourcePath = options && options.sourcePath;
  848. throw new DataParsingError(value, dataType, sourcePath);
  849. }
  850. return value;
  851. }
  852. __name(booleanTypeParser, "booleanTypeParser");
  853. // src/data-parsers/default-value-setter.js
  854. var import_js_service15 = require("@e22m4u/js-service");
  855. var import_js_empty_values12 = require("@e22m4u/js-empty-values");
  856. function defaultValueSetter(value, schema, options, container) {
  857. if (options && options.noDefaultValues) {
  858. return value;
  859. }
  860. if (schema.default === void 0) {
  861. return value;
  862. }
  863. const emptyValues = container.get(import_js_empty_values12.EmptyValuesService);
  864. const dataType = schema.type || DataType.ANY;
  865. if (!emptyValues.isEmptyOf(dataType, value)) {
  866. return value;
  867. }
  868. if (typeof schema.default === "function") {
  869. return schema.default(container);
  870. }
  871. return schema.default && typeof schema.default === "object" ? structuredClone(schema.default) : schema.default;
  872. }
  873. __name(defaultValueSetter, "defaultValueSetter");
  874. // src/data-parser.js
  875. var DataParser = class extends import_js_service16.Service {
  876. static {
  877. __name(this, "DataParser");
  878. }
  879. /**
  880. * Parsers.
  881. *
  882. * @type {Function[]}
  883. */
  884. _parsers = [
  885. stringTypeParser,
  886. booleanTypeParser,
  887. numberTypeParser,
  888. arrayTypeParser,
  889. objectTypeParser,
  890. defaultValueSetter
  891. ];
  892. /**
  893. * Get parsers.
  894. *
  895. * @returns {Function[]}
  896. */
  897. getParsers() {
  898. return [...this._parsers];
  899. }
  900. /**
  901. * Set parsers.
  902. *
  903. * @param {Function[]} list
  904. * @returns {this}
  905. */
  906. setParsers(list) {
  907. if (!Array.isArray(list)) {
  908. throw new import_js_format8.InvalidArgumentError(
  909. "Data parsers must be an Array, but %v was given.",
  910. list
  911. );
  912. }
  913. list.forEach((parser) => {
  914. if (typeof parser !== "function") {
  915. throw new import_js_format8.InvalidArgumentError(
  916. "Data parser must be a Function, but %v was given.",
  917. parser
  918. );
  919. }
  920. });
  921. this._parsers = [...list];
  922. return this;
  923. }
  924. /**
  925. * Define schema.
  926. *
  927. * @param {object} schemaDef
  928. * @returns {this}
  929. */
  930. defineSchema(schemaDef) {
  931. this.getService(DataSchemaRegistry).defineSchema(schemaDef);
  932. return this;
  933. }
  934. /**
  935. * Has schema.
  936. *
  937. * @param {string} schemaName
  938. * @returns {boolean}
  939. */
  940. hasSchema(schemaName) {
  941. return this.getService(DataSchemaRegistry).hasSchema(schemaName);
  942. }
  943. /**
  944. * Get schema.
  945. *
  946. * @param {string} schemaName
  947. * @returns {object}
  948. */
  949. getSchema(schemaName) {
  950. return this.getService(DataSchemaRegistry).getSchema(schemaName);
  951. }
  952. /**
  953. * Parse.
  954. *
  955. * @param {*} value
  956. * @param {object|Function|string} schema
  957. * @param {object} [options]
  958. * @returns {*}
  959. */
  960. parse(value, schema, options) {
  961. if (options !== void 0) {
  962. if (options === null || typeof options !== "object" || Array.isArray(options)) {
  963. throw new import_js_format8.InvalidArgumentError(
  964. "Parsing options must be an Object, but %v was given.",
  965. options
  966. );
  967. }
  968. if (options.sourcePath !== void 0) {
  969. if (!options.sourcePath || typeof options.sourcePath !== "string") {
  970. throw new import_js_format8.InvalidArgumentError(
  971. 'Option "sourcePath" must be a non-empty String, but %v was given.',
  972. options.sourcePath
  973. );
  974. }
  975. }
  976. if (options.shallowMode !== void 0) {
  977. if (typeof options.shallowMode !== "boolean") {
  978. throw new import_js_format8.InvalidArgumentError(
  979. 'Option "shallowMode" must be a Boolean, but %v was given.',
  980. options.shallowMode
  981. );
  982. }
  983. }
  984. if (options.noDefaultValues !== void 0) {
  985. if (typeof options.noDefaultValues !== "boolean") {
  986. throw new import_js_format8.InvalidArgumentError(
  987. 'Option "noDefaultValues" must be a Boolean, but %v was given.',
  988. options.noDefaultValues
  989. );
  990. }
  991. }
  992. if (options.noParsingErrors !== void 0) {
  993. if (typeof options.noParsingErrors !== "boolean") {
  994. throw new import_js_format8.InvalidArgumentError(
  995. 'Option "noParsingErrors" must be a Boolean, but %v was given.',
  996. options.noParsingErrors
  997. );
  998. }
  999. }
  1000. if (options.keepUnknownProperties !== void 0) {
  1001. if (typeof options.keepUnknownProperties !== "boolean") {
  1002. throw new import_js_format8.InvalidArgumentError(
  1003. 'Option "keepUnknownProperties" must be a Boolean, but %v was given.',
  1004. options.keepUnknownProperties
  1005. );
  1006. }
  1007. }
  1008. }
  1009. const sourcePath = options && options.sourcePath || void 0;
  1010. const shallowMode = Boolean(options && options.shallowMode);
  1011. const noParsingErrors = Boolean(options && options.noParsingErrors);
  1012. const keepUnknownProperties = Boolean(
  1013. options && options.keepUnknownProperties
  1014. );
  1015. validateDataSchema(schema, true);
  1016. const schemaResolver = this.getService(DataSchemaResolver);
  1017. if (typeof schema !== "object") {
  1018. schema = schemaResolver.resolve(schema);
  1019. }
  1020. value = this._parsers.reduce((input, parser) => {
  1021. return parser(input, schema, options, this.container);
  1022. }, value);
  1023. if (!shallowMode) {
  1024. if (Array.isArray(value) && schema.items !== void 0) {
  1025. value = [...value];
  1026. value.forEach((item, index) => {
  1027. const itemPath = (sourcePath || "array") + `[${index}]`;
  1028. const itemOptions = { ...options, sourcePath: itemPath };
  1029. value[index] = this.parse(item, schema.items, itemOptions);
  1030. });
  1031. } else if (value !== null && typeof value === "object" && schema.properties !== void 0) {
  1032. let propsSchema = schema.properties;
  1033. if (typeof propsSchema !== "object") {
  1034. const resolvedSchema = schemaResolver.resolve(propsSchema);
  1035. if (resolvedSchema.type !== DataType.OBJECT) {
  1036. throw new import_js_format8.InvalidArgumentError(
  1037. 'Unable to get the "properties" option from the data schema of %v type.',
  1038. resolvedSchema.type || DataType.ANY
  1039. );
  1040. }
  1041. propsSchema = resolvedSchema.properties || {};
  1042. }
  1043. let newValue = keepUnknownProperties ? { ...value } : {};
  1044. Object.keys(propsSchema).forEach((propName) => {
  1045. const propSchema = propsSchema[propName];
  1046. if (propSchema === void 0) {
  1047. return;
  1048. }
  1049. const propValue = value[propName];
  1050. const propPath = sourcePath ? sourcePath + `.${propName}` : propName;
  1051. const propOptions = { ...options, sourcePath: propPath };
  1052. const newPropValue = this.parse(propValue, propSchema, propOptions);
  1053. if (propName in value || value[propName] !== newPropValue) {
  1054. newValue[propName] = newPropValue;
  1055. }
  1056. });
  1057. value = newValue;
  1058. }
  1059. }
  1060. if (!noParsingErrors) {
  1061. const validator = this.getService(DataValidator);
  1062. validator.validate(value, schema, { shallowMode: true, sourcePath });
  1063. }
  1064. return value;
  1065. }
  1066. };
  1067. // Annotate the CommonJS export names for ESM import in node:
  1068. 0 && (module.exports = {
  1069. DATA_TYPE_LIST,
  1070. DataParser,
  1071. DataParsingError,
  1072. DataSchemaRegistry,
  1073. DataSchemaResolver,
  1074. DataType,
  1075. DataValidationError,
  1076. DataValidator,
  1077. arrayTypeParser,
  1078. arrayTypeValidator,
  1079. booleanTypeParser,
  1080. booleanTypeValidator,
  1081. defaultValueSetter,
  1082. getDataTypeFromValue,
  1083. numberTypeParser,
  1084. numberTypeValidator,
  1085. objectTypeParser,
  1086. objectTypeValidator,
  1087. requiredValueValidator,
  1088. stringTypeParser,
  1089. stringTypeValidator,
  1090. validateDataSchema,
  1091. validateDataSchemaDefinition
  1092. });