index.cjs 34 KB

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