@fgv/ts-utils 1.9.4 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (211) hide show
  1. package/CHANGELOG.json +26 -0
  2. package/CHANGELOG.md +14 -0
  3. package/dist/ts-utils.d.ts +3007 -0
  4. package/dist/tsdoc-metadata.json +11 -0
  5. package/lib/index.d.ts +12 -0
  6. package/lib/index.d.ts.map +1 -0
  7. package/lib/index.js +68 -0
  8. package/lib/index.js.map +1 -0
  9. package/lib/packlets/base/brand.d.ts.map +1 -0
  10. package/lib/packlets/base/brand.js +24 -0
  11. package/lib/packlets/base/brand.js.map +1 -0
  12. package/lib/packlets/base/index.d.ts +7 -0
  13. package/lib/packlets/base/index.d.ts.map +1 -0
  14. package/lib/packlets/base/index.js +58 -0
  15. package/lib/packlets/base/index.js.map +1 -0
  16. package/{logger.d.ts → lib/packlets/base/logger.d.ts} +16 -1
  17. package/lib/packlets/base/logger.d.ts.map +1 -0
  18. package/lib/packlets/base/logger.js +128 -0
  19. package/lib/packlets/base/logger.js.map +1 -0
  20. package/{normalize.d.ts → lib/packlets/base/normalize.d.ts} +6 -0
  21. package/lib/packlets/base/normalize.d.ts.map +1 -0
  22. package/lib/packlets/base/normalize.js +139 -0
  23. package/lib/packlets/base/normalize.js.map +1 -0
  24. package/{result.d.ts → lib/packlets/base/result.d.ts} +16 -15
  25. package/lib/packlets/base/result.d.ts.map +1 -0
  26. package/lib/packlets/base/result.js +571 -0
  27. package/lib/packlets/base/result.js.map +1 -0
  28. package/lib/packlets/base/utils.d.ts.map +1 -0
  29. package/lib/packlets/base/utils.js +200 -0
  30. package/lib/packlets/base/utils.js.map +1 -0
  31. package/{converter.d.ts → lib/packlets/conversion/converter.d.ts} +11 -12
  32. package/lib/packlets/conversion/converter.d.ts.map +1 -0
  33. package/lib/packlets/conversion/converter.js +218 -0
  34. package/lib/packlets/conversion/converter.js.map +1 -0
  35. package/{converters.d.ts → lib/packlets/conversion/converters.d.ts} +52 -216
  36. package/lib/packlets/conversion/converters.d.ts.map +1 -0
  37. package/lib/packlets/conversion/converters.js +739 -0
  38. package/lib/packlets/conversion/converters.js.map +1 -0
  39. package/lib/packlets/conversion/index.d.ts +6 -0
  40. package/lib/packlets/conversion/index.d.ts.map +1 -0
  41. package/lib/packlets/conversion/index.js +56 -0
  42. package/lib/packlets/conversion/index.js.map +1 -0
  43. package/lib/packlets/conversion/objectConverter.d.ts +94 -0
  44. package/lib/packlets/conversion/objectConverter.d.ts.map +1 -0
  45. package/lib/packlets/conversion/objectConverter.js +110 -0
  46. package/lib/packlets/conversion/objectConverter.js.map +1 -0
  47. package/lib/packlets/conversion/stringConverter.d.ts +78 -0
  48. package/lib/packlets/conversion/stringConverter.d.ts.map +1 -0
  49. package/lib/packlets/conversion/stringConverter.js +97 -0
  50. package/lib/packlets/conversion/stringConverter.js.map +1 -0
  51. package/{csvHelpers.d.ts → lib/packlets/csv/csvHelpers.d.ts} +1 -1
  52. package/lib/packlets/csv/csvHelpers.d.ts.map +1 -0
  53. package/lib/packlets/csv/csvHelpers.js +69 -0
  54. package/lib/packlets/csv/csvHelpers.js.map +1 -0
  55. package/lib/packlets/csv/index.d.ts +2 -0
  56. package/lib/packlets/csv/index.d.ts.map +1 -0
  57. package/lib/packlets/csv/index.js +39 -0
  58. package/lib/packlets/csv/index.js.map +1 -0
  59. package/{extendedArray.d.ts → lib/packlets/experimental/extendedArray.d.ts} +7 -7
  60. package/lib/packlets/experimental/extendedArray.d.ts.map +1 -0
  61. package/lib/packlets/experimental/extendedArray.js +106 -0
  62. package/lib/packlets/experimental/extendedArray.js.map +1 -0
  63. package/{formatter.d.ts → lib/packlets/experimental/formatter.d.ts} +6 -6
  64. package/lib/packlets/experimental/formatter.d.ts.map +1 -0
  65. package/lib/packlets/experimental/formatter.js +75 -0
  66. package/lib/packlets/experimental/formatter.js.map +1 -0
  67. package/lib/packlets/experimental/index.d.ts +4 -0
  68. package/lib/packlets/experimental/index.d.ts.map +1 -0
  69. package/lib/packlets/experimental/index.js +41 -0
  70. package/lib/packlets/experimental/index.js.map +1 -0
  71. package/{rangeOf.d.ts → lib/packlets/experimental/rangeOf.d.ts} +21 -24
  72. package/lib/packlets/experimental/rangeOf.d.ts.map +1 -0
  73. package/lib/packlets/experimental/rangeOf.js +185 -0
  74. package/lib/packlets/experimental/rangeOf.js.map +1 -0
  75. package/{hash.d.ts → lib/packlets/hash/hash.d.ts} +1 -1
  76. package/lib/packlets/hash/hash.d.ts.map +1 -0
  77. package/lib/packlets/hash/hash.js +168 -0
  78. package/lib/packlets/hash/hash.js.map +1 -0
  79. package/lib/packlets/hash/index.d.ts +2 -0
  80. package/lib/packlets/hash/index.d.ts.map +1 -0
  81. package/lib/packlets/hash/index.js +39 -0
  82. package/lib/packlets/hash/index.js.map +1 -0
  83. package/lib/packlets/record-jar/index.d.ts +2 -0
  84. package/lib/packlets/record-jar/index.d.ts.map +1 -0
  85. package/lib/packlets/record-jar/index.js +39 -0
  86. package/lib/packlets/record-jar/index.js.map +1 -0
  87. package/{recordJarHelpers.d.ts → lib/packlets/record-jar/recordJarHelpers.d.ts} +1 -1
  88. package/lib/packlets/record-jar/recordJarHelpers.d.ts.map +1 -0
  89. package/lib/packlets/record-jar/recordJarHelpers.js +263 -0
  90. package/lib/packlets/record-jar/recordJarHelpers.js.map +1 -0
  91. package/{validation → lib/packlets/validation}/array.d.ts +2 -2
  92. package/lib/packlets/validation/array.d.ts.map +1 -0
  93. package/lib/packlets/validation/array.js +67 -0
  94. package/lib/packlets/validation/array.js.map +1 -0
  95. package/{validation → lib/packlets/validation}/boolean.d.ts +1 -1
  96. package/lib/packlets/validation/boolean.d.ts.map +1 -0
  97. package/lib/packlets/validation/boolean.js +56 -0
  98. package/lib/packlets/validation/boolean.js.map +1 -0
  99. package/lib/packlets/validation/classes.d.ts.map +1 -0
  100. package/lib/packlets/validation/classes.js +38 -0
  101. package/lib/packlets/validation/classes.js.map +1 -0
  102. package/lib/packlets/validation/common.d.ts.map +1 -0
  103. package/lib/packlets/validation/common.js +24 -0
  104. package/lib/packlets/validation/common.js.map +1 -0
  105. package/{validation → lib/packlets/validation}/field.d.ts +1 -1
  106. package/lib/packlets/validation/field.d.ts.map +1 -0
  107. package/lib/packlets/validation/field.js +71 -0
  108. package/lib/packlets/validation/field.js.map +1 -0
  109. package/{validation → lib/packlets/validation}/genericValidator.d.ts +2 -3
  110. package/lib/packlets/validation/genericValidator.d.ts.map +1 -0
  111. package/lib/packlets/validation/genericValidator.js +137 -0
  112. package/lib/packlets/validation/genericValidator.js.map +1 -0
  113. package/lib/packlets/validation/index.d.ts.map +1 -0
  114. package/{validation → lib/packlets/validation}/index.js +1 -1
  115. package/lib/packlets/validation/index.js.map +1 -0
  116. package/{validation → lib/packlets/validation}/number.d.ts +1 -1
  117. package/lib/packlets/validation/number.d.ts.map +1 -0
  118. package/lib/packlets/validation/number.js +54 -0
  119. package/lib/packlets/validation/number.js.map +1 -0
  120. package/{validation → lib/packlets/validation}/object.d.ts +1 -1
  121. package/lib/packlets/validation/object.d.ts.map +1 -0
  122. package/lib/packlets/validation/object.js +139 -0
  123. package/lib/packlets/validation/object.js.map +1 -0
  124. package/{validation → lib/packlets/validation}/string.d.ts +1 -1
  125. package/lib/packlets/validation/string.d.ts.map +1 -0
  126. package/lib/packlets/validation/string.js +54 -0
  127. package/lib/packlets/validation/string.js.map +1 -0
  128. package/lib/packlets/validation/traits.d.ts.map +1 -0
  129. package/lib/packlets/validation/traits.js +58 -0
  130. package/lib/packlets/validation/traits.js.map +1 -0
  131. package/{validation → lib/packlets/validation}/typeGuard.d.ts +1 -1
  132. package/lib/packlets/validation/typeGuard.d.ts.map +1 -0
  133. package/lib/packlets/validation/typeGuard.js +62 -0
  134. package/lib/packlets/validation/typeGuard.js.map +1 -0
  135. package/{validation → lib/packlets/validation}/validator.d.ts +15 -16
  136. package/lib/packlets/validation/validator.d.ts.map +1 -0
  137. package/lib/packlets/validation/validator.js +24 -0
  138. package/lib/packlets/validation/validator.js.map +1 -0
  139. package/{validation → lib/packlets/validation}/validatorBase.d.ts +1 -1
  140. package/lib/packlets/validation/validatorBase.d.ts.map +1 -0
  141. package/lib/packlets/validation/validatorBase.js +41 -0
  142. package/lib/packlets/validation/validatorBase.js.map +1 -0
  143. package/{validation → lib/packlets/validation}/validators.d.ts +3 -6
  144. package/lib/packlets/validation/validators.d.ts.map +1 -0
  145. package/lib/packlets/validation/validators.js +121 -0
  146. package/lib/packlets/validation/validators.js.map +1 -0
  147. package/package.json +21 -20
  148. package/brand.d.ts.map +0 -1
  149. package/brand.js +0 -24
  150. package/converter.d.ts.map +0 -1
  151. package/converter.js +0 -222
  152. package/converters.d.ts.map +0 -1
  153. package/converters.js +0 -896
  154. package/csvHelpers.d.ts.map +0 -1
  155. package/csvHelpers.js +0 -69
  156. package/extendedArray.d.ts.map +0 -1
  157. package/extendedArray.js +0 -106
  158. package/formatter.d.ts.map +0 -1
  159. package/formatter.js +0 -75
  160. package/hash.d.ts.map +0 -1
  161. package/hash.js +0 -168
  162. package/index.d.ts +0 -15
  163. package/index.d.ts.map +0 -1
  164. package/index.js +0 -70
  165. package/logger.d.ts.map +0 -1
  166. package/logger.js +0 -115
  167. package/normalize.d.ts.map +0 -1
  168. package/normalize.js +0 -139
  169. package/rangeOf.d.ts.map +0 -1
  170. package/rangeOf.js +0 -185
  171. package/recordJarHelpers.d.ts.map +0 -1
  172. package/recordJarHelpers.js +0 -262
  173. package/result.d.ts.map +0 -1
  174. package/result.js +0 -569
  175. package/utils.d.ts.map +0 -1
  176. package/utils.js +0 -200
  177. package/validation/array.d.ts.map +0 -1
  178. package/validation/array.js +0 -67
  179. package/validation/boolean.d.ts.map +0 -1
  180. package/validation/boolean.js +0 -59
  181. package/validation/classes.d.ts.map +0 -1
  182. package/validation/classes.js +0 -38
  183. package/validation/common.d.ts.map +0 -1
  184. package/validation/common.js +0 -24
  185. package/validation/field.d.ts.map +0 -1
  186. package/validation/field.js +0 -72
  187. package/validation/genericValidator.d.ts.map +0 -1
  188. package/validation/genericValidator.js +0 -138
  189. package/validation/index.d.ts.map +0 -1
  190. package/validation/number.d.ts.map +0 -1
  191. package/validation/number.js +0 -57
  192. package/validation/object.d.ts.map +0 -1
  193. package/validation/object.js +0 -143
  194. package/validation/string.d.ts.map +0 -1
  195. package/validation/string.js +0 -57
  196. package/validation/traits.d.ts.map +0 -1
  197. package/validation/traits.js +0 -58
  198. package/validation/typeGuard.d.ts.map +0 -1
  199. package/validation/typeGuard.js +0 -62
  200. package/validation/validator.d.ts.map +0 -1
  201. package/validation/validator.js +0 -24
  202. package/validation/validatorBase.d.ts.map +0 -1
  203. package/validation/validatorBase.js +0 -44
  204. package/validation/validators.d.ts.map +0 -1
  205. package/validation/validators.js +0 -119
  206. /package/{brand.d.ts → lib/packlets/base/brand.d.ts} +0 -0
  207. /package/{utils.d.ts → lib/packlets/base/utils.d.ts} +0 -0
  208. /package/{validation → lib/packlets/validation}/classes.d.ts +0 -0
  209. /package/{validation → lib/packlets/validation}/common.d.ts +0 -0
  210. /package/{validation → lib/packlets/validation}/index.d.ts +0 -0
  211. /package/{validation → lib/packlets/validation}/traits.d.ts +0 -0
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2020 Erik Fortune
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in all
13
+ * copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ * SOFTWARE.
22
+ */
23
+ Object.defineProperty(exports, "__esModule", { value: true });
24
+ exports.FieldValidator = void 0;
25
+ const base_1 = require("../base");
26
+ const validatorBase_1 = require("./validatorBase");
27
+ /**
28
+ * An in-place {@link Validation.Validator | Validator} for properties
29
+ * an an object.
30
+ * @public
31
+ */
32
+ class FieldValidator extends validatorBase_1.ValidatorBase {
33
+ /**
34
+ * Constructs a new {@link Validation.FieldValidator | FieldValidator.}.
35
+ * @param fieldName - The name of the property that this validator should validate.
36
+ * @param fieldValidator - The {@link Validation.Validator | Validator} to be applied
37
+ * against the named property.
38
+ * @param options - Additional {@link Validation.FieldValidatorOptions | options} to be
39
+ * applied to this validation.
40
+ */
41
+ constructor(fieldName, fieldValidator, options) {
42
+ super({ options });
43
+ this.fieldName = fieldName;
44
+ this.fieldValidator = fieldValidator;
45
+ // istanbul ignore next
46
+ this._fieldOptions = options !== null && options !== void 0 ? options : {};
47
+ }
48
+ /**
49
+ * {@inheritdoc Validation.ValidatorBase._validate}
50
+ */
51
+ _validate(from, context) {
52
+ if (typeof from === 'object' && !Array.isArray(from) && from !== null) {
53
+ const optional = this._fieldOptions.optional === true;
54
+ if ((0, base_1.isKeyOf)(this.fieldName, from)) {
55
+ if (!optional || from[this.fieldName] !== undefined) {
56
+ const result = this.fieldValidator
57
+ .validate(from[this.fieldName], context)
58
+ .onFailure((message) => {
59
+ return (0, base_1.fail)(`${this.fieldName}: ${message}`);
60
+ });
61
+ return result.success ? true : result;
62
+ }
63
+ }
64
+ return optional ? true : (0, base_1.fail)(`"${this.fieldName}": Field not found in "${JSON.stringify(from)}`);
65
+ }
66
+ // istanbul ignore next -- defense in depth
67
+ return (0, base_1.fail)(`Cannot validate field '${this.fieldName}' from non-object "${JSON.stringify(from)}"`);
68
+ }
69
+ }
70
+ exports.FieldValidator = FieldValidator;
71
+ //# sourceMappingURL=field.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"field.js","sourceRoot":"","sources":["../../../src/packlets/validation/field.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,kCAAiD;AAEjD,mDAAgD;AAWhD;;;;GAIG;AACH,MAAa,cAAkC,SAAQ,6BAAoB;IAczE;;;;;;;OAOG;IACH,YACE,SAAiB,EACjB,cAAgC,EAChC,OAAmC;QAEnC,KAAK,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC;QACnB,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,cAAc,GAAG,cAAc,CAAC;QACrC,uBAAuB;QACvB,IAAI,CAAC,aAAa,GAAG,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,EAAE,CAAC;IACrC,CAAC;IAED;;OAEG;IACO,SAAS,CAAC,IAAa,EAAE,OAAY;QAC7C,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,IAAI,KAAK,IAAI,EAAE;YACrE,MAAM,QAAQ,GAAG,IAAI,CAAC,aAAa,CAAC,QAAQ,KAAK,IAAI,CAAC;YAEtD,IAAI,IAAA,cAAO,EAAC,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,EAAE;gBACjC,IAAI,CAAC,QAAQ,IAAI,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,SAAS,EAAE;oBACnD,MAAM,MAAM,GAAG,IAAI,CAAC,cAAc;yBAC/B,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,OAAO,CAAC;yBACvC,SAAS,CAAC,CAAC,OAAe,EAAE,EAAE;wBAC7B,OAAO,IAAA,WAAI,EAAC,GAAG,IAAI,CAAC,SAAS,KAAK,OAAO,EAAE,CAAC,CAAC;oBAC/C,CAAC,CAAC,CAAC;oBAEL,OAAO,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC;iBACvC;aACF;YAED,OAAO,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAA,WAAI,EAAC,IAAI,IAAI,CAAC,SAAS,0BAA0B,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;SACnG;QACD,2CAA2C;QAC3C,OAAO,IAAA,WAAI,EAAC,0BAA0B,IAAI,CAAC,SAAS,sBAAsB,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACrG,CAAC;CACF;AA1DD,wCA0DC","sourcesContent":["/*\n * Copyright (c) 2020 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { Failure, fail, isKeyOf } from '../base';\nimport { Validator, ValidatorOptions } from './validator';\nimport { ValidatorBase } from './validatorBase';\n\n/**\n * Parameters used to construct a {@link Validation.FieldValidator | FieldValidator}.\n * @public\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport interface FieldValidatorOptions<TC> extends ValidatorOptions<TC> {\n optional?: boolean;\n}\n\n/**\n * An in-place {@link Validation.Validator | Validator} for properties\n * an an object.\n * @public\n */\nexport class FieldValidator<T, TC = undefined> extends ValidatorBase<T, TC> {\n /**\n * The name of the property that this validator should validate.\n */\n public readonly fieldName: string;\n /**\n * The {@link Validation.Validator | Validator} to be applied against the named property.\n */\n public readonly fieldValidator: Validator<T, TC>;\n /**\n * @internal\n */\n protected readonly _fieldOptions: FieldValidatorOptions<TC>;\n\n /**\n * Constructs a new {@link Validation.FieldValidator | FieldValidator.}.\n * @param fieldName - The name of the property that this validator should validate.\n * @param fieldValidator - The {@link Validation.Validator | Validator} to be applied\n * against the named property.\n * @param options - Additional {@link Validation.FieldValidatorOptions | options} to be\n * applied to this validation.\n */\n public constructor(\n fieldName: string,\n fieldValidator: Validator<T, TC>,\n options?: FieldValidatorOptions<TC>\n ) {\n super({ options });\n this.fieldName = fieldName;\n this.fieldValidator = fieldValidator;\n // istanbul ignore next\n this._fieldOptions = options ?? {};\n }\n\n /**\n * {@inheritdoc Validation.ValidatorBase._validate}\n */\n protected _validate(from: unknown, context?: TC): boolean | Failure<T> {\n if (typeof from === 'object' && !Array.isArray(from) && from !== null) {\n const optional = this._fieldOptions.optional === true;\n\n if (isKeyOf(this.fieldName, from)) {\n if (!optional || from[this.fieldName] !== undefined) {\n const result = this.fieldValidator\n .validate(from[this.fieldName], context)\n .onFailure((message: string) => {\n return fail(`${this.fieldName}: ${message}`);\n });\n\n return result.success ? true : result;\n }\n }\n\n return optional ? true : fail(`\"${this.fieldName}\": Field not found in \"${JSON.stringify(from)}`);\n }\n // istanbul ignore next -- defense in depth\n return fail(`Cannot validate field '${this.fieldName}' from non-object \"${JSON.stringify(from)}\"`);\n }\n}\n"]}
@@ -1,7 +1,6 @@
1
- import { Constraint, Validator, ValidatorOptions } from './validator';
1
+ import { Brand, Failure, Result } from '../base';
2
2
  import { ConstraintTrait, ValidatorTraits } from './traits';
3
- import { Failure, Result } from '../result';
4
- import { Brand } from '../brand';
3
+ import { Constraint, Validator, ValidatorOptions } from './validator';
5
4
  /**
6
5
  * Type for a validation function, which validates that a supplied `unknown`
7
6
  * value is a valid value of type `<T>`, possibly as influenced by
@@ -0,0 +1 @@
1
+ {"version":3,"file":"genericValidator.d.ts","sourceRoot":"","sources":["../../../src/packlets/validation/genericValidator.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,EAAiB,MAAM,SAAS,CAAC;AAChE,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAC5D,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEtE;;;;;GAKG;AACH,MAAM,MAAM,aAAa,CAAC,CAAC,EAAE,EAAE,IAAI,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,EAAE,KAAK,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;AAEzF;;;GAGG;AAEH,MAAM,WAAW,iCAAiC,CAAC,CAAC,EAAE,EAAE;IACtD,OAAO,CAAC,EAAE,gBAAgB,CAAC,EAAE,CAAC,CAAC;IAC/B,MAAM,CAAC,EAAE,OAAO,CAAC,eAAe,CAAC,CAAC;IAClC,SAAS,CAAC,EAAE,aAAa,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;CAClC;AAED;;;GAGG;AACH,qBAAa,gBAAgB,CAAC,CAAC,EAAE,EAAE,GAAG,SAAS,CAAE,YAAW,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC;IAC1E;;OAEG;IACH,SAAgB,MAAM,EAAE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 "use strict";
2
+ /*
3
+ * Copyright (c) 2021 Erik Fortune
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in all
13
+ * copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ * SOFTWARE.
22
+ */
23
+ Object.defineProperty(exports, "__esModule", { value: true });
24
+ exports.GenericValidator = void 0;
25
+ const base_1 = require("../base");
26
+ const traits_1 = require("./traits");
27
+ /**
28
+ * Generic base implementation for an in-place {@link Validation.Validator | Validator}.
29
+ * @public
30
+ */
31
+ class GenericValidator {
32
+ /**
33
+ * Constructs a new {@link Validation.Base.GenericValidator | GenericValidator<T>}.
34
+ * @param params - The {@link Validation.Base.GenericValidatorConstructorParams | constructor params}
35
+ * used to configure validation.
36
+ */
37
+ constructor(params) {
38
+ var _a;
39
+ if (!params.validator) {
40
+ throw new Error('No validator function supplied');
41
+ }
42
+ this._validator = params.validator;
43
+ this._options = (_a = params.options) !== null && _a !== void 0 ? _a : {};
44
+ this.traits = new traits_1.ValidatorTraits(params.traits);
45
+ }
46
+ /**
47
+ * {@inheritdoc Validation.Validator.isOptional}
48
+ */
49
+ get isOptional() {
50
+ return this.traits.isOptional;
51
+ }
52
+ /**
53
+ * {@inheritdoc Validation.Validator.brand}
54
+ */
55
+ get brand() {
56
+ return this.traits.brand;
57
+ }
58
+ /**
59
+ * {@inheritdoc Validation.Validator.validate}
60
+ */
61
+ validate(from, context) {
62
+ const result = this._validator(from, this._context(context));
63
+ if (typeof result === 'boolean') {
64
+ return result ? (0, base_1.succeed)(from) : (0, base_1.fail)('Invalid value');
65
+ }
66
+ return result;
67
+ }
68
+ /**
69
+ * {@inheritdoc Validation.Validator.validateOptional}
70
+ */
71
+ validateOptional(from, context) {
72
+ return from === undefined ? (0, base_1.succeed)(undefined) : this.validate(from, context);
73
+ }
74
+ /**
75
+ * {@inheritdoc Validation.Validator.guard}
76
+ */
77
+ guard(from, context) {
78
+ return this._validator(from, this._context(context)) === true;
79
+ }
80
+ /**
81
+ * {@inheritdoc Validation.Validator.optional}
82
+ */
83
+ optional() {
84
+ return new GenericValidator({
85
+ validator: (from, context) => {
86
+ return from === undefined || this._validator(from, this._context(context));
87
+ },
88
+ traits: { isOptional: true }
89
+ });
90
+ }
91
+ /**
92
+ * {@inheritdoc Validation.Validator.withConstraint}
93
+ */
94
+ withConstraint(constraint, trait) {
95
+ trait = trait !== null && trait !== void 0 ? trait : { type: 'function' };
96
+ return new GenericValidator({
97
+ validator: (from, context) => {
98
+ if (this._validator(from, this._context(context)) === true) {
99
+ const constraintResult = constraint(from);
100
+ if (typeof constraintResult === 'boolean') {
101
+ return constraintResult
102
+ ? true
103
+ : (0, base_1.fail)(`Invalid value "${JSON.stringify(from)}": does not meet constraint.`);
104
+ }
105
+ return constraintResult;
106
+ }
107
+ return false;
108
+ },
109
+ traits: { constraints: [trait] }
110
+ });
111
+ }
112
+ /**
113
+ * {@inheritdoc Validation.Validator.brand}
114
+ */
115
+ withBrand(brand) {
116
+ if (this.brand) {
117
+ throw new Error(`Cannot replace existing brand "${this.brand}" with "${brand}".`);
118
+ }
119
+ return new GenericValidator({
120
+ validator: (from, context) => {
121
+ return this._validator(from, this._context(context));
122
+ },
123
+ traits: { brand }
124
+ });
125
+ }
126
+ /**
127
+ * Gets a default or explicit context.
128
+ * @param explicitContext - Optional explicit context.
129
+ * @returns The appropriate context to use.
130
+ * @internal
131
+ */
132
+ _context(explicitContext) {
133
+ return explicitContext !== null && explicitContext !== void 0 ? explicitContext : this._options.defaultContext;
134
+ }
135
+ }
136
+ exports.GenericValidator = GenericValidator;
137
+ //# sourceMappingURL=genericValidator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"genericValidator.js","sourceRoot":"","sources":["../../../src/packlets/validation/genericValidator.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,kCAAgE;AAChE,qCAA4D;AAsB5D;;;GAGG;AACH,MAAa,gBAAgB;IAe3B;;;;OAIG;IACH,YAAmB,MAAyD;;QAC1E,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE;YACrB,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;SACnD;QACD,IAAI,CAAC,UAAU,GAAG,MAAM,CAAC,SAAS,CAAC;QACnC,IAAI,CAAC,QAAQ,GAAG,MAAA,MAAM,CAAC,OAAO,mCAAI,EAAE,CAAC;QACrC,IAAI,CAAC,MAAM,GAAG,IAAI,wBAAe,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACnD,CAAC;IAED;;OAEG;IACH,IAAW,UAAU;QACnB,OAAO,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC;IAChC,CAAC;IAED;;OAEG;IACH,IAAW,KAAK;QACd,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC;IAC3B,CAAC;IAED;;OAEG;IACI,QAAQ,CAAC,IAAa,EAAE,OAAY;QACzC,MAAM,MAAM,GAAG,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,OAAO,C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,OAAO,CAAC,CAAC,KAAK,IAAI,EAAE;oBAC1D,MAAM,gBAAgB,GAAG,UAAU,CAAC,IAAS,CAAC,CAAC;oBAC/C,IAAI,OAAO,gBAAgB,KAAK,SAAS,EAAE;wBACzC,OAAO,gBAAgB;4BACrB,CAAC,CAAC,IAAI;4BACN,CAAC,CAAC,IAAA,WAAI,EAAC,kBAAkB,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,+BAA+B,CAAC,CAAC;qBACjF;oBACD,OAAO,gBAAgB,CAAC;iBACzB;gBACD,OAAO,KAAK,CAAC;YACf,CAAC;YACD,MAAM,EAAE,EAAE,WAAW,EAAE,CAAC,KAAK,CAAC,EAAE;SACjC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACI,SAAS,CAAmB,KAAQ;QACzC,IAAI,IAAI,CAAC,KAAK,EAAE;YACd,MAAM,IAAI,KAAK,CAAC,kCAAkC,IAAI,CAAC,KAAK,WAAW,KAAK,IAAI,CAAC,CAAC;SACnF;QAED,OAAO,IAAI,gBAAgB,CAAkB;YAC3C,SAAS,EAAE,CAAC,IAAa,EAAE,OAAY,EAAE,EAAE;gBACzC,OAAO,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAmC,CAAC;YACzF,CAAC;YACD,MAAM,EAAE,EAAE,KAAK,EAAE;SAClB,CAAC,CAAC;IACL,CAAC;IAED;;;;;OAKG;IACO,QAAQ,CAAC,eAAoB;QACrC,OAAO,eAAe,aAAf,eAAe,cAAf,eAAe,GAAI,IAAI,CAAC,QAAQ,CAAC,cAAc,CAAC;IACzD,CAAC;CACF;AA/HD,4CA+HC","sourcesContent":["/*\n * Copyright (c) 2021 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { Brand, Failure, Result, fail, succeed } from '../base';\nimport { ConstraintTrait, ValidatorTraits } from './traits';\nimport { Constraint, Validator, ValidatorOptions } from './validator';\n\n/**\n * Type for a validation function, which validates that a supplied `unknown`\n * value is a valid value of type `<T>`, possibly as influenced by\n * an optionally-supplied validation context of type `<TC>`.\n * @public\n */\nexport type ValidatorFunc<T, TC> = (from: unknown, context?: TC) => boolean | Failure<T>;\n\n/**\n * Options used to initialize a {@link Validation.Base.GenericValidator | GenericValidator}.\n * @public\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport interface GenericValidatorConstructorParams<T, TC> {\n options?: ValidatorOptions<TC>;\n traits?: Partial<ValidatorTraits>;\n validator?: ValidatorFunc<T, TC>;\n}\n\n/**\n * Generic base implementation for an in-place {@link Validation.Validator | Validator}.\n * @public\n */\nexport class GenericValidator<T, TC = undefined> implements Validator<T, TC> {\n /**\n * {@inheritdoc Validation.Validator.traits}\n */\n public readonly traits: ValidatorTraits;\n\n /**\n * @internal\n */\n protected readonly _validator: ValidatorFunc<T, TC>;\n /**\n * @internal\n */\n protected readonly _options: ValidatorOptions<TC>;\n\n /**\n * Constructs a new {@link Validation.Base.GenericValidator | GenericValidator<T>}.\n * @param params - The {@link Validation.Base.GenericValidatorConstructorParams | constructor params}\n * used to configure validation.\n */\n public constructor(params: Partial<GenericValidatorConstructorParams<T, TC>>) {\n if (!params.validator) {\n throw new Error('No validator function supplied');\n }\n this._validator = params.validator;\n this._options = params.options ?? {};\n this.traits = new ValidatorTraits(params.traits);\n }\n\n /**\n * {@inheritdoc Validation.Validator.isOptional}\n */\n public get isOptional(): boolean {\n return this.traits.isOptional;\n }\n\n /**\n * {@inheritdoc Validation.Validator.brand}\n */\n public get brand(): string | undefined {\n return this.traits.brand;\n }\n\n /**\n * {@inheritdoc Validation.Validator.validate}\n */\n public validate(from: unknown, context?: TC): Result<T> {\n const result = this._validator(from, this._context(context));\n if (typeof result === 'boolean') {\n return result ? succeed(from as T) : fail<T>('Invalid value');\n }\n return result;\n }\n\n /**\n * {@inheritdoc Validation.Validator.validateOptional}\n */\n public validateOptional(from: unknown, context?: TC): Result<T | undefined> {\n return from === undefined ? succeed(undefined) : this.validate(from, context);\n }\n\n /**\n * {@inheritdoc Validation.Validator.guard}\n */\n public guard(from: unknown, context?: TC): from is T {\n return this._validator(from, this._context(context)) === true;\n }\n\n /**\n * {@inheritdoc Validation.Validator.optional}\n */\n public optional(): Validator<T | undefined, TC> {\n return new GenericValidator({\n validator: (from: unknown, context?: TC) => {\n return from === undefined || this._validator(from, this._context(context));\n },\n traits: { isOptional: true }\n });\n }\n\n /**\n * {@inheritdoc Validation.Validator.withConstraint}\n */\n public withConstraint(constraint: Constraint<T>, trait?: ConstraintTrait): Validator<T, TC> {\n trait = trait ?? { type: 'function' };\n return new GenericValidator({\n validator: (from: unknown, context?: TC): boolean | Failure<T> => {\n if (this._validator(from, this._context(context)) === true) {\n const constraintResult = constraint(from as T);\n if (typeof constraintResult === 'boolean') {\n return constraintResult\n ? true\n : fail(`Invalid value \"${JSON.stringify(from)}\": does not meet constraint.`);\n }\n return constraintResult;\n }\n return false;\n },\n traits: { constraints: [trait] }\n });\n }\n\n /**\n * {@inheritdoc Validation.Validator.brand}\n */\n public withBrand<B extends string>(brand: B): Validator<Brand<T, B>, TC> {\n if (this.brand) {\n throw new Error(`Cannot replace existing brand \"${this.brand}\" with \"${brand}\".`);\n }\n\n return new GenericValidator<Brand<T, B>, TC>({\n validator: (from: unknown, context?: TC) => {\n return this._validator(from, this._context(context)) as boolean | Failure<Brand<T, B>>;\n },\n traits: { brand }\n });\n }\n\n /**\n * Gets a default or explicit context.\n * @param explicitContext - Optional explicit context.\n * @returns The appropriate context to use.\n * @internal\n */\n protected _context(explicitContext?: TC): TC | undefined {\n return explicitContext ?? this._options.defaultContext;\n }\n}\n"]}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/packlets/validation/index.ts"],"names":[],"mappings":"AAsBA,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAE5B,OAAO,KAAK,IAAI,MAAM,oBAAoB,CAAC;AAC3C,OAAO,KAAK,OAAO,MAAM,WAAW,CAAC;AACrC,OAAO,KAAK,UAAU,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,UAAU,EAAE,CAAC"}
@@ -57,4 +57,4 @@ const Classes = __importStar(require("./classes"));
57
57
  exports.Classes = Classes;
58
58
  const Validators = __importStar(require("./validators"));
59
59
  exports.Validators = Validators;
60
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvdmFsaWRhdGlvbi9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUE7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7O0dBb0JHOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OztBQUVILDJDQUF5QjtBQUN6QiwyQ0FBeUI7QUFDekIsOENBQTRCO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kgS0lORCwgRVhQUkVTUyBPUlxuICogSU1QTElFRCwgSU5DTFVESU5HIEJVVCBOT1QgTElNSVRFRCBUTyBUSEUgV0FSUkFOVElFUyBPRiBNRVJDSEFOVEFCSUxJVFksXG4gKiBGSVRORVNTIEZPUiBBIFBBUlRJQ1VMQVIgUFVSUE9TRSBBTkQgTk9OSU5GUklOR0VNRU5ULiBJTiBOTyBFVkVOVCBTSEFMTCBUSEVcbiAqIEFVVEhPUlMgT1IgQ09QWVJJR0hUIEhPTERFUlMgQkUgTElBQkxFIEZPUiBBTlkgQ0xBSU0sIERBTUFHRVMgT1IgT1RIRVJcbiAqIExJQUJJTElUWSwgV0hFVEhFUiBJTiBBTiBBQ1RJT04gT0YgQ09OVFJBQ1QsIFRPUlQgT1IgT1RIRVJXSVNFLCBBUklTSU5HIEZST00sXG4gKiBPVVQgT0YgT1IgSU4gQ09OTkVDVElPTiBXSVRIIFRIRSBTT0ZUV0FSRSBPUiBUSEUgVVNFIE9SIE9USEVSIERFQUxJTkdTIElOIFRIRVxuICogU09GVFdBUkUuXG4gKi9cblxuZXhwb3J0ICogZnJvbSAnLi9jb21tb24nO1xuZXhwb3J0ICogZnJvbSAnLi90cmFpdHMnO1xuZXhwb3J0ICogZnJvbSAnLi92YWxpZGF0b3InO1xuXG5pbXBvcnQgKiBhcyBCYXNlIGZyb20gJy4vZ2VuZXJpY1ZhbGlkYXRvcic7XG5pbXBvcnQgKiBhcyBDbGFzc2VzIGZyb20gJy4vY2xhc3Nlcyc7XG5pbXBvcnQgKiBhcyBWYWxpZGF0b3JzIGZyb20gJy4vdmFsaWRhdG9ycyc7XG5leHBvcnQgeyBCYXNlLCBDbGFzc2VzLCBWYWxpZGF0b3JzIH07XG4iXX0=
60
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/packlets/validation/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,2CAAyB;AACzB,2CAAyB;AACzB,8CAA4B;AAE5B,yDAA2C;AAGlC,oBAAI;AAFb,mDAAqC;AAEtB,0BAAO;AADtB,yDAA2C;AACnB,gCAAU","sourcesContent":["/*\n * Copyright (c) 2021 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nexport * from './common';\nexport * from './traits';\nexport * from './validator';\n\nimport * as Base from './genericValidator';\nimport * as Classes from './classes';\nimport * as Validators from './validators';\nexport { Base, Classes, Validators };\n"]}
@@ -1,4 +1,4 @@
1
- import { Failure } from '../result';
1
+ import { Failure } from '../base';
2
2
  import { GenericValidator, GenericValidatorConstructorParams } from './genericValidator';
3
3
  /**
4
4
  * Parameters used to construct a {@link Validation.Classes.NumberValidator | NumberValidator}.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"number.d.ts","sourceRoot":"","sources":["../../../src/packlets/validation/number.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,OAAO,EAAQ,MAAM,SAAS,CAAC;AACxC,OAAO,EAAE,gBAAgB,EAAE,iCAAiC,EAAE,MAAM,oBAAoB,CAAC;AAEzF;;;GAGG;AACH,MAAM,MAAM,gCAAgC,CAC1C,CAAC,SAAS,MAAM,GAAG,MAAM,EACzB,EAAE,GAAG,OAAO,IACV,iCAAiC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;AAE7C;;;GAGG;AACH,qBAAa,eAAe,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,EAAE,EAAE,GAAG,OAAO,CAAE,SAAQ,gBAAgB,CAAC,CAAC,EAAE,EAAE,CAAC;IACnG;;;;OAIG;gBACgB,MAAM,CAAC,EAAE,gCAAgC,CAAC,CAAC,EAAE,EAAE,CAAC;IAOnE;;;;;OAKG;WACW,cAAc,CAAC,CAAC,SAAS,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC;CAMpF"}
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2021 Erik Fortune
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in all
13
+ * copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ * SOFTWARE.
22
+ */
23
+ Object.defineProperty(exports, "__esModule", { value: true });
24
+ exports.NumberValidator = void 0;
25
+ const base_1 = require("../base");
26
+ const genericValidator_1 = require("./genericValidator");
27
+ /**
28
+ * An in-place {@link Validation.Validator | Validator} for `number` values.
29
+ * @public
30
+ */
31
+ class NumberValidator extends genericValidator_1.GenericValidator {
32
+ /**
33
+ * Constructs a new {@link Validation.Classes.NumberValidator | NumberValidator}.
34
+ * @param params - Optional {@link Validation.Classes.NumberValidatorConstructorParams | init params} for the
35
+ * new {@link Validation.Classes.NumberValidator | NumberValidator}.
36
+ */
37
+ constructor(params) {
38
+ super(Object.assign({ validator: NumberValidator.validateNumber }, (params !== null && params !== void 0 ? params : {})));
39
+ }
40
+ /**
41
+ * Static method which validates that a supplied `unknown` value is a `number`.
42
+ * @param from - The `unknown` value to be tested.
43
+ * @returns Returns `true` if `from` is a `number`, or {@link Failure} with an error
44
+ * message if not.
45
+ */
46
+ static validateNumber(from) {
47
+ if (typeof from === 'number') {
48
+ return true;
49
+ }
50
+ return (0, base_1.fail)(`"${from}": not a number`);
51
+ }
52
+ }
53
+ exports.NumberValidator = NumberValidator;
54
+ //# sourceMappingURL=number.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"number.js","sourceRoot":"","sources":["../../../src/packlets/validation/number.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,kCAAwC;AACxC,yDAAyF;AAWzF;;;GAGG;AACH,MAAa,eAAyD,SAAQ,mCAAuB;IACnG;;;;OAIG;IACH,YAAmB,MAAgD;QACjE,KAAK,iBACH,SAAS,EAAE,eAAe,CAAC,cAAc,IACtC,CAAC,MAAM,aAAN,MAAM,cAAN,MAAM,GAAI,EAAE,CAAC,EACjB,CAAC;IACL,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,cAAc,CAAmB,IAAa;QAC1D,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE;YAC5B,OAAO,IAAI,CAAC;SACb;QACD,OAAO,IAAA,WAAI,EAAI,IAAI,IAAI,iBAAiB,CAAC,CAAC;IAC5C,CAAC;CACF;AAzBD,0CAyBC","sourcesContent":["/*\n * Copyright (c) 2021 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { Failure, fail } from '../base';\nimport { GenericValidator, GenericValidatorConstructorParams } from './genericValidator';\n\n/**\n * Parameters used to construct a {@link Validation.Classes.NumberValidator | NumberValidator}.\n * @public\n */\nexport type NumberValidatorConstructorParams<\n T extends number = number,\n TC = unknown\n> = GenericValidatorConstructorParams<T, TC>;\n\n/**\n * An in-place {@link Validation.Validator | Validator} for `number` values.\n * @public\n */\nexport class NumberValidator<T extends number = number, TC = unknown> extends GenericValidator<T, TC> {\n /**\n * Constructs a new {@link Validation.Classes.NumberValidator | NumberValidator}.\n * @param params - Optional {@link Validation.Classes.NumberValidatorConstructorParams | init params} for the\n * new {@link Validation.Classes.NumberValidator | NumberValidator}.\n */\n public constructor(params?: NumberValidatorConstructorParams<T, TC>) {\n super({\n validator: NumberValidator.validateNumber,\n ...(params ?? {})\n });\n }\n\n /**\n * Static method which validates that a supplied `unknown` value is a `number`.\n * @param from - The `unknown` value to be tested.\n * @returns Returns `true` if `from` is a `number`, or {@link Failure} with an error\n * message if not.\n */\n public static validateNumber<T extends number>(from: unknown): boolean | Failure<T> {\n if (typeof from === 'number') {\n return true;\n }\n return fail<T>(`\"${from}\": not a number`);\n }\n}\n"]}
@@ -1,4 +1,4 @@
1
- import { Failure } from '../result';
1
+ import { Failure } from '../base';
2
2
  import { Validator, ValidatorOptions } from './validator';
3
3
  import { ValidatorBase, ValidatorBaseConstructorParams } from './validatorBase';
4
4
  /**
@@ -0,0 +1 @@
1
+ {"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../../../src/packlets/validation/object.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,OAAO,EAAQ,MAAM,SAAS,CAAC;AACxC,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC1D,OAAO,EAAE,aAAa,EAAE,8BAA8B,EAAE,MAAM,iBAAiB,CAAC;AAIhF;;;GAGG;AACH,MAAM,MAAM,eAAe,CAAC,CAAC,EAAE,EAAE,GAAG,OAAO,IAAI;KAAG,GAAG,IAAI,MAAM,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC;CAAE,CAAC;AAE3F;;;GAGG;AAEH,MAAM,WAAW,sBAAsB,CAAC,CAAC,EAAE,EAAE,CAAE,SAAQ,gBAAgB,CAAC,EAAE,CAAC;IACzE;;OAEG;IACH,cAAc,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC;IAC7B;;;OAGG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;CA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,UAAU,CAAC,iBAAiB,EAAE,CAAC,MAAM,CAAC,CAAC,EAAE,GAAG,eAAe,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC;IAMlF;;;OAGG;IACH,SAAS,CAAC,SAAS,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,EAAE,GAAG,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC;CA0BvE"}
@@ -0,0 +1,139 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2021 Erik Fortune
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in all
13
+ * copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ * SOFTWARE.
22
+ */
23
+ Object.defineProperty(exports, "__esModule", { value: true });
24
+ exports.ObjectValidator = void 0;
25
+ const base_1 = require("../base");
26
+ const validatorBase_1 = require("./validatorBase");
27
+ const field_1 = require("./field");
28
+ /**
29
+ * In-place {@link Validation.Validator | Validator} for an object of type `<T>`.
30
+ * @remarks
31
+ * By default, succeeds if all of the required fields exist and are validate, and fails if
32
+ * any required fields do not exist or are invalid. See {@link Validation.Classes.ObjectValidatorOptions}
33
+ * for other validation options.
34
+ * @public
35
+ */
36
+ class ObjectValidator extends validatorBase_1.ValidatorBase {
37
+ /**
38
+ * Constructs a new {@link Validation.Classes.ObjectValidator | ObjectValidator<T>}.
39
+ * @param fields - A {@link Validation.Classes.FieldValidators | FieldValidators<T>} containing
40
+ * a {@link Validation.Validator | Validator} for each field.
41
+ * @param options - An optional {@link Validation.Classes.ObjectValidatorOptions} to configure
42
+ * validation.
43
+ */
44
+ constructor(params) {
45
+ var _a;
46
+ super(params);
47
+ this.fields = params.fields;
48
+ this.options = (_a = params.options) !== null && _a !== void 0 ? _a : {};
49
+ this._innerValidators = ObjectValidator._resolveValidators(this.fields, this.options);
50
+ this._allowedFields =
51
+ this.options.strict === true ? new Set(Object.keys(this.fields)) : undefined;
52
+ }
53
+ /**
54
+ * Creates the actual {@link Validation.Classes.FieldValidators | FieldValidators<T>} to be
55
+ * used by this converter by applying any options or traits defined in the options
56
+ * to the field converters passed to the constructor.
57
+ * @param fields - The base {@link Validation.Classes.FieldValidators | FieldValidators<T>} passed
58
+ * in to the constructor.
59
+ * @param options - The {@link Validation.Classes.ObjectValidatorOptions | object validator options}
60
+ * passed in to the constructor.
61
+ * @returns A new {@link Validation.Classes.FieldValidators | FieldValidators} with the fully-configured
62
+ * individual {@link Validation.Validator | field validators} to be applied.
63
+ * @internal
64
+ */
65
+ static _resolveValidators(fields, options) {
66
+ var _a;
67
+ const resolved = {};
68
+ for (const key in fields) {
69
+ if (fields[key]) {
70
+ // istanbul ignore next
71
+ const optional = fields[key].isOptional || ((_a = options === null || options === void 0 ? void 0 : options.optionalFields) === null || _a === void 0 ? void 0 : _a.includes(key));
72
+ resolved[key] = new field_1.FieldValidator(key, fields[key], { optional });
73
+ }
74
+ }
75
+ return resolved;
76
+ }
77
+ /**
78
+ * Creates a new {@link Validation.Classes.ObjectValidator | ObjectValidator} derived from this one but with
79
+ * new optional properties as specified by a supplied
80
+ * {@link Validation.Classes.ObjectValidatorOptions | ObjectValidatorOptions<T>}.
81
+ * @param options - The {@link Validation.Classes.ObjectValidatorOptions | options} to be applied to the new
82
+ * {@link Validation.Classes.ObjectValidator | validator}.
83
+ * @returns A new {@link Validation.Classes.ObjectValidator | ObjectValidator} with the additional optional
84
+ * source properties.
85
+ */
86
+ partial(options) {
87
+ // istanbul ignore next
88
+ options = options !== null && options !== void 0 ? options : {};
89
+ return new ObjectValidator({
90
+ fields: this.fields,
91
+ options: Object.assign(Object.assign({}, this.options), options),
92
+ traits: this.traits
93
+ });
94
+ }
95
+ /**
96
+ * Creates a new {@link Validation.Classes.ObjectValidator | ObjectValidator} derived from this one but with
97
+ * new optional properties as specified by a supplied array of `keyof T`.
98
+ * @param addOptionalProperties - The keys to be made optional.
99
+ * @returns A new {@link Validation.Classes.ObjectValidator | ObjectValidator} with the additional optional
100
+ * source properties.
101
+ */
102
+ addPartial(addOptionalFields) {
103
+ var _a;
104
+ return this.partial({
105
+ optionalFields: [...((_a = this.options.optionalFields) !== null && _a !== void 0 ? _a : []), ...addOptionalFields]
106
+ });
107
+ }
108
+ /**
109
+ * {@inheritdoc Validation.ValidatorBase._validate}
110
+ * @internal
111
+ */
112
+ _validate(from, context) {
113
+ if (typeof from !== 'object' || from === null || Array.isArray(from)) {
114
+ return (0, base_1.fail)('source is not an object');
115
+ }
116
+ // eslint bug thinks key is used before defined
117
+ // eslint-disable-next-line no-use-before-define
118
+ const converted = {};
119
+ const errors = [];
120
+ for (const key in this._innerValidators) {
121
+ if (this._innerValidators[key]) {
122
+ const result = this._innerValidators[key].validate(from, context);
123
+ if (result.success && result.value !== undefined) {
124
+ converted[key] = result.value;
125
+ }
126
+ else if (result.isFailure()) {
127
+ errors.push(result.message);
128
+ }
129
+ }
130
+ }
131
+ if (this._allowedFields) {
132
+ const invalid = Object.keys(from).filter((k) => !this._allowedFields.has(k));
133
+ invalid.forEach((key) => errors.push(`${key}: unexpected field in source object.`));
134
+ }
135
+ return errors.length === 0 ? true : (0, base_1.fail)(errors.join('\n'));
136
+ }
137
+ }
138
+ exports.ObjectValidator = ObjectValidator;
139
+ //# sourceMappingURL=object.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"object.js","sourceRoot":"","sources":["../../../src/packlets/validation/object.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,kCAAwC;AAExC,mDAAgF;AAEhF,mCAAyC;AA4CzC;;;;;;;GAOG;AACH,MAAa,eAAiC,SAAQ,6BAAoB;IAoBxE;;;;;;OAMG;IACH,YAAmB,MAA+C;;QAChE,KAAK,CAAC,MAAM,CAAC,CAAC;QAEd,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;QAC5B,IAAI,CAAC,OAAO,GAAG,MAAA,MAAM,CAAC,OAAO,mCAAI,EAAE,CAAC;QACpC,IAAI,CAAC,gBAAgB,GAAG,eAAe,CAAC,kBAAkB,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;QACtF,IAAI,CAAC,cAAc;YACjB,IAAI,CAAC,OAAO,CAAC,MAAM,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAgB,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAChG,CAAC;IAED;;;;;;;;;;;OAWG;IACO,MAAM,CAAC,kBAAkB,CACjC,MAA8B,EAC9B,OAAuC;;QAEvC,MAAM,QAAQ,GAAoC,EAAE,CAAC;QACrD,KAAK,MAAM,GAAG,IAAI,MAAM,EAAE;YACxB,IAAI,MAAM,CAAC,GAAG,CAAC,EAAE;gBACf,uBAAuB;gBACvB,MAAM,QAAQ,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,UAAU,KAAI,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,cAAc,0CAAE,Q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CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;gBAClE,IAAI,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC,KAAK,KAAK,SAAS,EAAE;oBAChD,SAAS,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC;iBAC/B;qBAAM,IAAI,MAAM,CAAC,SAAS,EAAE,EAAE;oBAC7B,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;iBAC7B;aACF;SACF;QAED,IAAI,IAAI,CAAC,cAAc,EAAE;YACvB,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,cAAe,CAAC,GAAG,CAAC,CAAY,CAAC,CAAC,CAAC;YACzF,OAAO,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,GAAG,sCAAsC,CAAC,CAAC,CAAC;SACrF;QACD,OAAO,MAAM,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAA,WAAI,EAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IAC9D,CAAC;CACF;AAjID,0CAiIC","sourcesContent":["/*\n * Copyright (c) 2021 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { Failure, fail } from '../base';\nimport { Validator, ValidatorOptions } from './validator';\nimport { ValidatorBase, ValidatorBaseConstructorParams } from './validatorBase';\n\nimport { FieldValidator } from './field';\n\n/**\n * Per-property {@link Validation.Validator | validators} for each of the properties in `<T>`.\n * @public\n */\nexport type FieldValidators<T, TC = unknown> = { [key in keyof T]: Validator<T[key], TC> };\n\n/**\n * Options for an {@link Validation.Classes.ObjectValidator | ObjectValidator}.\n * @public\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport interface ObjectValidatorOptions<T, TC> extends ValidatorOptions<TC> {\n /**\n * If present, lists optional fields. Missing non-optional fields cause an error.\n */\n optionalFields?: (keyof T)[];\n /**\n * If true, unrecognized fields yield an error. If false or undefined (default),\n * unrecognized fields are ignored.\n */\n strict?: boolean;\n}\n\n/**\n * Options for the {@link Validation.Classes.ObjectValidator | ObjectValidator} constructor.\n * @public\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport interface ObjectValidatorConstructorParams<T, TC> extends ValidatorBaseConstructorParams<T, TC> {\n /**\n * A {@link Validation.Classes.FieldValidators | FieldValidators} object specifying a\n * {@link Validation.Validator | Validator} for each of the expected properties\n * of a result object.\n */\n fields: FieldValidators<T>;\n /**\n * Optional additional {@link Validation.Classes.ObjectValidatorOptions | ValidatorOptions} to\n * configure validation.\n */\n options?: ObjectValidatorOptions<T, TC>;\n}\n\n/**\n * In-place {@link Validation.Validator | Validator} for an object of type `<T>`.\n * @remarks\n * By default, succeeds if all of the required fields exist and are validate, and fails if\n * any required fields do not exist or are invalid. See {@link Validation.Classes.ObjectValidatorOptions}\n * for other validation options.\n * @public\n */\nexport class ObjectValidator<T, TC = unknown> extends ValidatorBase<T, TC> {\n /**\n * A {@link Validation.Classes.FieldValidators | FieldValidators} object specifying a\n * {@link Validation.Validator | Validator} for each of the expected properties\n */\n public readonly fields: FieldValidators<T>;\n /**\n * {@link Validation.Classes.ObjectValidatorOptions | Options} which apply to this\n * validator.\n */\n public readonly options: ObjectValidatorOptions<T, TC>;\n /**\n * @internal\n */\n protected readonly _innerValidators: FieldValidators<T>;\n /**\n * @internal\n */\n protected readonly _allowedFields?: Set<keyof T>;\n\n /**\n * Constructs a new {@link Validation.Classes.ObjectValidator | ObjectValidator<T>}.\n * @param fields - A {@link Validation.Classes.FieldValidators | FieldValidators<T>} containing\n * a {@link Validation.Validator | Validator} for each field.\n * @param options - An optional {@link Validation.Classes.ObjectValidatorOptions} to configure\n * validation.\n */\n public constructor(params: ObjectValidatorConstructorParams<T, TC>) {\n super(params);\n\n this.fields = params.fields;\n this.options = params.options ?? {};\n this._innerValidators = ObjectValidator._resolveValidators(this.fields, this.options);\n this._allowedFields =\n this.options.strict === true ? new Set(Object.keys(this.fields) as (keyof T)[]) : undefined;\n }\n\n /**\n * Creates the actual {@link Validation.Classes.FieldValidators | FieldValidators<T>} to be\n * used by this converter by applying any options or traits defined in the options\n * to the field converters passed to the constructor.\n * @param fields - The base {@link Validation.Classes.FieldValidators | FieldValidators<T>} passed\n * in to the constructor.\n * @param options - The {@link Validation.Classes.ObjectValidatorOptions | object validator options}\n * passed in to the constructor.\n * @returns A new {@link Validation.Classes.FieldValidators | FieldValidators} with the fully-configured\n * individual {@link Validation.Validator | field validators} to be applied.\n * @internal\n */\n protected static _resolveValidators<T, TC>(\n fields: FieldValidators<T, TC>,\n options?: ObjectValidatorOptions<T, TC>\n ): FieldValidators<T, TC> {\n const resolved: Partial<FieldValidators<T, TC>> = {};\n for (const key in fields) {\n if (fields[key]) {\n // istanbul ignore next\n const optional = fields[key].isOptional || options?.optionalFields?.includes(key);\n resolved[key] = new FieldValidator(key, fields[key], { optional });\n }\n }\n return resolved as FieldValidators<T, TC>;\n }\n\n /**\n * Creates a new {@link Validation.Classes.ObjectValidator | ObjectValidator} derived from this one but with\n * new optional properties as specified by a supplied\n * {@link Validation.Classes.ObjectValidatorOptions | ObjectValidatorOptions<T>}.\n * @param options - The {@link Validation.Classes.ObjectValidatorOptions | options} to be applied to the new\n * {@link Validation.Classes.ObjectValidator | validator}.\n * @returns A new {@link Validation.Classes.ObjectValidator | ObjectValidator} with the additional optional\n * source properties.\n */\n public partial(options?: ObjectValidatorOptions<T, TC>): ObjectValidator<Partial<T>, TC> {\n // istanbul ignore next\n options = options ?? {};\n return new ObjectValidator<Partial<T>, TC>({\n fields: this.fields as FieldValidators<Partial<T>, TC>,\n options: {\n ...this.options,\n ...options\n },\n traits: this.traits\n });\n }\n\n /**\n * Creates a new {@link Validation.Classes.ObjectValidator | ObjectValidator} derived from this one but with\n * new optional properties as specified by a supplied array of `keyof T`.\n * @param addOptionalProperties - The keys to be made optional.\n * @returns A new {@link Validation.Classes.ObjectValidator | ObjectValidator} with the additional optional\n * source properties.\n */\n public addPartial(addOptionalFields: (keyof T)[]): ObjectValidator<Partial<T>, TC> {\n return this.partial({\n optionalFields: [...(this.options.optionalFields ?? []), ...addOptionalFields]\n });\n }\n\n /**\n * {@inheritdoc Validation.ValidatorBase._validate}\n * @internal\n */\n protected _validate(from: unknown, context?: TC): boolean | Failure<T> {\n if (typeof from !== 'object' || from === null || Array.isArray(from)) {\n return fail('source is not an object');\n }\n\n // eslint bug thinks key is used before defined\n // eslint-disable-next-line no-use-before-define\n const converted = {} as { [key in keyof T]: T[key] };\n const errors: string[] = [];\n for (const key in this._innerValidators) {\n if (this._innerValidators[key]) {\n const result = this._innerValidators[key].validate(from, context);\n if (result.success && result.value !== undefined) {\n converted[key] = result.value;\n } else if (result.isFailure()) {\n errors.push(result.message);\n }\n }\n }\n\n if (this._allowedFields) {\n const invalid = Object.keys(from).filter((k) => !this._allowedFields!.has(k as keyof T));\n invalid.forEach((key) => errors.push(`${key}: unexpected field in source object.`));\n }\n return errors.length === 0 ? true : fail(errors.join('\\n'));\n }\n}\n"]}
@@ -1,4 +1,4 @@
1
- import { Failure } from '../result';
1
+ import { Failure } from '../base';
2
2
  import { GenericValidator, GenericValidatorConstructorParams } from './genericValidator';
3
3
  /**
4
4
  * Parameters used to construct a {@link Validation.Classes.StringValidator | StringValidator}.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"string.d.ts","sourceRoot":"","sources":["../../../src/packlets/validation/string.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,OAAO,EAAQ,MAAM,SAAS,CAAC;AACxC,OAAO,EAAE,gBAAgB,EAAE,iCAAiC,EAAE,MAAM,oBAAoB,CAAC;AAEzF;;;GAGG;AACH,MAAM,MAAM,gCAAgC,CAC1C,CAAC,SAAS,MAAM,GAAG,MAAM,EACzB,EAAE,GAAG,OAAO,IACV,iCAAiC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;AAE7C;;;GAGG;AACH,qBAAa,eAAe,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,EAAE,EAAE,GAAG,OAAO,CAAE,SAAQ,gBAAgB,CAAC,CAAC,EAAE,EAAE,CAAC;IACnG;;;;OAIG;gBACgB,MAAM,CAAC,EAAE,gCAAgC,CAAC,CAAC,EAAE,EAAE,CAAC;IAOnE;;;;;OAKG;WACW,cAAc,CAAC,CAAC,SAAS,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC;CAMpF"}
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2021 Erik Fortune
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in all
13
+ * copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ * SOFTWARE.
22
+ */
23
+ Object.defineProperty(exports, "__esModule", { value: true });
24
+ exports.StringValidator = void 0;
25
+ const base_1 = require("../base");
26
+ const genericValidator_1 = require("./genericValidator");
27
+ /**
28
+ * An in-place {@link Validation.Validator | Validator} for `string` values.
29
+ * @public
30
+ */
31
+ class StringValidator extends genericValidator_1.GenericValidator {
32
+ /**
33
+ * Constructs a new {@link Validation.Classes.StringValidator | StringValidator}.
34
+ * @param params - Optional {@link Validation.Classes.StringValidatorConstructorParams | init params}
35
+ * for the new {@link Validation.Classes.StringValidator | StringValidator}.
36
+ */
37
+ constructor(params) {
38
+ super(Object.assign({ validator: StringValidator.validateString }, (params !== null && params !== void 0 ? params : {})));
39
+ }
40
+ /**
41
+ * Static method which validates that a supplied `unknown` value is a `string`.
42
+ * @param from - The `unknown` value to be tested.
43
+ * @returns Returns `true` if `from` is a `string`, or {@link Failure} with an error
44
+ * message if not.
45
+ */
46
+ static validateString(from) {
47
+ if (typeof from === 'string') {
48
+ return true;
49
+ }
50
+ return (0, base_1.fail)(`"${from}": not a string`);
51
+ }
52
+ }
53
+ exports.StringValidator = StringValidator;
54
+ //# sourceMappingURL=string.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"string.js","sourceRoot":"","sources":["../../../src/packlets/validation/string.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,kCAAwC;AACxC,yDAAyF;AAWzF;;;GAGG;AACH,MAAa,eAAyD,SAAQ,mCAAuB;IACnG;;;;OAIG;IACH,YAAmB,MAAgD;QACjE,KAAK,iBACH,SAAS,EAAE,eAAe,CAAC,cAAc,IACtC,CAAC,MAAM,aAAN,MAAM,cAAN,MAAM,GAAI,EAAE,CAAC,EACjB,CAAC;IACL,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,cAAc,CAAmB,IAAa;QAC1D,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE;YAC5B,OAAO,IAAI,CAAC;SACb;QACD,OAAO,IAAA,WAAI,EAAI,IAAI,IAAI,iBAAiB,CAAC,CAAC;IAC5C,CAAC;CACF;AAzBD,0CAyBC","sourcesContent":["/*\n * Copyright (c) 2021 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { Failure, fail } from '../base';\nimport { GenericValidator, GenericValidatorConstructorParams } from './genericValidator';\n\n/**\n * Parameters used to construct a {@link Validation.Classes.StringValidator | StringValidator}.\n * @public\n */\nexport type StringValidatorConstructorParams<\n T extends string = string,\n TC = unknown\n> = GenericValidatorConstructorParams<T, TC>;\n\n/**\n * An in-place {@link Validation.Validator | Validator} for `string` values.\n * @public\n */\nexport class StringValidator<T extends string = string, TC = unknown> extends GenericValidator<T, TC> {\n /**\n * Constructs a new {@link Validation.Classes.StringValidator | StringValidator}.\n * @param params - Optional {@link Validation.Classes.StringValidatorConstructorParams | init params}\n * for the new {@link Validation.Classes.StringValidator | StringValidator}.\n */\n public constructor(params?: StringValidatorConstructorParams<T, TC>) {\n super({\n validator: StringValidator.validateString,\n ...(params ?? {})\n });\n }\n\n /**\n * Static method which validates that a supplied `unknown` value is a `string`.\n * @param from - The `unknown` value to be tested.\n * @returns Returns `true` if `from` is a `string`, or {@link Failure} with an error\n * message if not.\n */\n public static validateString<T extends string>(from: unknown): boolean | Failure<T> {\n if (typeof from === 'string') {\n return true;\n }\n return fail<T>(`\"${from}\": not a string`);\n }\n}\n"]}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"traits.d.ts","sourceRoot":"","sources":["../../../src/packlets/validation/traits.ts"],"names":[],"mappings":"AAsBA;;;;;GAKG;AAEH,MAAM,WAAW,uBAAuB;IACtC,IAAI,EAAE,UAAU,CAAC;CAClB;AAED;;;GAGG;AACH,MAAM,MAAM,eAAe,GAAG,uBAAuB,CAAC;AAEtD;;;GAGG;AAEH,MAAM,WAAW,oBAAoB;IACnC;;;OAGG;IACH,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;IAE7B;;;OAGG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IAExB;;;OAGG;IACH,QAAQ,CAAC,WAAW,EAAE,eAAe,EAAE,CAAC;CACzC;AAED;;;GAGG;AACH,eAAO,MAAM,sBAAsB,EAAE,oBAGpC,CAAC;AAEF;;;GAGG;AACH,qBAAa,eAAgB,YAAW,oBAAoB;IAC1D;;OAEG;IACH,SAAgB,UAAU,EAAE,OAAO,CAAC;IAEpC;;OAEG;IACH,SAAgB,KAAK,CAAC,EAAE,MAAM,CAAC;IAE/B;;OAEG;IACH,SAAgB,WAAW,EAAE,eAAe,EAAE,CAAC;IAE/C;;;;;;;OAOG;gBACgB,IAAI,CAAC,EAAE,OAAO,CAAC,oBAAoB,CAAC,EAAE,IAAI,CAAC,EAAE,oBAAoB;CASrF"}