bguard 0.0.4 → 0.0.5

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 (148) hide show
  1. package/lib/{CommonSchema-DeTBsQhn.d.mts → CommonSchema-BIe85W_A.d.mts} +3 -1
  2. package/lib/{CommonSchema-DeTBsQhn.d.ts → CommonSchema-BIe85W_A.d.ts} +3 -1
  3. package/lib/InferType.d.mts +18 -0
  4. package/lib/InferType.d.ts +18 -0
  5. package/lib/InferType.js +19 -0
  6. package/lib/InferType.js.map +1 -0
  7. package/lib/InferType.mjs +1 -0
  8. package/lib/InferType.mjs.map +1 -0
  9. package/lib/__tests__/ArraySchema.test.d.mts +2 -0
  10. package/lib/__tests__/ArraySchema.test.d.ts +2 -0
  11. package/lib/__tests__/ArraySchema.test.js +201 -0
  12. package/lib/__tests__/ArraySchema.test.js.map +1 -0
  13. package/lib/__tests__/ArraySchema.test.mjs +199 -0
  14. package/lib/__tests__/ArraySchema.test.mjs.map +1 -0
  15. package/lib/__tests__/BooleanSchema.test.d.mts +2 -0
  16. package/lib/__tests__/BooleanSchema.test.d.ts +2 -0
  17. package/lib/__tests__/BooleanSchema.test.js +219 -0
  18. package/lib/__tests__/BooleanSchema.test.js.map +1 -0
  19. package/lib/__tests__/BooleanSchema.test.mjs +217 -0
  20. package/lib/__tests__/BooleanSchema.test.mjs.map +1 -0
  21. package/lib/__tests__/CommonSchema.test.d.mts +2 -0
  22. package/lib/__tests__/CommonSchema.test.d.ts +2 -0
  23. package/lib/__tests__/CommonSchema.test.js +253 -0
  24. package/lib/__tests__/CommonSchema.test.js.map +1 -0
  25. package/lib/__tests__/CommonSchema.test.mjs +251 -0
  26. package/lib/__tests__/CommonSchema.test.mjs.map +1 -0
  27. package/lib/__tests__/MixTypes.test.d.mts +2 -0
  28. package/lib/__tests__/MixTypes.test.d.ts +2 -0
  29. package/lib/__tests__/MixTypes.test.js +165 -0
  30. package/lib/__tests__/MixTypes.test.js.map +1 -0
  31. package/lib/__tests__/MixTypes.test.mjs +163 -0
  32. package/lib/__tests__/MixTypes.test.mjs.map +1 -0
  33. package/lib/__tests__/NumberSchema.test.d.mts +2 -0
  34. package/lib/__tests__/NumberSchema.test.d.ts +2 -0
  35. package/lib/__tests__/NumberSchema.test.js +253 -0
  36. package/lib/__tests__/NumberSchema.test.js.map +1 -0
  37. package/lib/__tests__/NumberSchema.test.mjs +251 -0
  38. package/lib/__tests__/NumberSchema.test.mjs.map +1 -0
  39. package/lib/__tests__/ObjectSchema.test.d.mts +2 -0
  40. package/lib/__tests__/ObjectSchema.test.d.ts +2 -0
  41. package/lib/__tests__/ObjectSchema.test.js +356 -0
  42. package/lib/__tests__/ObjectSchema.test.js.map +1 -0
  43. package/lib/__tests__/ObjectSchema.test.mjs +354 -0
  44. package/lib/__tests__/ObjectSchema.test.mjs.map +1 -0
  45. package/lib/__tests__/StringSchema.test.d.mts +2 -0
  46. package/lib/__tests__/StringSchema.test.d.ts +2 -0
  47. package/lib/__tests__/StringSchema.test.js +228 -0
  48. package/lib/__tests__/StringSchema.test.js.map +1 -0
  49. package/lib/__tests__/StringSchema.test.mjs +226 -0
  50. package/lib/__tests__/StringSchema.test.mjs.map +1 -0
  51. package/lib/asserts/array/index.d.mts +18 -2
  52. package/lib/asserts/array/index.d.ts +18 -2
  53. package/lib/asserts/boolean/index.d.mts +16 -2
  54. package/lib/asserts/boolean/index.d.ts +16 -2
  55. package/lib/asserts/common/equalTo.d.mts +1 -1
  56. package/lib/asserts/common/equalTo.d.ts +1 -1
  57. package/lib/asserts/common/index.d.mts +1 -1
  58. package/lib/asserts/common/index.d.ts +1 -1
  59. package/lib/asserts/number/index.d.mts +16 -2
  60. package/lib/asserts/number/index.d.ts +16 -2
  61. package/lib/asserts/number/max.d.mts +1 -1
  62. package/lib/asserts/number/max.d.ts +1 -1
  63. package/lib/asserts/number/maxExcluded.d.mts +1 -1
  64. package/lib/asserts/number/maxExcluded.d.ts +1 -1
  65. package/lib/asserts/number/min.d.mts +1 -1
  66. package/lib/asserts/number/min.d.ts +1 -1
  67. package/lib/asserts/number/minExcluded.d.mts +1 -1
  68. package/lib/asserts/number/minExcluded.d.ts +1 -1
  69. package/lib/asserts/number/negative.d.mts +1 -1
  70. package/lib/asserts/number/negative.d.ts +1 -1
  71. package/lib/asserts/number/positive.d.mts +1 -1
  72. package/lib/asserts/number/positive.d.ts +1 -1
  73. package/lib/asserts/object/index.d.mts +21 -2
  74. package/lib/asserts/object/index.d.ts +21 -2
  75. package/lib/asserts/string/email.d.mts +1 -1
  76. package/lib/asserts/string/email.d.ts +1 -1
  77. package/lib/asserts/string/index.d.mts +16 -2
  78. package/lib/asserts/string/index.d.ts +16 -2
  79. package/lib/asserts/string/maxLength.d.mts +1 -1
  80. package/lib/asserts/string/maxLength.d.ts +1 -1
  81. package/lib/asserts/string/minLength.d.mts +1 -1
  82. package/lib/asserts/string/minLength.d.ts +1 -1
  83. package/lib/asserts/string/regExp.d.mts +1 -1
  84. package/lib/asserts/string/regExp.d.ts +1 -1
  85. package/lib/core.d.mts +1 -0
  86. package/lib/core.d.ts +1 -0
  87. package/lib/core.js +111 -0
  88. package/lib/core.js.map +1 -0
  89. package/lib/core.mjs +83 -0
  90. package/lib/core.mjs.map +1 -0
  91. package/lib/exceptions.d.mts +12 -0
  92. package/lib/exceptions.d.ts +12 -0
  93. package/lib/exceptions.js +52 -0
  94. package/lib/exceptions.js.map +1 -0
  95. package/lib/exceptions.mjs +25 -0
  96. package/lib/exceptions.mjs.map +1 -0
  97. package/lib/index.d.mts +14 -51
  98. package/lib/index.d.ts +14 -51
  99. package/lib/index.js +8 -2
  100. package/lib/index.js.map +1 -1
  101. package/lib/index.mjs +4 -1
  102. package/lib/parseSchema.d.mts +37 -0
  103. package/lib/parseSchema.d.ts +37 -0
  104. package/lib/parseSchema.js +119 -0
  105. package/lib/parseSchema.js.map +1 -0
  106. package/lib/parseSchema.mjs +92 -0
  107. package/lib/parseSchema.mjs.map +1 -0
  108. package/lib/schemas/ArraySchema.d.mts +13 -0
  109. package/lib/schemas/ArraySchema.d.ts +13 -0
  110. package/lib/schemas/ArraySchema.js +85 -0
  111. package/lib/schemas/ArraySchema.js.map +1 -0
  112. package/lib/schemas/ArraySchema.mjs +58 -0
  113. package/lib/schemas/ArraySchema.mjs.map +1 -0
  114. package/lib/{index-nzqBrOHf.d.mts → schemas/BooleanSchema.d.mts} +2 -14
  115. package/lib/{index-B1yBez0g.d.ts → schemas/BooleanSchema.d.ts} +2 -14
  116. package/lib/schemas/BooleanSchema.js +113 -0
  117. package/lib/schemas/BooleanSchema.js.map +1 -0
  118. package/lib/schemas/BooleanSchema.mjs +86 -0
  119. package/lib/schemas/BooleanSchema.mjs.map +1 -0
  120. package/lib/schemas/CommonSchema.d.mts +1 -0
  121. package/lib/schemas/CommonSchema.d.ts +1 -0
  122. package/lib/schemas/CommonSchema.js +67 -0
  123. package/lib/schemas/CommonSchema.js.map +1 -0
  124. package/lib/schemas/CommonSchema.mjs +40 -0
  125. package/lib/schemas/CommonSchema.mjs.map +1 -0
  126. package/lib/{index-B63cXDOm.d.mts → schemas/NumberSchema.d.mts} +2 -14
  127. package/lib/{index-CK_g_aEC.d.ts → schemas/NumberSchema.d.ts} +2 -14
  128. package/lib/schemas/NumberSchema.js +107 -0
  129. package/lib/schemas/NumberSchema.js.map +1 -0
  130. package/lib/schemas/NumberSchema.mjs +80 -0
  131. package/lib/schemas/NumberSchema.mjs.map +1 -0
  132. package/lib/schemas/ObjectSchema.d.mts +12 -0
  133. package/lib/schemas/ObjectSchema.d.ts +12 -0
  134. package/lib/schemas/ObjectSchema.js +89 -0
  135. package/lib/schemas/ObjectSchema.js.map +1 -0
  136. package/lib/schemas/ObjectSchema.mjs +62 -0
  137. package/lib/schemas/ObjectSchema.mjs.map +1 -0
  138. package/lib/{index-aBEYpJdZ.d.mts → schemas/StringSchema.d.mts} +2 -14
  139. package/lib/{index-BEyBEBSm.d.ts → schemas/StringSchema.d.ts} +2 -14
  140. package/lib/schemas/StringSchema.js +107 -0
  141. package/lib/schemas/StringSchema.js.map +1 -0
  142. package/lib/schemas/StringSchema.mjs +80 -0
  143. package/lib/schemas/StringSchema.mjs.map +1 -0
  144. package/package.json +28 -27
  145. package/lib/index-D3R2euaT.d.mts +0 -29
  146. package/lib/index-DPkImfCy.d.ts +0 -29
  147. package/lib/index-DitgsWDO.d.ts +0 -27
  148. package/lib/index-T3KBmyyn.d.mts +0 -27
@@ -0,0 +1,58 @@
1
+ // src/exceptions.ts
2
+ var BuildSchemaError = class extends Error {
3
+ };
4
+
5
+ // src/core.ts
6
+ var ctxSymbol = Symbol("contextSymbol");
7
+
8
+ // src/schemas/CommonSchema.ts
9
+ var CommonSchema = class {
10
+ [ctxSymbol];
11
+ constructor(ctx) {
12
+ this[ctxSymbol] = ctx;
13
+ }
14
+ /**
15
+ * @param validators - One or more custom validation functions.
16
+ * @returns {this} The schema instance with the added custom validation.
17
+ */
18
+ custom(...validators) {
19
+ this[ctxSymbol].requiredValidations.push(...validators);
20
+ return this;
21
+ }
22
+ /**
23
+ * Marks the schema as nullable, allowing the value to be `null`.
24
+ *
25
+ * @returns {WithNull<this>} The schema instance marked as nullable.
26
+ */
27
+ nullable() {
28
+ this[ctxSymbol].isNullable = true;
29
+ return this;
30
+ }
31
+ /**
32
+ * Marks the schema as optional, allowing the value to be `undefined`.
33
+ *
34
+ * @returns {WithUndefined<this>}} The schema instance marked as optional.
35
+ */
36
+ optional() {
37
+ this[ctxSymbol].isOptional = true;
38
+ return this;
39
+ }
40
+ };
41
+
42
+ // src/schemas/ArraySchema.ts
43
+ var ArraySchema = class extends CommonSchema {
44
+ _array = 1;
45
+ constructor(ctx, arraySchema) {
46
+ super(ctx);
47
+ this.validateArrayEntry(arraySchema);
48
+ this[ctxSymbol].array = arraySchema;
49
+ }
50
+ validateArrayEntry(arraySchema) {
51
+ if (!arraySchema) throw new BuildSchemaError("Missing schema in array method");
52
+ if (!(arraySchema instanceof CommonSchema)) throw new BuildSchemaError("Invalid schema in array method");
53
+ }
54
+ };
55
+ export {
56
+ ArraySchema
57
+ };
58
+ //# sourceMappingURL=ArraySchema.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/exceptions.ts","../../src/core.ts","../../src/schemas/CommonSchema.ts","../../src/schemas/ArraySchema.ts"],"sourcesContent":["export class ValidationError extends Error {\n expected: unknown;\n received: unknown;\n pathToError: string;\n message: string;\n constructor(expected: unknown, received: unknown, pathToError: string, message: string) {\n super();\n this.expected = expected;\n this.received = received;\n this.pathToError = pathToError;\n this.message = message;\n }\n}\n\nexport class BuildSchemaError extends Error {}\n\nexport function throwException(expected: unknown, received: unknown, pathToError: string, message: string): never {\n throw new ValidationError(expected, received, pathToError, message);\n}\n","export const ctxSymbol = Symbol('contextSymbol');\n\nimport { throwException } from './exceptions';\nimport type { CommonSchema } from './schemas/CommonSchema';\n\nexport function innerCheck(schema: CommonSchema, receivedValue: unknown, pathToError: string) {\n const ctx = schema[ctxSymbol];\n if (receivedValue === undefined) {\n if (!ctx.isOptional) throwException('Required', receivedValue, pathToError, 'The required value is missing');\n return receivedValue;\n }\n\n if (receivedValue === null) {\n if (!ctx.isNullable) throwException('Not null', receivedValue, pathToError, 'Value should not be null');\n return receivedValue;\n }\n\n if (ctx.array) {\n if (!Array.isArray(receivedValue))\n throwException('Array', receivedValue, pathToError, 'Expected an array but received a different type');\n const schema = ctx.array;\n\n receivedValue.forEach((elem, i) => {\n innerCheck(schema, elem, `${pathToError}[${i}]`);\n });\n\n return receivedValue;\n }\n\n const typeOfVal = typeof receivedValue;\n\n if (ctx.object) {\n if (typeOfVal !== 'object')\n throwException('Object', receivedValue, pathToError, 'Expected an object but received a different type');\n if (Array.isArray(receivedValue))\n throwException(\n 'Object',\n receivedValue,\n pathToError,\n 'Expected an object but received an array. Invalid type of data',\n );\n const shapeSchema = ctx.object;\n for (const keyPerReceivedValue of Object.keys(receivedValue)) {\n if (shapeSchema[keyPerReceivedValue] === undefined)\n throwException('Not allowed', keyPerReceivedValue, pathToError, 'This key is not allowed in the object');\n }\n\n for (const [keyOfSchema, valueOfSchema] of Object.entries(shapeSchema)) {\n const receivedObjectValuePropery = (receivedValue as Record<string, unknown>)[keyOfSchema];\n if (receivedObjectValuePropery === undefined) {\n if (!valueOfSchema[ctxSymbol].isOptional)\n throwException(\n 'Required',\n receivedObjectValuePropery,\n pathToError,\n 'Missing required property in the object',\n );\n }\n innerCheck(valueOfSchema, receivedObjectValuePropery, `${pathToError}.${keyOfSchema}`);\n }\n\n return receivedValue;\n }\n\n if (ctx.type.length) {\n if (!ctx.type.includes(typeOfVal)) throwException(ctx.type, typeOfVal, pathToError, 'Invalid type of data');\n }\n\n ctx.requiredValidations.forEach((requiredValidation) => {\n requiredValidation(receivedValue, pathToError);\n });\n\n return receivedValue;\n}\n","import { ctxSymbol } from '../core';\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type RequiredValidation = (received: any, pathToError: string) => void;\nexport type ObjectShapeSchemaType = Record<string, CommonSchema>;\n\nexport type PrimitiveType = 'number' | 'string' | 'boolean' | 'undefined' | 'object' | 'function' | 'symbol' | 'bigint';\nexport interface ValidatorContext {\n type: PrimitiveType[];\n isNullable?: boolean;\n isOptional?: boolean;\n requiredValidations: RequiredValidation[];\n array?: CommonSchema;\n object?: ObjectShapeSchemaType;\n}\n\nexport class CommonSchema {\n [ctxSymbol]: ValidatorContext;\n constructor(ctx: ValidatorContext) {\n this[ctxSymbol] = ctx;\n }\n\n /**\n * @param validators - One or more custom validation functions.\n * @returns {this} The schema instance with the added custom validation.\n */\n custom(...validators: RequiredValidation[]): this {\n this[ctxSymbol].requiredValidations.push(...validators);\n return this;\n }\n\n /**\n * Marks the schema as nullable, allowing the value to be `null`.\n *\n * @returns {WithNull<this>} The schema instance marked as nullable.\n */\n nullable(): WithNull<this> {\n this[ctxSymbol].isNullable = true;\n return this as WithNull<this>;\n }\n\n /**\n * Marks the schema as optional, allowing the value to be `undefined`.\n *\n * @returns {WithUndefined<this>}} The schema instance marked as optional.\n */\n optional(): WithUndefined<this> {\n this[ctxSymbol].isOptional = true;\n return this as WithUndefined<this>;\n }\n}\n\nexport type WithNull<T extends CommonSchema> = T & { validation_null: true };\nexport type WithUndefined<T extends CommonSchema> = T & { validation_undefined: true };\n\ntype TypeMapping = {\n number: number;\n string: string;\n boolean: boolean;\n undefined: undefined;\n object: object;\n // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type\n function: Function;\n symbol: symbol;\n bigint: bigint;\n};\n\nexport type WithMix<Y = unknown> = CommonSchema & { validation_mix: Y };\nexport type ExtractFromMix<T> = T extends WithMix<infer X> ? X : never;\n\nexport type MapMixTypes<T extends PrimitiveType[]> = T extends (infer U)[]\n ? U extends keyof TypeMapping\n ? TypeMapping[U]\n : never\n : never;\n","import { ctxSymbol } from '../core';\nimport { BuildSchemaError } from '../exceptions';\nimport { CommonSchema, ValidatorContext } from './CommonSchema';\n\nexport class ArraySchema extends CommonSchema {\n _array = 1;\n constructor(ctx: ValidatorContext, arraySchema: CommonSchema) {\n super(ctx);\n this.validateArrayEntry(arraySchema);\n this[ctxSymbol].array = arraySchema;\n }\n\n private validateArrayEntry(arraySchema: CommonSchema) {\n if (!arraySchema) throw new BuildSchemaError('Missing schema in array method');\n if (!(arraySchema instanceof CommonSchema)) throw new BuildSchemaError('Invalid schema in array method');\n }\n}\n\nexport type WithArray<Y extends CommonSchema> = ArraySchema & { validation_array: Y };\nexport type ExtractFromArray<T> = T extends WithArray<infer X> ? X : never;\n"],"mappings":";AAcO,IAAM,mBAAN,cAA+B,MAAM;AAAC;;;ACdtC,IAAM,YAAY,OAAO,eAAe;;;ACgBxC,IAAM,eAAN,MAAmB;AAAA,EACxB,CAAC,SAAS;AAAA,EACV,YAAY,KAAuB;AACjC,SAAK,SAAS,IAAI;AAAA,EACpB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,UAAU,YAAwC;AAChD,SAAK,SAAS,EAAE,oBAAoB,KAAK,GAAG,UAAU;AACtD,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,WAA2B;AACzB,SAAK,SAAS,EAAE,aAAa;AAC7B,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,WAAgC;AAC9B,SAAK,SAAS,EAAE,aAAa;AAC7B,WAAO;AAAA,EACT;AACF;;;AC9CO,IAAM,cAAN,cAA0B,aAAa;AAAA,EAC5C,SAAS;AAAA,EACT,YAAY,KAAuB,aAA2B;AAC5D,UAAM,GAAG;AACT,SAAK,mBAAmB,WAAW;AACnC,SAAK,SAAS,EAAE,QAAQ;AAAA,EAC1B;AAAA,EAEQ,mBAAmB,aAA2B;AACpD,QAAI,CAAC,YAAa,OAAM,IAAI,iBAAiB,gCAAgC;AAC7E,QAAI,EAAE,uBAAuB,cAAe,OAAM,IAAI,iBAAiB,gCAAgC;AAAA,EACzG;AACF;","names":[]}
@@ -1,4 +1,4 @@
1
- import { C as CommonSchema } from './CommonSchema-DeTBsQhn.mjs';
1
+ import { C as CommonSchema } from '../CommonSchema-BIe85W_A.mjs';
2
2
 
3
3
  declare class BooleanSchema extends CommonSchema {
4
4
  _boolean: number;
@@ -24,16 +24,4 @@ type WithBoolean<T extends BooleanSchema, Y = boolean> = T & {
24
24
  };
25
25
  type ExtractFromBoolean<T> = T extends WithBoolean<BooleanSchema, infer Y> ? Y : never;
26
26
 
27
- /**
28
- * Creates a new schema for validating boolean values.
29
- *
30
- * @returns {BooleanSchema} A new instance of `BooleanSchema` for validating booleans.
31
- *
32
- * @example
33
- * const schema = boolean();
34
- * parseSchema(schema, true); // Validates successfully
35
- * parseSchema(schema, 'true'); // Throws a validation error
36
- */
37
- declare function boolean(): BooleanSchema;
38
-
39
- export { BooleanSchema as B, type ExtractFromBoolean as E, type WithBoolean as W, boolean as b };
27
+ export { BooleanSchema, type ExtractFromBoolean, type WithBoolean };
@@ -1,4 +1,4 @@
1
- import { C as CommonSchema } from './CommonSchema-DeTBsQhn.js';
1
+ import { C as CommonSchema } from '../CommonSchema-BIe85W_A.js';
2
2
 
3
3
  declare class BooleanSchema extends CommonSchema {
4
4
  _boolean: number;
@@ -24,16 +24,4 @@ type WithBoolean<T extends BooleanSchema, Y = boolean> = T & {
24
24
  };
25
25
  type ExtractFromBoolean<T> = T extends WithBoolean<BooleanSchema, infer Y> ? Y : never;
26
26
 
27
- /**
28
- * Creates a new schema for validating boolean values.
29
- *
30
- * @returns {BooleanSchema} A new instance of `BooleanSchema` for validating booleans.
31
- *
32
- * @example
33
- * const schema = boolean();
34
- * parseSchema(schema, true); // Validates successfully
35
- * parseSchema(schema, 'true'); // Throws a validation error
36
- */
37
- declare function boolean(): BooleanSchema;
38
-
39
- export { BooleanSchema as B, type ExtractFromBoolean as E, type WithBoolean as W, boolean as b };
27
+ export { BooleanSchema, type ExtractFromBoolean, type WithBoolean };
@@ -0,0 +1,113 @@
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 __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/schemas/BooleanSchema.ts
21
+ var BooleanSchema_exports = {};
22
+ __export(BooleanSchema_exports, {
23
+ BooleanSchema: () => BooleanSchema
24
+ });
25
+ module.exports = __toCommonJS(BooleanSchema_exports);
26
+
27
+ // src/exceptions.ts
28
+ var ValidationError = class extends Error {
29
+ expected;
30
+ received;
31
+ pathToError;
32
+ message;
33
+ constructor(expected, received, pathToError, message) {
34
+ super();
35
+ this.expected = expected;
36
+ this.received = received;
37
+ this.pathToError = pathToError;
38
+ this.message = message;
39
+ }
40
+ };
41
+ function throwException(expected, received, pathToError, message) {
42
+ throw new ValidationError(expected, received, pathToError, message);
43
+ }
44
+
45
+ // src/core.ts
46
+ var ctxSymbol = Symbol("contextSymbol");
47
+
48
+ // src/schemas/CommonSchema.ts
49
+ var CommonSchema = class {
50
+ [ctxSymbol];
51
+ constructor(ctx) {
52
+ this[ctxSymbol] = ctx;
53
+ }
54
+ /**
55
+ * @param validators - One or more custom validation functions.
56
+ * @returns {this} The schema instance with the added custom validation.
57
+ */
58
+ custom(...validators) {
59
+ this[ctxSymbol].requiredValidations.push(...validators);
60
+ return this;
61
+ }
62
+ /**
63
+ * Marks the schema as nullable, allowing the value to be `null`.
64
+ *
65
+ * @returns {WithNull<this>} The schema instance marked as nullable.
66
+ */
67
+ nullable() {
68
+ this[ctxSymbol].isNullable = true;
69
+ return this;
70
+ }
71
+ /**
72
+ * Marks the schema as optional, allowing the value to be `undefined`.
73
+ *
74
+ * @returns {WithUndefined<this>}} The schema instance marked as optional.
75
+ */
76
+ optional() {
77
+ this[ctxSymbol].isOptional = true;
78
+ return this;
79
+ }
80
+ };
81
+
82
+ // src/schemas/BooleanSchema.ts
83
+ var isBoolean = (expected) => (received, pathToError) => {
84
+ if (received !== expected) throwException(expected, received, pathToError, `The received value is not ${expected}`);
85
+ };
86
+ var BooleanSchema = class extends CommonSchema {
87
+ _boolean = 1;
88
+ /**
89
+ * Restricts the schema to exactly match the boolean value true and infers the true value as the TypeScript type.
90
+ *
91
+ * @returns - The schema instance restricted to the value true, with the true value inferred as the TypeScript type
92
+ *
93
+ * @example - boolean().onlyTrue(); // Infers the type true
94
+ */
95
+ onlyTrue() {
96
+ return this.custom(isBoolean(true));
97
+ }
98
+ /**
99
+ * Restricts the schema to exactly match the boolean value false and infers the false value as the TypeScript type.
100
+ *
101
+ * @returns - The schema instance restricted to the value false, with the false value inferred as the TypeScript type
102
+ *
103
+ * @example - boolean().onlyFalse(); // Infers the type false
104
+ */
105
+ onlyFalse() {
106
+ return this.custom(isBoolean(false));
107
+ }
108
+ };
109
+ // Annotate the CommonJS export names for ESM import in node:
110
+ 0 && (module.exports = {
111
+ BooleanSchema
112
+ });
113
+ //# sourceMappingURL=BooleanSchema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/schemas/BooleanSchema.ts","../../src/exceptions.ts","../../src/core.ts","../../src/schemas/CommonSchema.ts"],"sourcesContent":["import { CommonSchema } from './CommonSchema';\nimport { throwException } from '../exceptions';\n\nconst isBoolean = (expected: boolean) => (received: boolean, pathToError: string) => {\n if (received !== expected) throwException(expected, received, pathToError, `The received value is not ${expected}`);\n};\n\nexport class BooleanSchema extends CommonSchema {\n _boolean = 1;\n\n /**\n * Restricts the schema to exactly match the boolean value true and infers the true value as the TypeScript type.\n *\n * @returns - The schema instance restricted to the value true, with the true value inferred as the TypeScript type\n *\n * @example - boolean().onlyTrue(); // Infers the type true\n */\n onlyTrue(): WithBoolean<this, true> {\n return this.custom(isBoolean(true)) as WithBoolean<this, true>;\n }\n\n /**\n * Restricts the schema to exactly match the boolean value false and infers the false value as the TypeScript type.\n *\n * @returns - The schema instance restricted to the value false, with the false value inferred as the TypeScript type\n *\n * @example - boolean().onlyFalse(); // Infers the type false\n */\n onlyFalse(): WithBoolean<this, false> {\n return this.custom(isBoolean(false)) as WithBoolean<this, false>;\n }\n}\n\nexport type WithBoolean<T extends BooleanSchema, Y = boolean> = T & { validation_boolean: Y };\nexport type ExtractFromBoolean<T> = T extends WithBoolean<BooleanSchema, infer Y> ? Y : never;\n","export class ValidationError extends Error {\n expected: unknown;\n received: unknown;\n pathToError: string;\n message: string;\n constructor(expected: unknown, received: unknown, pathToError: string, message: string) {\n super();\n this.expected = expected;\n this.received = received;\n this.pathToError = pathToError;\n this.message = message;\n }\n}\n\nexport class BuildSchemaError extends Error {}\n\nexport function throwException(expected: unknown, received: unknown, pathToError: string, message: string): never {\n throw new ValidationError(expected, received, pathToError, message);\n}\n","export const ctxSymbol = Symbol('contextSymbol');\n\nimport { throwException } from './exceptions';\nimport type { CommonSchema } from './schemas/CommonSchema';\n\nexport function innerCheck(schema: CommonSchema, receivedValue: unknown, pathToError: string) {\n const ctx = schema[ctxSymbol];\n if (receivedValue === undefined) {\n if (!ctx.isOptional) throwException('Required', receivedValue, pathToError, 'The required value is missing');\n return receivedValue;\n }\n\n if (receivedValue === null) {\n if (!ctx.isNullable) throwException('Not null', receivedValue, pathToError, 'Value should not be null');\n return receivedValue;\n }\n\n if (ctx.array) {\n if (!Array.isArray(receivedValue))\n throwException('Array', receivedValue, pathToError, 'Expected an array but received a different type');\n const schema = ctx.array;\n\n receivedValue.forEach((elem, i) => {\n innerCheck(schema, elem, `${pathToError}[${i}]`);\n });\n\n return receivedValue;\n }\n\n const typeOfVal = typeof receivedValue;\n\n if (ctx.object) {\n if (typeOfVal !== 'object')\n throwException('Object', receivedValue, pathToError, 'Expected an object but received a different type');\n if (Array.isArray(receivedValue))\n throwException(\n 'Object',\n receivedValue,\n pathToError,\n 'Expected an object but received an array. Invalid type of data',\n );\n const shapeSchema = ctx.object;\n for (const keyPerReceivedValue of Object.keys(receivedValue)) {\n if (shapeSchema[keyPerReceivedValue] === undefined)\n throwException('Not allowed', keyPerReceivedValue, pathToError, 'This key is not allowed in the object');\n }\n\n for (const [keyOfSchema, valueOfSchema] of Object.entries(shapeSchema)) {\n const receivedObjectValuePropery = (receivedValue as Record<string, unknown>)[keyOfSchema];\n if (receivedObjectValuePropery === undefined) {\n if (!valueOfSchema[ctxSymbol].isOptional)\n throwException(\n 'Required',\n receivedObjectValuePropery,\n pathToError,\n 'Missing required property in the object',\n );\n }\n innerCheck(valueOfSchema, receivedObjectValuePropery, `${pathToError}.${keyOfSchema}`);\n }\n\n return receivedValue;\n }\n\n if (ctx.type.length) {\n if (!ctx.type.includes(typeOfVal)) throwException(ctx.type, typeOfVal, pathToError, 'Invalid type of data');\n }\n\n ctx.requiredValidations.forEach((requiredValidation) => {\n requiredValidation(receivedValue, pathToError);\n });\n\n return receivedValue;\n}\n","import { ctxSymbol } from '../core';\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type RequiredValidation = (received: any, pathToError: string) => void;\nexport type ObjectShapeSchemaType = Record<string, CommonSchema>;\n\nexport type PrimitiveType = 'number' | 'string' | 'boolean' | 'undefined' | 'object' | 'function' | 'symbol' | 'bigint';\nexport interface ValidatorContext {\n type: PrimitiveType[];\n isNullable?: boolean;\n isOptional?: boolean;\n requiredValidations: RequiredValidation[];\n array?: CommonSchema;\n object?: ObjectShapeSchemaType;\n}\n\nexport class CommonSchema {\n [ctxSymbol]: ValidatorContext;\n constructor(ctx: ValidatorContext) {\n this[ctxSymbol] = ctx;\n }\n\n /**\n * @param validators - One or more custom validation functions.\n * @returns {this} The schema instance with the added custom validation.\n */\n custom(...validators: RequiredValidation[]): this {\n this[ctxSymbol].requiredValidations.push(...validators);\n return this;\n }\n\n /**\n * Marks the schema as nullable, allowing the value to be `null`.\n *\n * @returns {WithNull<this>} The schema instance marked as nullable.\n */\n nullable(): WithNull<this> {\n this[ctxSymbol].isNullable = true;\n return this as WithNull<this>;\n }\n\n /**\n * Marks the schema as optional, allowing the value to be `undefined`.\n *\n * @returns {WithUndefined<this>}} The schema instance marked as optional.\n */\n optional(): WithUndefined<this> {\n this[ctxSymbol].isOptional = true;\n return this as WithUndefined<this>;\n }\n}\n\nexport type WithNull<T extends CommonSchema> = T & { validation_null: true };\nexport type WithUndefined<T extends CommonSchema> = T & { validation_undefined: true };\n\ntype TypeMapping = {\n number: number;\n string: string;\n boolean: boolean;\n undefined: undefined;\n object: object;\n // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type\n function: Function;\n symbol: symbol;\n bigint: bigint;\n};\n\nexport type WithMix<Y = unknown> = CommonSchema & { validation_mix: Y };\nexport type ExtractFromMix<T> = T extends WithMix<infer X> ? X : never;\n\nexport type MapMixTypes<T extends PrimitiveType[]> = T extends (infer U)[]\n ? U extends keyof TypeMapping\n ? TypeMapping[U]\n : never\n : never;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,IAAM,kBAAN,cAA8B,MAAM;AAAA,EACzC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,YAAY,UAAmB,UAAmB,aAAqB,SAAiB;AACtF,UAAM;AACN,SAAK,WAAW;AAChB,SAAK,WAAW;AAChB,SAAK,cAAc;AACnB,SAAK,UAAU;AAAA,EACjB;AACF;AAIO,SAAS,eAAe,UAAmB,UAAmB,aAAqB,SAAwB;AAChH,QAAM,IAAI,gBAAgB,UAAU,UAAU,aAAa,OAAO;AACpE;;;AClBO,IAAM,YAAY,OAAO,eAAe;;;ACgBxC,IAAM,eAAN,MAAmB;AAAA,EACxB,CAAC,SAAS;AAAA,EACV,YAAY,KAAuB;AACjC,SAAK,SAAS,IAAI;AAAA,EACpB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,UAAU,YAAwC;AAChD,SAAK,SAAS,EAAE,oBAAoB,KAAK,GAAG,UAAU;AACtD,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,WAA2B;AACzB,SAAK,SAAS,EAAE,aAAa;AAC7B,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,WAAgC;AAC9B,SAAK,SAAS,EAAE,aAAa;AAC7B,WAAO;AAAA,EACT;AACF;;;AH/CA,IAAM,YAAY,CAAC,aAAsB,CAAC,UAAmB,gBAAwB;AACnF,MAAI,aAAa,SAAU,gBAAe,UAAU,UAAU,aAAa,6BAA6B,QAAQ,EAAE;AACpH;AAEO,IAAM,gBAAN,cAA4B,aAAa;AAAA,EAC9C,WAAW;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASX,WAAoC;AAClC,WAAO,KAAK,OAAO,UAAU,IAAI,CAAC;AAAA,EACpC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,YAAsC;AACpC,WAAO,KAAK,OAAO,UAAU,KAAK,CAAC;AAAA,EACrC;AACF;","names":[]}
@@ -0,0 +1,86 @@
1
+ // src/exceptions.ts
2
+ var ValidationError = class extends Error {
3
+ expected;
4
+ received;
5
+ pathToError;
6
+ message;
7
+ constructor(expected, received, pathToError, message) {
8
+ super();
9
+ this.expected = expected;
10
+ this.received = received;
11
+ this.pathToError = pathToError;
12
+ this.message = message;
13
+ }
14
+ };
15
+ function throwException(expected, received, pathToError, message) {
16
+ throw new ValidationError(expected, received, pathToError, message);
17
+ }
18
+
19
+ // src/core.ts
20
+ var ctxSymbol = Symbol("contextSymbol");
21
+
22
+ // src/schemas/CommonSchema.ts
23
+ var CommonSchema = class {
24
+ [ctxSymbol];
25
+ constructor(ctx) {
26
+ this[ctxSymbol] = ctx;
27
+ }
28
+ /**
29
+ * @param validators - One or more custom validation functions.
30
+ * @returns {this} The schema instance with the added custom validation.
31
+ */
32
+ custom(...validators) {
33
+ this[ctxSymbol].requiredValidations.push(...validators);
34
+ return this;
35
+ }
36
+ /**
37
+ * Marks the schema as nullable, allowing the value to be `null`.
38
+ *
39
+ * @returns {WithNull<this>} The schema instance marked as nullable.
40
+ */
41
+ nullable() {
42
+ this[ctxSymbol].isNullable = true;
43
+ return this;
44
+ }
45
+ /**
46
+ * Marks the schema as optional, allowing the value to be `undefined`.
47
+ *
48
+ * @returns {WithUndefined<this>}} The schema instance marked as optional.
49
+ */
50
+ optional() {
51
+ this[ctxSymbol].isOptional = true;
52
+ return this;
53
+ }
54
+ };
55
+
56
+ // src/schemas/BooleanSchema.ts
57
+ var isBoolean = (expected) => (received, pathToError) => {
58
+ if (received !== expected) throwException(expected, received, pathToError, `The received value is not ${expected}`);
59
+ };
60
+ var BooleanSchema = class extends CommonSchema {
61
+ _boolean = 1;
62
+ /**
63
+ * Restricts the schema to exactly match the boolean value true and infers the true value as the TypeScript type.
64
+ *
65
+ * @returns - The schema instance restricted to the value true, with the true value inferred as the TypeScript type
66
+ *
67
+ * @example - boolean().onlyTrue(); // Infers the type true
68
+ */
69
+ onlyTrue() {
70
+ return this.custom(isBoolean(true));
71
+ }
72
+ /**
73
+ * Restricts the schema to exactly match the boolean value false and infers the false value as the TypeScript type.
74
+ *
75
+ * @returns - The schema instance restricted to the value false, with the false value inferred as the TypeScript type
76
+ *
77
+ * @example - boolean().onlyFalse(); // Infers the type false
78
+ */
79
+ onlyFalse() {
80
+ return this.custom(isBoolean(false));
81
+ }
82
+ };
83
+ export {
84
+ BooleanSchema
85
+ };
86
+ //# sourceMappingURL=BooleanSchema.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/exceptions.ts","../../src/core.ts","../../src/schemas/CommonSchema.ts","../../src/schemas/BooleanSchema.ts"],"sourcesContent":["export class ValidationError extends Error {\n expected: unknown;\n received: unknown;\n pathToError: string;\n message: string;\n constructor(expected: unknown, received: unknown, pathToError: string, message: string) {\n super();\n this.expected = expected;\n this.received = received;\n this.pathToError = pathToError;\n this.message = message;\n }\n}\n\nexport class BuildSchemaError extends Error {}\n\nexport function throwException(expected: unknown, received: unknown, pathToError: string, message: string): never {\n throw new ValidationError(expected, received, pathToError, message);\n}\n","export const ctxSymbol = Symbol('contextSymbol');\n\nimport { throwException } from './exceptions';\nimport type { CommonSchema } from './schemas/CommonSchema';\n\nexport function innerCheck(schema: CommonSchema, receivedValue: unknown, pathToError: string) {\n const ctx = schema[ctxSymbol];\n if (receivedValue === undefined) {\n if (!ctx.isOptional) throwException('Required', receivedValue, pathToError, 'The required value is missing');\n return receivedValue;\n }\n\n if (receivedValue === null) {\n if (!ctx.isNullable) throwException('Not null', receivedValue, pathToError, 'Value should not be null');\n return receivedValue;\n }\n\n if (ctx.array) {\n if (!Array.isArray(receivedValue))\n throwException('Array', receivedValue, pathToError, 'Expected an array but received a different type');\n const schema = ctx.array;\n\n receivedValue.forEach((elem, i) => {\n innerCheck(schema, elem, `${pathToError}[${i}]`);\n });\n\n return receivedValue;\n }\n\n const typeOfVal = typeof receivedValue;\n\n if (ctx.object) {\n if (typeOfVal !== 'object')\n throwException('Object', receivedValue, pathToError, 'Expected an object but received a different type');\n if (Array.isArray(receivedValue))\n throwException(\n 'Object',\n receivedValue,\n pathToError,\n 'Expected an object but received an array. Invalid type of data',\n );\n const shapeSchema = ctx.object;\n for (const keyPerReceivedValue of Object.keys(receivedValue)) {\n if (shapeSchema[keyPerReceivedValue] === undefined)\n throwException('Not allowed', keyPerReceivedValue, pathToError, 'This key is not allowed in the object');\n }\n\n for (const [keyOfSchema, valueOfSchema] of Object.entries(shapeSchema)) {\n const receivedObjectValuePropery = (receivedValue as Record<string, unknown>)[keyOfSchema];\n if (receivedObjectValuePropery === undefined) {\n if (!valueOfSchema[ctxSymbol].isOptional)\n throwException(\n 'Required',\n receivedObjectValuePropery,\n pathToError,\n 'Missing required property in the object',\n );\n }\n innerCheck(valueOfSchema, receivedObjectValuePropery, `${pathToError}.${keyOfSchema}`);\n }\n\n return receivedValue;\n }\n\n if (ctx.type.length) {\n if (!ctx.type.includes(typeOfVal)) throwException(ctx.type, typeOfVal, pathToError, 'Invalid type of data');\n }\n\n ctx.requiredValidations.forEach((requiredValidation) => {\n requiredValidation(receivedValue, pathToError);\n });\n\n return receivedValue;\n}\n","import { ctxSymbol } from '../core';\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type RequiredValidation = (received: any, pathToError: string) => void;\nexport type ObjectShapeSchemaType = Record<string, CommonSchema>;\n\nexport type PrimitiveType = 'number' | 'string' | 'boolean' | 'undefined' | 'object' | 'function' | 'symbol' | 'bigint';\nexport interface ValidatorContext {\n type: PrimitiveType[];\n isNullable?: boolean;\n isOptional?: boolean;\n requiredValidations: RequiredValidation[];\n array?: CommonSchema;\n object?: ObjectShapeSchemaType;\n}\n\nexport class CommonSchema {\n [ctxSymbol]: ValidatorContext;\n constructor(ctx: ValidatorContext) {\n this[ctxSymbol] = ctx;\n }\n\n /**\n * @param validators - One or more custom validation functions.\n * @returns {this} The schema instance with the added custom validation.\n */\n custom(...validators: RequiredValidation[]): this {\n this[ctxSymbol].requiredValidations.push(...validators);\n return this;\n }\n\n /**\n * Marks the schema as nullable, allowing the value to be `null`.\n *\n * @returns {WithNull<this>} The schema instance marked as nullable.\n */\n nullable(): WithNull<this> {\n this[ctxSymbol].isNullable = true;\n return this as WithNull<this>;\n }\n\n /**\n * Marks the schema as optional, allowing the value to be `undefined`.\n *\n * @returns {WithUndefined<this>}} The schema instance marked as optional.\n */\n optional(): WithUndefined<this> {\n this[ctxSymbol].isOptional = true;\n return this as WithUndefined<this>;\n }\n}\n\nexport type WithNull<T extends CommonSchema> = T & { validation_null: true };\nexport type WithUndefined<T extends CommonSchema> = T & { validation_undefined: true };\n\ntype TypeMapping = {\n number: number;\n string: string;\n boolean: boolean;\n undefined: undefined;\n object: object;\n // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type\n function: Function;\n symbol: symbol;\n bigint: bigint;\n};\n\nexport type WithMix<Y = unknown> = CommonSchema & { validation_mix: Y };\nexport type ExtractFromMix<T> = T extends WithMix<infer X> ? X : never;\n\nexport type MapMixTypes<T extends PrimitiveType[]> = T extends (infer U)[]\n ? U extends keyof TypeMapping\n ? TypeMapping[U]\n : never\n : never;\n","import { CommonSchema } from './CommonSchema';\nimport { throwException } from '../exceptions';\n\nconst isBoolean = (expected: boolean) => (received: boolean, pathToError: string) => {\n if (received !== expected) throwException(expected, received, pathToError, `The received value is not ${expected}`);\n};\n\nexport class BooleanSchema extends CommonSchema {\n _boolean = 1;\n\n /**\n * Restricts the schema to exactly match the boolean value true and infers the true value as the TypeScript type.\n *\n * @returns - The schema instance restricted to the value true, with the true value inferred as the TypeScript type\n *\n * @example - boolean().onlyTrue(); // Infers the type true\n */\n onlyTrue(): WithBoolean<this, true> {\n return this.custom(isBoolean(true)) as WithBoolean<this, true>;\n }\n\n /**\n * Restricts the schema to exactly match the boolean value false and infers the false value as the TypeScript type.\n *\n * @returns - The schema instance restricted to the value false, with the false value inferred as the TypeScript type\n *\n * @example - boolean().onlyFalse(); // Infers the type false\n */\n onlyFalse(): WithBoolean<this, false> {\n return this.custom(isBoolean(false)) as WithBoolean<this, false>;\n }\n}\n\nexport type WithBoolean<T extends BooleanSchema, Y = boolean> = T & { validation_boolean: Y };\nexport type ExtractFromBoolean<T> = T extends WithBoolean<BooleanSchema, infer Y> ? Y : never;\n"],"mappings":";AAAO,IAAM,kBAAN,cAA8B,MAAM;AAAA,EACzC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,YAAY,UAAmB,UAAmB,aAAqB,SAAiB;AACtF,UAAM;AACN,SAAK,WAAW;AAChB,SAAK,WAAW;AAChB,SAAK,cAAc;AACnB,SAAK,UAAU;AAAA,EACjB;AACF;AAIO,SAAS,eAAe,UAAmB,UAAmB,aAAqB,SAAwB;AAChH,QAAM,IAAI,gBAAgB,UAAU,UAAU,aAAa,OAAO;AACpE;;;AClBO,IAAM,YAAY,OAAO,eAAe;;;ACgBxC,IAAM,eAAN,MAAmB;AAAA,EACxB,CAAC,SAAS;AAAA,EACV,YAAY,KAAuB;AACjC,SAAK,SAAS,IAAI;AAAA,EACpB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,UAAU,YAAwC;AAChD,SAAK,SAAS,EAAE,oBAAoB,KAAK,GAAG,UAAU;AACtD,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,WAA2B;AACzB,SAAK,SAAS,EAAE,aAAa;AAC7B,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,WAAgC;AAC9B,SAAK,SAAS,EAAE,aAAa;AAC7B,WAAO;AAAA,EACT;AACF;;;AC/CA,IAAM,YAAY,CAAC,aAAsB,CAAC,UAAmB,gBAAwB;AACnF,MAAI,aAAa,SAAU,gBAAe,UAAU,UAAU,aAAa,6BAA6B,QAAQ,EAAE;AACpH;AAEO,IAAM,gBAAN,cAA4B,aAAa;AAAA,EAC9C,WAAW;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASX,WAAoC;AAClC,WAAO,KAAK,OAAO,UAAU,IAAI,CAAC;AAAA,EACpC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,YAAsC;AACpC,WAAO,KAAK,OAAO,UAAU,KAAK,CAAC;AAAA,EACrC;AACF;","names":[]}
@@ -0,0 +1 @@
1
+ export { C as CommonSchema, E as ExtractFromMix, M as MapMixTypes, O as ObjectShapeSchemaType, P as PrimitiveType, R as RequiredValidation, V as ValidatorContext, b as WithMix, a as WithNull, W as WithUndefined } from '../CommonSchema-BIe85W_A.mjs';
@@ -0,0 +1 @@
1
+ export { C as CommonSchema, E as ExtractFromMix, M as MapMixTypes, O as ObjectShapeSchemaType, P as PrimitiveType, R as RequiredValidation, V as ValidatorContext, b as WithMix, a as WithNull, W as WithUndefined } from '../CommonSchema-BIe85W_A.js';
@@ -0,0 +1,67 @@
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 __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/schemas/CommonSchema.ts
21
+ var CommonSchema_exports = {};
22
+ __export(CommonSchema_exports, {
23
+ CommonSchema: () => CommonSchema
24
+ });
25
+ module.exports = __toCommonJS(CommonSchema_exports);
26
+
27
+ // src/core.ts
28
+ var ctxSymbol = Symbol("contextSymbol");
29
+
30
+ // src/schemas/CommonSchema.ts
31
+ var CommonSchema = class {
32
+ [ctxSymbol];
33
+ constructor(ctx) {
34
+ this[ctxSymbol] = ctx;
35
+ }
36
+ /**
37
+ * @param validators - One or more custom validation functions.
38
+ * @returns {this} The schema instance with the added custom validation.
39
+ */
40
+ custom(...validators) {
41
+ this[ctxSymbol].requiredValidations.push(...validators);
42
+ return this;
43
+ }
44
+ /**
45
+ * Marks the schema as nullable, allowing the value to be `null`.
46
+ *
47
+ * @returns {WithNull<this>} The schema instance marked as nullable.
48
+ */
49
+ nullable() {
50
+ this[ctxSymbol].isNullable = true;
51
+ return this;
52
+ }
53
+ /**
54
+ * Marks the schema as optional, allowing the value to be `undefined`.
55
+ *
56
+ * @returns {WithUndefined<this>}} The schema instance marked as optional.
57
+ */
58
+ optional() {
59
+ this[ctxSymbol].isOptional = true;
60
+ return this;
61
+ }
62
+ };
63
+ // Annotate the CommonJS export names for ESM import in node:
64
+ 0 && (module.exports = {
65
+ CommonSchema
66
+ });
67
+ //# sourceMappingURL=CommonSchema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/schemas/CommonSchema.ts","../../src/core.ts"],"sourcesContent":["import { ctxSymbol } from '../core';\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type RequiredValidation = (received: any, pathToError: string) => void;\nexport type ObjectShapeSchemaType = Record<string, CommonSchema>;\n\nexport type PrimitiveType = 'number' | 'string' | 'boolean' | 'undefined' | 'object' | 'function' | 'symbol' | 'bigint';\nexport interface ValidatorContext {\n type: PrimitiveType[];\n isNullable?: boolean;\n isOptional?: boolean;\n requiredValidations: RequiredValidation[];\n array?: CommonSchema;\n object?: ObjectShapeSchemaType;\n}\n\nexport class CommonSchema {\n [ctxSymbol]: ValidatorContext;\n constructor(ctx: ValidatorContext) {\n this[ctxSymbol] = ctx;\n }\n\n /**\n * @param validators - One or more custom validation functions.\n * @returns {this} The schema instance with the added custom validation.\n */\n custom(...validators: RequiredValidation[]): this {\n this[ctxSymbol].requiredValidations.push(...validators);\n return this;\n }\n\n /**\n * Marks the schema as nullable, allowing the value to be `null`.\n *\n * @returns {WithNull<this>} The schema instance marked as nullable.\n */\n nullable(): WithNull<this> {\n this[ctxSymbol].isNullable = true;\n return this as WithNull<this>;\n }\n\n /**\n * Marks the schema as optional, allowing the value to be `undefined`.\n *\n * @returns {WithUndefined<this>}} The schema instance marked as optional.\n */\n optional(): WithUndefined<this> {\n this[ctxSymbol].isOptional = true;\n return this as WithUndefined<this>;\n }\n}\n\nexport type WithNull<T extends CommonSchema> = T & { validation_null: true };\nexport type WithUndefined<T extends CommonSchema> = T & { validation_undefined: true };\n\ntype TypeMapping = {\n number: number;\n string: string;\n boolean: boolean;\n undefined: undefined;\n object: object;\n // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type\n function: Function;\n symbol: symbol;\n bigint: bigint;\n};\n\nexport type WithMix<Y = unknown> = CommonSchema & { validation_mix: Y };\nexport type ExtractFromMix<T> = T extends WithMix<infer X> ? X : never;\n\nexport type MapMixTypes<T extends PrimitiveType[]> = T extends (infer U)[]\n ? U extends keyof TypeMapping\n ? TypeMapping[U]\n : never\n : never;\n","export const ctxSymbol = Symbol('contextSymbol');\n\nimport { throwException } from './exceptions';\nimport type { CommonSchema } from './schemas/CommonSchema';\n\nexport function innerCheck(schema: CommonSchema, receivedValue: unknown, pathToError: string) {\n const ctx = schema[ctxSymbol];\n if (receivedValue === undefined) {\n if (!ctx.isOptional) throwException('Required', receivedValue, pathToError, 'The required value is missing');\n return receivedValue;\n }\n\n if (receivedValue === null) {\n if (!ctx.isNullable) throwException('Not null', receivedValue, pathToError, 'Value should not be null');\n return receivedValue;\n }\n\n if (ctx.array) {\n if (!Array.isArray(receivedValue))\n throwException('Array', receivedValue, pathToError, 'Expected an array but received a different type');\n const schema = ctx.array;\n\n receivedValue.forEach((elem, i) => {\n innerCheck(schema, elem, `${pathToError}[${i}]`);\n });\n\n return receivedValue;\n }\n\n const typeOfVal = typeof receivedValue;\n\n if (ctx.object) {\n if (typeOfVal !== 'object')\n throwException('Object', receivedValue, pathToError, 'Expected an object but received a different type');\n if (Array.isArray(receivedValue))\n throwException(\n 'Object',\n receivedValue,\n pathToError,\n 'Expected an object but received an array. Invalid type of data',\n );\n const shapeSchema = ctx.object;\n for (const keyPerReceivedValue of Object.keys(receivedValue)) {\n if (shapeSchema[keyPerReceivedValue] === undefined)\n throwException('Not allowed', keyPerReceivedValue, pathToError, 'This key is not allowed in the object');\n }\n\n for (const [keyOfSchema, valueOfSchema] of Object.entries(shapeSchema)) {\n const receivedObjectValuePropery = (receivedValue as Record<string, unknown>)[keyOfSchema];\n if (receivedObjectValuePropery === undefined) {\n if (!valueOfSchema[ctxSymbol].isOptional)\n throwException(\n 'Required',\n receivedObjectValuePropery,\n pathToError,\n 'Missing required property in the object',\n );\n }\n innerCheck(valueOfSchema, receivedObjectValuePropery, `${pathToError}.${keyOfSchema}`);\n }\n\n return receivedValue;\n }\n\n if (ctx.type.length) {\n if (!ctx.type.includes(typeOfVal)) throwException(ctx.type, typeOfVal, pathToError, 'Invalid type of data');\n }\n\n ctx.requiredValidations.forEach((requiredValidation) => {\n requiredValidation(receivedValue, pathToError);\n });\n\n return receivedValue;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,IAAM,YAAY,OAAO,eAAe;;;ADgBxC,IAAM,eAAN,MAAmB;AAAA,EACxB,CAAC,SAAS;AAAA,EACV,YAAY,KAAuB;AACjC,SAAK,SAAS,IAAI;AAAA,EACpB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,UAAU,YAAwC;AAChD,SAAK,SAAS,EAAE,oBAAoB,KAAK,GAAG,UAAU;AACtD,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,WAA2B;AACzB,SAAK,SAAS,EAAE,aAAa;AAC7B,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,WAAgC;AAC9B,SAAK,SAAS,EAAE,aAAa;AAC7B,WAAO;AAAA,EACT;AACF;","names":[]}
@@ -0,0 +1,40 @@
1
+ // src/core.ts
2
+ var ctxSymbol = Symbol("contextSymbol");
3
+
4
+ // src/schemas/CommonSchema.ts
5
+ var CommonSchema = class {
6
+ [ctxSymbol];
7
+ constructor(ctx) {
8
+ this[ctxSymbol] = ctx;
9
+ }
10
+ /**
11
+ * @param validators - One or more custom validation functions.
12
+ * @returns {this} The schema instance with the added custom validation.
13
+ */
14
+ custom(...validators) {
15
+ this[ctxSymbol].requiredValidations.push(...validators);
16
+ return this;
17
+ }
18
+ /**
19
+ * Marks the schema as nullable, allowing the value to be `null`.
20
+ *
21
+ * @returns {WithNull<this>} The schema instance marked as nullable.
22
+ */
23
+ nullable() {
24
+ this[ctxSymbol].isNullable = true;
25
+ return this;
26
+ }
27
+ /**
28
+ * Marks the schema as optional, allowing the value to be `undefined`.
29
+ *
30
+ * @returns {WithUndefined<this>}} The schema instance marked as optional.
31
+ */
32
+ optional() {
33
+ this[ctxSymbol].isOptional = true;
34
+ return this;
35
+ }
36
+ };
37
+ export {
38
+ CommonSchema
39
+ };
40
+ //# sourceMappingURL=CommonSchema.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/core.ts","../../src/schemas/CommonSchema.ts"],"sourcesContent":["export const ctxSymbol = Symbol('contextSymbol');\n\nimport { throwException } from './exceptions';\nimport type { CommonSchema } from './schemas/CommonSchema';\n\nexport function innerCheck(schema: CommonSchema, receivedValue: unknown, pathToError: string) {\n const ctx = schema[ctxSymbol];\n if (receivedValue === undefined) {\n if (!ctx.isOptional) throwException('Required', receivedValue, pathToError, 'The required value is missing');\n return receivedValue;\n }\n\n if (receivedValue === null) {\n if (!ctx.isNullable) throwException('Not null', receivedValue, pathToError, 'Value should not be null');\n return receivedValue;\n }\n\n if (ctx.array) {\n if (!Array.isArray(receivedValue))\n throwException('Array', receivedValue, pathToError, 'Expected an array but received a different type');\n const schema = ctx.array;\n\n receivedValue.forEach((elem, i) => {\n innerCheck(schema, elem, `${pathToError}[${i}]`);\n });\n\n return receivedValue;\n }\n\n const typeOfVal = typeof receivedValue;\n\n if (ctx.object) {\n if (typeOfVal !== 'object')\n throwException('Object', receivedValue, pathToError, 'Expected an object but received a different type');\n if (Array.isArray(receivedValue))\n throwException(\n 'Object',\n receivedValue,\n pathToError,\n 'Expected an object but received an array. Invalid type of data',\n );\n const shapeSchema = ctx.object;\n for (const keyPerReceivedValue of Object.keys(receivedValue)) {\n if (shapeSchema[keyPerReceivedValue] === undefined)\n throwException('Not allowed', keyPerReceivedValue, pathToError, 'This key is not allowed in the object');\n }\n\n for (const [keyOfSchema, valueOfSchema] of Object.entries(shapeSchema)) {\n const receivedObjectValuePropery = (receivedValue as Record<string, unknown>)[keyOfSchema];\n if (receivedObjectValuePropery === undefined) {\n if (!valueOfSchema[ctxSymbol].isOptional)\n throwException(\n 'Required',\n receivedObjectValuePropery,\n pathToError,\n 'Missing required property in the object',\n );\n }\n innerCheck(valueOfSchema, receivedObjectValuePropery, `${pathToError}.${keyOfSchema}`);\n }\n\n return receivedValue;\n }\n\n if (ctx.type.length) {\n if (!ctx.type.includes(typeOfVal)) throwException(ctx.type, typeOfVal, pathToError, 'Invalid type of data');\n }\n\n ctx.requiredValidations.forEach((requiredValidation) => {\n requiredValidation(receivedValue, pathToError);\n });\n\n return receivedValue;\n}\n","import { ctxSymbol } from '../core';\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type RequiredValidation = (received: any, pathToError: string) => void;\nexport type ObjectShapeSchemaType = Record<string, CommonSchema>;\n\nexport type PrimitiveType = 'number' | 'string' | 'boolean' | 'undefined' | 'object' | 'function' | 'symbol' | 'bigint';\nexport interface ValidatorContext {\n type: PrimitiveType[];\n isNullable?: boolean;\n isOptional?: boolean;\n requiredValidations: RequiredValidation[];\n array?: CommonSchema;\n object?: ObjectShapeSchemaType;\n}\n\nexport class CommonSchema {\n [ctxSymbol]: ValidatorContext;\n constructor(ctx: ValidatorContext) {\n this[ctxSymbol] = ctx;\n }\n\n /**\n * @param validators - One or more custom validation functions.\n * @returns {this} The schema instance with the added custom validation.\n */\n custom(...validators: RequiredValidation[]): this {\n this[ctxSymbol].requiredValidations.push(...validators);\n return this;\n }\n\n /**\n * Marks the schema as nullable, allowing the value to be `null`.\n *\n * @returns {WithNull<this>} The schema instance marked as nullable.\n */\n nullable(): WithNull<this> {\n this[ctxSymbol].isNullable = true;\n return this as WithNull<this>;\n }\n\n /**\n * Marks the schema as optional, allowing the value to be `undefined`.\n *\n * @returns {WithUndefined<this>}} The schema instance marked as optional.\n */\n optional(): WithUndefined<this> {\n this[ctxSymbol].isOptional = true;\n return this as WithUndefined<this>;\n }\n}\n\nexport type WithNull<T extends CommonSchema> = T & { validation_null: true };\nexport type WithUndefined<T extends CommonSchema> = T & { validation_undefined: true };\n\ntype TypeMapping = {\n number: number;\n string: string;\n boolean: boolean;\n undefined: undefined;\n object: object;\n // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type\n function: Function;\n symbol: symbol;\n bigint: bigint;\n};\n\nexport type WithMix<Y = unknown> = CommonSchema & { validation_mix: Y };\nexport type ExtractFromMix<T> = T extends WithMix<infer X> ? X : never;\n\nexport type MapMixTypes<T extends PrimitiveType[]> = T extends (infer U)[]\n ? U extends keyof TypeMapping\n ? TypeMapping[U]\n : never\n : never;\n"],"mappings":";AAAO,IAAM,YAAY,OAAO,eAAe;;;ACgBxC,IAAM,eAAN,MAAmB;AAAA,EACxB,CAAC,SAAS;AAAA,EACV,YAAY,KAAuB;AACjC,SAAK,SAAS,IAAI;AAAA,EACpB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,UAAU,YAAwC;AAChD,SAAK,SAAS,EAAE,oBAAoB,KAAK,GAAG,UAAU;AACtD,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,WAA2B;AACzB,SAAK,SAAS,EAAE,aAAa;AAC7B,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,WAAgC;AAC9B,SAAK,SAAS,EAAE,aAAa;AAC7B,WAAO;AAAA,EACT;AACF;","names":[]}
@@ -1,4 +1,4 @@
1
- import { C as CommonSchema } from './CommonSchema-DeTBsQhn.mjs';
1
+ import { C as CommonSchema } from '../CommonSchema-BIe85W_A.mjs';
2
2
 
3
3
  declare class NumberSchema extends CommonSchema {
4
4
  _number: number;
@@ -17,16 +17,4 @@ type WithNumber<T extends NumberSchema, Y = number> = T & {
17
17
  };
18
18
  type ExtractFromNumber<T> = T extends WithNumber<NumberSchema, infer Y> ? Y : never;
19
19
 
20
- /**
21
- * Creates a new schema for validating number values.
22
- *
23
- * @returns {NumberSchema} A new instance of `NumberSchema` for validating numbers.
24
- *
25
- * @example
26
- * const schema = number();
27
- * parseSchema(schema, 42); // Validates successfully
28
- * parseSchema(schema, '42'); // Throws a validation error
29
- */
30
- declare function number(): NumberSchema;
31
-
32
- export { type ExtractFromNumber as E, NumberSchema as N, type WithNumber as W, number as n };
20
+ export { type ExtractFromNumber, NumberSchema, type WithNumber };
@@ -1,4 +1,4 @@
1
- import { C as CommonSchema } from './CommonSchema-DeTBsQhn.js';
1
+ import { C as CommonSchema } from '../CommonSchema-BIe85W_A.js';
2
2
 
3
3
  declare class NumberSchema extends CommonSchema {
4
4
  _number: number;
@@ -17,16 +17,4 @@ type WithNumber<T extends NumberSchema, Y = number> = T & {
17
17
  };
18
18
  type ExtractFromNumber<T> = T extends WithNumber<NumberSchema, infer Y> ? Y : never;
19
19
 
20
- /**
21
- * Creates a new schema for validating number values.
22
- *
23
- * @returns {NumberSchema} A new instance of `NumberSchema` for validating numbers.
24
- *
25
- * @example
26
- * const schema = number();
27
- * parseSchema(schema, 42); // Validates successfully
28
- * parseSchema(schema, '42'); // Throws a validation error
29
- */
30
- declare function number(): NumberSchema;
31
-
32
- export { type ExtractFromNumber as E, NumberSchema as N, type WithNumber as W, number as n };
20
+ export { type ExtractFromNumber, NumberSchema, type WithNumber };