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
@@ -1,5 +1,7 @@
1
1
  declare const ctxSymbol: unique symbol;
2
2
 
3
+ declare function innerCheck(schema: CommonSchema, receivedValue: unknown, pathToError: string): {} | null | undefined;
4
+
3
5
  type RequiredValidation = (received: any, pathToError: string) => void;
4
6
  type ObjectShapeSchemaType = Record<string, CommonSchema>;
5
7
  type PrimitiveType = 'number' | 'string' | 'boolean' | 'undefined' | 'object' | 'function' | 'symbol' | 'bigint';
@@ -54,4 +56,4 @@ type WithMix<Y = unknown> = CommonSchema & {
54
56
  type ExtractFromMix<T> = T extends WithMix<infer X> ? X : never;
55
57
  type MapMixTypes<T extends PrimitiveType[]> = T extends (infer U)[] ? U extends keyof TypeMapping ? TypeMapping[U] : never : never;
56
58
 
57
- export { CommonSchema as C, type ExtractFromMix as E, type MapMixTypes as M, type ObjectShapeSchemaType as O, type PrimitiveType as P, type RequiredValidation as R, type ValidatorContext as V, type WithUndefined as W, type WithNull as a, type WithMix as b };
59
+ export { CommonSchema as C, type ExtractFromMix as E, type MapMixTypes as M, type ObjectShapeSchemaType as O, type PrimitiveType as P, type RequiredValidation as R, type ValidatorContext as V, type WithUndefined as W, type WithNull as a, type WithMix as b, ctxSymbol as c, innerCheck as i };
@@ -1,5 +1,7 @@
1
1
  declare const ctxSymbol: unique symbol;
2
2
 
3
+ declare function innerCheck(schema: CommonSchema, receivedValue: unknown, pathToError: string): {} | null | undefined;
4
+
3
5
  type RequiredValidation = (received: any, pathToError: string) => void;
4
6
  type ObjectShapeSchemaType = Record<string, CommonSchema>;
5
7
  type PrimitiveType = 'number' | 'string' | 'boolean' | 'undefined' | 'object' | 'function' | 'symbol' | 'bigint';
@@ -54,4 +56,4 @@ type WithMix<Y = unknown> = CommonSchema & {
54
56
  type ExtractFromMix<T> = T extends WithMix<infer X> ? X : never;
55
57
  type MapMixTypes<T extends PrimitiveType[]> = T extends (infer U)[] ? U extends keyof TypeMapping ? TypeMapping[U] : never : never;
56
58
 
57
- export { CommonSchema as C, type ExtractFromMix as E, type MapMixTypes as M, type ObjectShapeSchemaType as O, type PrimitiveType as P, type RequiredValidation as R, type ValidatorContext as V, type WithUndefined as W, type WithNull as a, type WithMix as b };
59
+ export { CommonSchema as C, type ExtractFromMix as E, type MapMixTypes as M, type ObjectShapeSchemaType as O, type PrimitiveType as P, type RequiredValidation as R, type ValidatorContext as V, type WithUndefined as W, type WithNull as a, type WithMix as b, ctxSymbol as c, innerCheck as i };
@@ -0,0 +1,18 @@
1
+ import { ArraySchema, ExtractFromArray } from './schemas/ArraySchema.mjs';
2
+ import { WithBoolean, BooleanSchema, ExtractFromBoolean } from './schemas/BooleanSchema.mjs';
3
+ import { W as WithUndefined, a as WithNull, b as WithMix, E as ExtractFromMix, C as CommonSchema } from './CommonSchema-BIe85W_A.mjs';
4
+ import { WithNumber, NumberSchema, ExtractFromNumber } from './schemas/NumberSchema.mjs';
5
+ import { ObjectSchema, WithObject } from './schemas/ObjectSchema.mjs';
6
+ import { WithString, StringSchema, ExtractFromString } from './schemas/StringSchema.mjs';
7
+
8
+ type InferType<T> = T extends WithUndefined<WithNull<WithString<StringSchema>>> ? ExtractFromString<T> | null | undefined : T extends WithUndefined<WithString<StringSchema>> ? ExtractFromString<T> | undefined : T extends WithNull<WithString<StringSchema>> ? ExtractFromString<T> | null : T extends WithString<StringSchema> ? ExtractFromString<T> : T extends WithUndefined<WithNull<StringSchema>> ? string | null | undefined : T extends WithUndefined<StringSchema> ? string | undefined : T extends WithNull<StringSchema> ? string | null : T extends StringSchema ? string : T extends WithUndefined<WithNull<WithNumber<NumberSchema>>> ? ExtractFromNumber<T> | null | undefined : T extends WithUndefined<WithNumber<NumberSchema>> ? ExtractFromNumber<T> | undefined : T extends WithNull<WithNumber<NumberSchema>> ? ExtractFromNumber<T> | null : T extends WithNumber<NumberSchema> ? ExtractFromNumber<T> : T extends WithUndefined<WithNull<NumberSchema>> ? number | null | undefined : T extends WithUndefined<NumberSchema> ? number | undefined : T extends WithNull<NumberSchema> ? number | null : T extends NumberSchema ? number : T extends WithUndefined<WithNull<WithBoolean<BooleanSchema>>> ? ExtractFromBoolean<T> | null | undefined : T extends WithUndefined<WithBoolean<BooleanSchema>> ? ExtractFromBoolean<T> | undefined : T extends WithNull<WithBoolean<BooleanSchema>> ? ExtractFromBoolean<T> | null : T extends WithBoolean<BooleanSchema> ? ExtractFromBoolean<T> : T extends WithUndefined<WithNull<BooleanSchema>> ? boolean | null | undefined : T extends WithUndefined<BooleanSchema> ? boolean | undefined : T extends WithNull<BooleanSchema> ? boolean | null : T extends BooleanSchema ? boolean : T extends WithUndefined<WithNull<ArraySchema>> ? InferType<ExtractFromArray<T>>[] | null | undefined : T extends WithUndefined<ArraySchema> ? InferType<ExtractFromArray<T>>[] | undefined : T extends WithNull<ArraySchema> ? InferType<ExtractFromArray<T>>[] | null : T extends ArraySchema ? InferType<ExtractFromArray<T>>[] : T extends WithUndefined<WithNull<ObjectSchema>> ? ExtractFromObject<T> | null | undefined : T extends WithUndefined<ObjectSchema> ? ExtractFromObject<T> | undefined : T extends WithNull<ObjectSchema> ? ExtractFromObject<T> | null : T extends ObjectSchema ? ExtractFromObject<T> : T extends WithUndefined<WithNull<WithMix>> ? ExtractFromMix<T> | null | undefined : T extends WithUndefined<WithMix> ? ExtractFromMix<T> | undefined : T extends WithNull<WithMix> ? ExtractFromMix<T> | null : T extends WithMix ? ExtractFromMix<T> : unknown;
9
+ type Merge<T> = T extends infer U ? {
10
+ [K in keyof U]: U[K];
11
+ } : never;
12
+ type ExtractFromObject<T extends ObjectSchema> = T extends WithObject<infer X> ? Merge<{
13
+ [K in keyof X as X[K] extends WithUndefined<CommonSchema> ? never : K]: InferType<X[K]>;
14
+ } & {
15
+ [K in keyof X as X[K] extends WithUndefined<CommonSchema> ? K : never]?: InferType<X[K]>;
16
+ }> : unknown;
17
+
18
+ export type { InferType };
@@ -0,0 +1,18 @@
1
+ import { ArraySchema, ExtractFromArray } from './schemas/ArraySchema.js';
2
+ import { WithBoolean, BooleanSchema, ExtractFromBoolean } from './schemas/BooleanSchema.js';
3
+ import { W as WithUndefined, a as WithNull, b as WithMix, E as ExtractFromMix, C as CommonSchema } from './CommonSchema-BIe85W_A.js';
4
+ import { WithNumber, NumberSchema, ExtractFromNumber } from './schemas/NumberSchema.js';
5
+ import { ObjectSchema, WithObject } from './schemas/ObjectSchema.js';
6
+ import { WithString, StringSchema, ExtractFromString } from './schemas/StringSchema.js';
7
+
8
+ type InferType<T> = T extends WithUndefined<WithNull<WithString<StringSchema>>> ? ExtractFromString<T> | null | undefined : T extends WithUndefined<WithString<StringSchema>> ? ExtractFromString<T> | undefined : T extends WithNull<WithString<StringSchema>> ? ExtractFromString<T> | null : T extends WithString<StringSchema> ? ExtractFromString<T> : T extends WithUndefined<WithNull<StringSchema>> ? string | null | undefined : T extends WithUndefined<StringSchema> ? string | undefined : T extends WithNull<StringSchema> ? string | null : T extends StringSchema ? string : T extends WithUndefined<WithNull<WithNumber<NumberSchema>>> ? ExtractFromNumber<T> | null | undefined : T extends WithUndefined<WithNumber<NumberSchema>> ? ExtractFromNumber<T> | undefined : T extends WithNull<WithNumber<NumberSchema>> ? ExtractFromNumber<T> | null : T extends WithNumber<NumberSchema> ? ExtractFromNumber<T> : T extends WithUndefined<WithNull<NumberSchema>> ? number | null | undefined : T extends WithUndefined<NumberSchema> ? number | undefined : T extends WithNull<NumberSchema> ? number | null : T extends NumberSchema ? number : T extends WithUndefined<WithNull<WithBoolean<BooleanSchema>>> ? ExtractFromBoolean<T> | null | undefined : T extends WithUndefined<WithBoolean<BooleanSchema>> ? ExtractFromBoolean<T> | undefined : T extends WithNull<WithBoolean<BooleanSchema>> ? ExtractFromBoolean<T> | null : T extends WithBoolean<BooleanSchema> ? ExtractFromBoolean<T> : T extends WithUndefined<WithNull<BooleanSchema>> ? boolean | null | undefined : T extends WithUndefined<BooleanSchema> ? boolean | undefined : T extends WithNull<BooleanSchema> ? boolean | null : T extends BooleanSchema ? boolean : T extends WithUndefined<WithNull<ArraySchema>> ? InferType<ExtractFromArray<T>>[] | null | undefined : T extends WithUndefined<ArraySchema> ? InferType<ExtractFromArray<T>>[] | undefined : T extends WithNull<ArraySchema> ? InferType<ExtractFromArray<T>>[] | null : T extends ArraySchema ? InferType<ExtractFromArray<T>>[] : T extends WithUndefined<WithNull<ObjectSchema>> ? ExtractFromObject<T> | null | undefined : T extends WithUndefined<ObjectSchema> ? ExtractFromObject<T> | undefined : T extends WithNull<ObjectSchema> ? ExtractFromObject<T> | null : T extends ObjectSchema ? ExtractFromObject<T> : T extends WithUndefined<WithNull<WithMix>> ? ExtractFromMix<T> | null | undefined : T extends WithUndefined<WithMix> ? ExtractFromMix<T> | undefined : T extends WithNull<WithMix> ? ExtractFromMix<T> | null : T extends WithMix ? ExtractFromMix<T> : unknown;
9
+ type Merge<T> = T extends infer U ? {
10
+ [K in keyof U]: U[K];
11
+ } : never;
12
+ type ExtractFromObject<T extends ObjectSchema> = T extends WithObject<infer X> ? Merge<{
13
+ [K in keyof X as X[K] extends WithUndefined<CommonSchema> ? never : K]: InferType<X[K]>;
14
+ } & {
15
+ [K in keyof X as X[K] extends WithUndefined<CommonSchema> ? K : never]?: InferType<X[K]>;
16
+ }> : unknown;
17
+
18
+ export type { InferType };
@@ -0,0 +1,19 @@
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 __copyProps = (to, from, except, desc) => {
7
+ if (from && typeof from === "object" || typeof from === "function") {
8
+ for (let key of __getOwnPropNames(from))
9
+ if (!__hasOwnProp.call(to, key) && key !== except)
10
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
+ }
12
+ return to;
13
+ };
14
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
+
16
+ // src/InferType.ts
17
+ var InferType_exports = {};
18
+ module.exports = __toCommonJS(InferType_exports);
19
+ //# sourceMappingURL=InferType.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/InferType.ts"],"sourcesContent":["import type { ArraySchema, ExtractFromArray } from './schemas/ArraySchema';\nimport type { BooleanSchema, ExtractFromBoolean, WithBoolean } from './schemas/BooleanSchema';\nimport type { CommonSchema, ExtractFromMix, WithMix, WithNull, WithUndefined } from './schemas/CommonSchema';\nimport type { ExtractFromNumber, NumberSchema, WithNumber } from './schemas/NumberSchema';\nimport type { ObjectSchema, WithObject } from './schemas/ObjectSchema';\nimport type { ExtractFromString, StringSchema, WithString } from './schemas/StringSchema';\n\n// prettier-ignore\nexport type InferType<T> =\n // with string\n T extends WithUndefined<WithNull<WithString<StringSchema>>>\n ? ExtractFromString<T> | null | undefined\n : T extends WithUndefined<WithString<StringSchema>>\n ? ExtractFromString<T> | undefined\n : T extends WithNull<WithString<StringSchema>>\n ? ExtractFromString<T> | null\n : T extends WithString<StringSchema>\n ? ExtractFromString<T> \n\n // string\n : T extends WithUndefined<WithNull<StringSchema>>\n ? string | null | undefined\n : T extends WithUndefined<StringSchema>\n ? string | undefined\n : T extends WithNull<StringSchema>\n ? string | null\n : T extends StringSchema\n ? string\n\n : // with number\n T extends WithUndefined<WithNull<WithNumber<NumberSchema>>>\n ? ExtractFromNumber<T> | null | undefined\n : T extends WithUndefined<WithNumber<NumberSchema>>\n ? ExtractFromNumber<T> | undefined\n : T extends WithNull<WithNumber<NumberSchema>>\n ? ExtractFromNumber<T> | null\n : T extends WithNumber<NumberSchema>\n ? ExtractFromNumber<T>\n\n : // number\n T extends WithUndefined<WithNull<NumberSchema>>\n ? number | null | undefined\n : T extends WithUndefined<NumberSchema>\n ? number | undefined\n : T extends WithNull<NumberSchema>\n ? number | null\n : T extends NumberSchema\n ? number\n\n : // with boolean\n T extends WithUndefined<WithNull<WithBoolean<BooleanSchema>>>\n ? ExtractFromBoolean<T> | null | undefined\n : T extends WithUndefined<WithBoolean<BooleanSchema>>\n ? ExtractFromBoolean<T> | undefined\n : T extends WithNull<WithBoolean<BooleanSchema>>\n ? ExtractFromBoolean<T> | null\n : T extends WithBoolean<BooleanSchema>\n ? ExtractFromBoolean<T>\n\n : // boolean\n T extends WithUndefined<WithNull<BooleanSchema>>\n ? boolean | null | undefined\n : T extends WithUndefined<BooleanSchema>\n ? boolean | undefined\n : T extends WithNull<BooleanSchema>\n ? boolean | null\n : T extends BooleanSchema\n ? boolean\n\n : // array\n T extends WithUndefined<WithNull<ArraySchema>>\n ? InferType<ExtractFromArray<T>>[] | null | undefined\n : T extends WithUndefined<ArraySchema>\n ? InferType<ExtractFromArray<T>>[] | undefined\n : T extends WithNull<ArraySchema>\n ? InferType<ExtractFromArray<T>>[] | null\n : T extends ArraySchema\n ? InferType<ExtractFromArray<T>>[]\n\n : // object\n T extends WithUndefined<WithNull<ObjectSchema>>\n ? ExtractFromObject<T> | null | undefined\n : T extends WithUndefined<ObjectSchema>\n ? ExtractFromObject<T> | undefined\n : T extends WithNull<ObjectSchema>\n ? ExtractFromObject<T> | null\n : T extends ObjectSchema\n ? ExtractFromObject<T>\n\n : // with mix\n T extends WithUndefined<WithNull<WithMix>>\n ? ExtractFromMix<T> | null | undefined\n : T extends WithUndefined<WithMix>\n ? ExtractFromMix<T> | undefined\n : T extends WithNull<WithMix>\n ? ExtractFromMix<T> | null\n : T extends WithMix\n ? ExtractFromMix<T>\n\n : unknown;\n\ntype Merge<T> = T extends infer U ? { [K in keyof U]: U[K] } : never;\n\ntype ExtractFromObject<T extends ObjectSchema> =\n T extends WithObject<infer X>\n ? Merge<\n { [K in keyof X as X[K] extends WithUndefined<CommonSchema> ? never : K]: InferType<X[K]> } & {\n [K in keyof X as X[K] extends WithUndefined<CommonSchema> ? K : never]?: InferType<X[K]>;\n }\n >\n : unknown;\n"],"mappings":";;;;;;;;;;;;;;;;AAAA;AAAA;","names":[]}
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=InferType.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -0,0 +1,2 @@
1
+
2
+ export { }
@@ -0,0 +1,2 @@
1
+
2
+ export { }
@@ -0,0 +1,201 @@
1
+ "use strict";
2
+
3
+ // jest/setup.ts
4
+ function expectEqualTypes(areEqual) {
5
+ expect(typeof areEqual).toBe("boolean");
6
+ }
7
+
8
+ // src/exceptions.ts
9
+ var ValidationError = class extends Error {
10
+ expected;
11
+ received;
12
+ pathToError;
13
+ message;
14
+ constructor(expected, received, pathToError, message) {
15
+ super();
16
+ this.expected = expected;
17
+ this.received = received;
18
+ this.pathToError = pathToError;
19
+ this.message = message;
20
+ }
21
+ };
22
+ var BuildSchemaError = class extends Error {
23
+ };
24
+ function throwException(expected, received, pathToError, message) {
25
+ throw new ValidationError(expected, received, pathToError, message);
26
+ }
27
+
28
+ // src/core.ts
29
+ var ctxSymbol = Symbol("contextSymbol");
30
+ function innerCheck(schema, receivedValue, pathToError) {
31
+ const ctx = schema[ctxSymbol];
32
+ if (receivedValue === void 0) {
33
+ if (!ctx.isOptional) throwException("Required", receivedValue, pathToError, "The required value is missing");
34
+ return receivedValue;
35
+ }
36
+ if (receivedValue === null) {
37
+ if (!ctx.isNullable) throwException("Not null", receivedValue, pathToError, "Value should not be null");
38
+ return receivedValue;
39
+ }
40
+ if (ctx.array) {
41
+ if (!Array.isArray(receivedValue))
42
+ throwException("Array", receivedValue, pathToError, "Expected an array but received a different type");
43
+ const schema2 = ctx.array;
44
+ receivedValue.forEach((elem, i) => {
45
+ innerCheck(schema2, elem, `${pathToError}[${i}]`);
46
+ });
47
+ return receivedValue;
48
+ }
49
+ const typeOfVal = typeof receivedValue;
50
+ if (ctx.object) {
51
+ if (typeOfVal !== "object")
52
+ throwException("Object", receivedValue, pathToError, "Expected an object but received a different type");
53
+ if (Array.isArray(receivedValue))
54
+ throwException(
55
+ "Object",
56
+ receivedValue,
57
+ pathToError,
58
+ "Expected an object but received an array. Invalid type of data"
59
+ );
60
+ const shapeSchema = ctx.object;
61
+ for (const keyPerReceivedValue of Object.keys(receivedValue)) {
62
+ if (shapeSchema[keyPerReceivedValue] === void 0)
63
+ throwException("Not allowed", keyPerReceivedValue, pathToError, "This key is not allowed in the object");
64
+ }
65
+ for (const [keyOfSchema, valueOfSchema] of Object.entries(shapeSchema)) {
66
+ const receivedObjectValuePropery = receivedValue[keyOfSchema];
67
+ if (receivedObjectValuePropery === void 0) {
68
+ if (!valueOfSchema[ctxSymbol].isOptional)
69
+ throwException(
70
+ "Required",
71
+ receivedObjectValuePropery,
72
+ pathToError,
73
+ "Missing required property in the object"
74
+ );
75
+ }
76
+ innerCheck(valueOfSchema, receivedObjectValuePropery, `${pathToError}.${keyOfSchema}`);
77
+ }
78
+ return receivedValue;
79
+ }
80
+ if (ctx.type.length) {
81
+ if (!ctx.type.includes(typeOfVal)) throwException(ctx.type, typeOfVal, pathToError, "Invalid type of data");
82
+ }
83
+ ctx.requiredValidations.forEach((requiredValidation) => {
84
+ requiredValidation(receivedValue, pathToError);
85
+ });
86
+ return receivedValue;
87
+ }
88
+
89
+ // src/parseSchema.ts
90
+ function parseSchema(schema, receivedValue) {
91
+ try {
92
+ return innerCheck(schema, receivedValue, "");
93
+ } catch (e) {
94
+ if (e instanceof ValidationError) throw e;
95
+ throw new Error("Something unexpected happened");
96
+ }
97
+ }
98
+
99
+ // src/asserts/common/equalTo.ts
100
+ var equalTo = (expected) => (received, pathToError) => {
101
+ if (expected !== received)
102
+ throwException(expected, received, pathToError, "The received value is not equal to expected");
103
+ };
104
+
105
+ // src/schemas/CommonSchema.ts
106
+ var CommonSchema = class {
107
+ [ctxSymbol];
108
+ constructor(ctx) {
109
+ this[ctxSymbol] = ctx;
110
+ }
111
+ /**
112
+ * @param validators - One or more custom validation functions.
113
+ * @returns {this} The schema instance with the added custom validation.
114
+ */
115
+ custom(...validators) {
116
+ this[ctxSymbol].requiredValidations.push(...validators);
117
+ return this;
118
+ }
119
+ /**
120
+ * Marks the schema as nullable, allowing the value to be `null`.
121
+ *
122
+ * @returns {WithNull<this>} The schema instance marked as nullable.
123
+ */
124
+ nullable() {
125
+ this[ctxSymbol].isNullable = true;
126
+ return this;
127
+ }
128
+ /**
129
+ * Marks the schema as optional, allowing the value to be `undefined`.
130
+ *
131
+ * @returns {WithUndefined<this>}} The schema instance marked as optional.
132
+ */
133
+ optional() {
134
+ this[ctxSymbol].isOptional = true;
135
+ return this;
136
+ }
137
+ };
138
+
139
+ // src/schemas/NumberSchema.ts
140
+ var NumberSchema = class extends CommonSchema {
141
+ _number = 1;
142
+ /**
143
+ * Restricts the schema to exactly match the specified value and infers the literal value as the TypeScript type.
144
+ *
145
+ * @param expectedValue - The value that the schema must exactly match.
146
+ * @returns - The schema instance restricted to the specified value, with the literal value inferred as the TypeScript type
147
+ *
148
+ * @example - number().equalTo(42); // Infers the type 42
149
+ */
150
+ equalTo(expectedValue) {
151
+ return this.custom(equalTo(expectedValue));
152
+ }
153
+ };
154
+
155
+ // src/asserts/number/index.ts
156
+ function number() {
157
+ return new NumberSchema({ type: ["number"], requiredValidations: [] });
158
+ }
159
+
160
+ // src/schemas/ArraySchema.ts
161
+ var ArraySchema = class extends CommonSchema {
162
+ _array = 1;
163
+ constructor(ctx, arraySchema) {
164
+ super(ctx);
165
+ this.validateArrayEntry(arraySchema);
166
+ this[ctxSymbol].array = arraySchema;
167
+ }
168
+ validateArrayEntry(arraySchema) {
169
+ if (!arraySchema) throw new BuildSchemaError("Missing schema in array method");
170
+ if (!(arraySchema instanceof CommonSchema)) throw new BuildSchemaError("Invalid schema in array method");
171
+ }
172
+ };
173
+
174
+ // src/asserts/array/index.ts
175
+ function array(arraySchema) {
176
+ return new ArraySchema({ type: [], requiredValidations: [] }, arraySchema);
177
+ }
178
+
179
+ // src/__tests__/ArraySchema.test.ts
180
+ describe("ArraySchema", () => {
181
+ it("should be an array", () => {
182
+ const arrayOfNumbersSchema = array(number());
183
+ expectEqualTypes(true);
184
+ const myArray = [1, 2, 3];
185
+ expect(parseSchema(arrayOfNumbersSchema, myArray)).toBe(myArray);
186
+ expect(() => parseSchema(arrayOfNumbersSchema, 1)).toThrow("Expected an array but received a different type");
187
+ });
188
+ it("should not build with missing parameters", () => {
189
+ const invalidArraySchemaFunction = () => array();
190
+ expectEqualTypes(true);
191
+ expect(invalidArraySchemaFunction).toThrow("Missing schema in array method");
192
+ expect(invalidArraySchemaFunction).toThrow(BuildSchemaError);
193
+ });
194
+ it("should not build with invalid parameters", () => {
195
+ const invalidArraySchemaFunction = () => array("not a bguard schema");
196
+ expectEqualTypes(true);
197
+ expect(invalidArraySchemaFunction).toThrow("Invalid schema in array method");
198
+ expect(invalidArraySchemaFunction).toThrow(BuildSchemaError);
199
+ });
200
+ });
201
+ //# sourceMappingURL=ArraySchema.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../jest/setup.ts","../../src/exceptions.ts","../../src/core.ts","../../src/parseSchema.ts","../../src/asserts/common/equalTo.ts","../../src/schemas/CommonSchema.ts","../../src/schemas/NumberSchema.ts","../../src/asserts/number/index.ts","../../src/schemas/ArraySchema.ts","../../src/asserts/array/index.ts","../../src/__tests__/ArraySchema.test.ts"],"sourcesContent":["export type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;\n\nexport function expectEqualTypes<X, Y>(areEqual: Equal<X, Y>): void {\n expect(typeof areEqual).toBe('boolean');\n}\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 { InferType } from './InferType';\nimport { innerCheck } from './core';\nimport { ValidationError } from './exceptions';\nimport { CommonSchema } from './schemas/CommonSchema';\n\n/**\n * Parses and validates a value against the provided schema, returning a type-safe result.\n *\n * This function will throw a `ValidationError` if the value does not conform to the schema.\n * The inferred TypeScript type of the returned value will match the structure defined by the schema.\n *\n * @template T\n * @param {T} schema - The schema to validate the received value against. This schema dictates the expected structure and type of the value.\n * @param {unknown} receivedValue - The value to be validated and parsed according to the schema.\n * @returns {InferType<T>} The validated value, with its TypeScript type inferred from the schema.\n *\n * @throws {ValidationError} If the received value does not match the schema, a `ValidationError` will be thrown.\n * @throws {Error} If an unexpected error occurs during validation, an error will be thrown with a generic message.\n *\n * @example\n * const schema = object({\n * name: string(),\n * age: number(),\n * });\n *\n * const result = parseSchema(schema, { name: 'Alice', age: 30 });\n * // result will be inferred as { name: string; age: number }\n *\n * parseSchema(schema, { name: 'Alice', age: '30' });\n * // Throws ValidationError because 'age' should be a number, not a string.\n */\nexport function parseSchema<T extends CommonSchema>(schema: T, receivedValue: unknown): InferType<T> {\n try {\n return innerCheck(schema, receivedValue, '') as InferType<T>;\n } catch (e) {\n if (e instanceof ValidationError) throw e;\n /* istanbul ignore next */\n throw new Error('Something unexpected happened');\n }\n}\n","import { throwException } from '../../exceptions';\nimport { RequiredValidation } from '../../schemas/CommonSchema';\n\n/**\n * Creates a custom assertion that checks if a value is equal to the expected value.\n *\n * This assertion can be used within a `.custom` method to enforce that a value strictly matches the expected value.\n * It will throw a `ValidationError` if the values are not equal.\n *\n * @param {unknown} expected - The value that the received value is expected to match.\n * @returns {RequiredValidation} A function that takes the received value and the path to the error,\n * and throws a `ValidationError` if the received value does not equal the expected value.\n *\n * @throws {ValidationError} If the received value does not match the expected value.\n *\n * @example\n * import { number, equalTo } from 'bguard';\n *\n * // Define a schema with a custom assertion\n * const schema = number().custom(equalTo(5));\n *\n * // This will pass\n * parseSchema(schema, 5);\n *\n * // This will throw a ValidationError\n * parseSchema(schema, 3);\n */\nexport const equalTo =\n (expected: unknown): RequiredValidation =>\n (received: number, pathToError: string) => {\n if (expected !== received)\n throwException(expected, received, pathToError, 'The received value is not equal to expected');\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 { equalTo } from '../asserts/common/equalTo';\n\nimport { CommonSchema } from './CommonSchema';\n\nexport class NumberSchema extends CommonSchema {\n _number = 1;\n\n /**\n * Restricts the schema to exactly match the specified value and infers the literal value as the TypeScript type.\n *\n * @param expectedValue - The value that the schema must exactly match.\n * @returns - The schema instance restricted to the specified value, with the literal value inferred as the TypeScript type\n *\n * @example - number().equalTo(42); // Infers the type 42\n */\n equalTo<Y extends number>(expectedValue: Y): WithNumber<this, Y> {\n return this.custom(equalTo(expectedValue)) as WithNumber<this, Y>;\n }\n}\n\nexport type WithNumber<T extends NumberSchema, Y = number> = T & { validation_number: Y };\nexport type ExtractFromNumber<T> = T extends WithNumber<NumberSchema, infer Y> ? Y : never;\n","import { NumberSchema } from '../../schemas/NumberSchema';\n\n/**\n * Creates a new schema for validating number values.\n *\n * @returns {NumberSchema} A new instance of `NumberSchema` for validating numbers.\n *\n * @example\n * const schema = number();\n * parseSchema(schema, 42); // Validates successfully\n * parseSchema(schema, '42'); // Throws a validation error\n */\nexport function number(): NumberSchema {\n return new NumberSchema({ type: ['number'], requiredValidations: [] });\n}\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","import { ArraySchema, WithArray } from '../../schemas/ArraySchema';\nimport { CommonSchema } from '../../schemas/CommonSchema';\n\n/**\n * Creates a new schema for validating arrays where each element must match the specified schema.\n *\n * @template T\n * @param {T} arraySchema - The schema that each element of the array must match.\n * @returns {WithArray<T>} A new instance of `ArraySchema` for validating arrays of elements that match the specified schema.\n *\n * @example\n * const schema = array(string());\n * parseSchema(schema, ['hello', 'world']); // Validates successfully\n * parseSchema(schema, ['hello', 123]); // Throws a validation error\n */\nexport function array<T extends CommonSchema>(arraySchema: T): WithArray<T> {\n return new ArraySchema({ type: [], requiredValidations: [] }, arraySchema) as WithArray<T>;\n}\n","import { expectEqualTypes } from '../../jest/setup';\nimport { BuildSchemaError } from '../exceptions';\nimport { parseSchema, InferType, array, number } from '../';\n\ndescribe('ArraySchema', () => {\n it('should be an array', () => {\n const arrayOfNumbersSchema = array(number());\n expectEqualTypes<number[], InferType<typeof arrayOfNumbersSchema>>(true);\n const myArray = [1, 2, 3];\n expect(parseSchema(arrayOfNumbersSchema, myArray)).toBe(myArray);\n expect(() => parseSchema(arrayOfNumbersSchema, 1)).toThrow('Expected an array but received a different type');\n });\n\n it('should not build with missing parameters', () => {\n //@ts-expect-error testing missing parameters in runtime\n const invalidArraySchemaFunction = () => array();\n expectEqualTypes<unknown[], InferType<ReturnType<typeof invalidArraySchemaFunction>>>(true);\n expect(invalidArraySchemaFunction).toThrow('Missing schema in array method');\n expect(invalidArraySchemaFunction).toThrow(BuildSchemaError);\n });\n\n it('should not build with invalid parameters', () => {\n //@ts-expect-error testing invalid schema in runtime\n const invalidArraySchemaFunction = () => array('not a bguard schema');\n expectEqualTypes<unknown[], InferType<ReturnType<typeof invalidArraySchemaFunction>>>(true);\n expect(invalidArraySchemaFunction).toThrow('Invalid schema in array method');\n expect(invalidArraySchemaFunction).toThrow(BuildSchemaError);\n });\n});\n"],"mappings":";;;AAEO,SAAS,iBAAuB,UAA6B;AAClE,SAAO,OAAO,QAAQ,EAAE,KAAK,SAAS;AACxC;;;ACJO,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;AAEO,IAAM,mBAAN,cAA+B,MAAM;AAAC;AAEtC,SAAS,eAAe,UAAmB,UAAmB,aAAqB,SAAwB;AAChH,QAAM,IAAI,gBAAgB,UAAU,UAAU,aAAa,OAAO;AACpE;;;AClBO,IAAM,YAAY,OAAO,eAAe;AAKxC,SAAS,WAAW,QAAsB,eAAwB,aAAqB;AAC5F,QAAM,MAAM,OAAO,SAAS;AAC5B,MAAI,kBAAkB,QAAW;AAC/B,QAAI,CAAC,IAAI,WAAY,gBAAe,YAAY,eAAe,aAAa,+BAA+B;AAC3G,WAAO;AAAA,EACT;AAEA,MAAI,kBAAkB,MAAM;AAC1B,QAAI,CAAC,IAAI,WAAY,gBAAe,YAAY,eAAe,aAAa,0BAA0B;AACtG,WAAO;AAAA,EACT;AAEA,MAAI,IAAI,OAAO;AACb,QAAI,CAAC,MAAM,QAAQ,aAAa;AAC9B,qBAAe,SAAS,eAAe,aAAa,iDAAiD;AACvG,UAAMA,UAAS,IAAI;AAEnB,kBAAc,QAAQ,CAAC,MAAM,MAAM;AACjC,iBAAWA,SAAQ,MAAM,GAAG,WAAW,IAAI,CAAC,GAAG;AAAA,IACjD,CAAC;AAED,WAAO;AAAA,EACT;AAEA,QAAM,YAAY,OAAO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,uBAAuB,MAAM,OAAO,CAAC;AAC3C,qBAAmE,IAAI;AACvE,UAAM,UAAU,CAAC,GAAG,GAAG,CAAC;AACxB,WAAO,YAAY,sBAAsB,OAAO,CAAC,EAAE,KAAK,OAAO;AAC/D,WAAO,MAAM,YAAY,sBAAsB,CAAC,CAAC,EAAE,QAAQ,iDAAiD;AAAA,EAC9G,CAAC;AAED,KAAG,4CAA4C,MAAM;AAEnD,UAAM,6BAA6B,MAAM,MAAM;AAC/C,qBAAsF,IAAI;AAC1F,WAAO,0BAA0B,EAAE,QAAQ,gCAAgC;AAC3E,WAAO,0BAA0B,EAAE,QAAQ,gBAAgB;AAAA,EAC7D,CAAC;AAED,KAAG,4CAA4C,MAAM;AAEnD,UAAM,6BAA6B,MAAM,MAAM,qBAAqB;AACpE,qBAAsF,IAAI;AAC1F,WAAO,0BAA0B,EAAE,QAAQ,gCAAgC;AAC3E,WAAO,0BAA0B,EAAE,QAAQ,gBAAgB;AAAA,EAC7D,CAAC;AACH,CAAC;","names":["schema"]}
@@ -0,0 +1,199 @@
1
+ // jest/setup.ts
2
+ function expectEqualTypes(areEqual) {
3
+ expect(typeof areEqual).toBe("boolean");
4
+ }
5
+
6
+ // src/exceptions.ts
7
+ var ValidationError = class extends Error {
8
+ expected;
9
+ received;
10
+ pathToError;
11
+ message;
12
+ constructor(expected, received, pathToError, message) {
13
+ super();
14
+ this.expected = expected;
15
+ this.received = received;
16
+ this.pathToError = pathToError;
17
+ this.message = message;
18
+ }
19
+ };
20
+ var BuildSchemaError = class extends Error {
21
+ };
22
+ function throwException(expected, received, pathToError, message) {
23
+ throw new ValidationError(expected, received, pathToError, message);
24
+ }
25
+
26
+ // src/core.ts
27
+ var ctxSymbol = Symbol("contextSymbol");
28
+ function innerCheck(schema, receivedValue, pathToError) {
29
+ const ctx = schema[ctxSymbol];
30
+ if (receivedValue === void 0) {
31
+ if (!ctx.isOptional) throwException("Required", receivedValue, pathToError, "The required value is missing");
32
+ return receivedValue;
33
+ }
34
+ if (receivedValue === null) {
35
+ if (!ctx.isNullable) throwException("Not null", receivedValue, pathToError, "Value should not be null");
36
+ return receivedValue;
37
+ }
38
+ if (ctx.array) {
39
+ if (!Array.isArray(receivedValue))
40
+ throwException("Array", receivedValue, pathToError, "Expected an array but received a different type");
41
+ const schema2 = ctx.array;
42
+ receivedValue.forEach((elem, i) => {
43
+ innerCheck(schema2, elem, `${pathToError}[${i}]`);
44
+ });
45
+ return receivedValue;
46
+ }
47
+ const typeOfVal = typeof receivedValue;
48
+ if (ctx.object) {
49
+ if (typeOfVal !== "object")
50
+ throwException("Object", receivedValue, pathToError, "Expected an object but received a different type");
51
+ if (Array.isArray(receivedValue))
52
+ throwException(
53
+ "Object",
54
+ receivedValue,
55
+ pathToError,
56
+ "Expected an object but received an array. Invalid type of data"
57
+ );
58
+ const shapeSchema = ctx.object;
59
+ for (const keyPerReceivedValue of Object.keys(receivedValue)) {
60
+ if (shapeSchema[keyPerReceivedValue] === void 0)
61
+ throwException("Not allowed", keyPerReceivedValue, pathToError, "This key is not allowed in the object");
62
+ }
63
+ for (const [keyOfSchema, valueOfSchema] of Object.entries(shapeSchema)) {
64
+ const receivedObjectValuePropery = receivedValue[keyOfSchema];
65
+ if (receivedObjectValuePropery === void 0) {
66
+ if (!valueOfSchema[ctxSymbol].isOptional)
67
+ throwException(
68
+ "Required",
69
+ receivedObjectValuePropery,
70
+ pathToError,
71
+ "Missing required property in the object"
72
+ );
73
+ }
74
+ innerCheck(valueOfSchema, receivedObjectValuePropery, `${pathToError}.${keyOfSchema}`);
75
+ }
76
+ return receivedValue;
77
+ }
78
+ if (ctx.type.length) {
79
+ if (!ctx.type.includes(typeOfVal)) throwException(ctx.type, typeOfVal, pathToError, "Invalid type of data");
80
+ }
81
+ ctx.requiredValidations.forEach((requiredValidation) => {
82
+ requiredValidation(receivedValue, pathToError);
83
+ });
84
+ return receivedValue;
85
+ }
86
+
87
+ // src/parseSchema.ts
88
+ function parseSchema(schema, receivedValue) {
89
+ try {
90
+ return innerCheck(schema, receivedValue, "");
91
+ } catch (e) {
92
+ if (e instanceof ValidationError) throw e;
93
+ throw new Error("Something unexpected happened");
94
+ }
95
+ }
96
+
97
+ // src/asserts/common/equalTo.ts
98
+ var equalTo = (expected) => (received, pathToError) => {
99
+ if (expected !== received)
100
+ throwException(expected, received, pathToError, "The received value is not equal to expected");
101
+ };
102
+
103
+ // src/schemas/CommonSchema.ts
104
+ var CommonSchema = class {
105
+ [ctxSymbol];
106
+ constructor(ctx) {
107
+ this[ctxSymbol] = ctx;
108
+ }
109
+ /**
110
+ * @param validators - One or more custom validation functions.
111
+ * @returns {this} The schema instance with the added custom validation.
112
+ */
113
+ custom(...validators) {
114
+ this[ctxSymbol].requiredValidations.push(...validators);
115
+ return this;
116
+ }
117
+ /**
118
+ * Marks the schema as nullable, allowing the value to be `null`.
119
+ *
120
+ * @returns {WithNull<this>} The schema instance marked as nullable.
121
+ */
122
+ nullable() {
123
+ this[ctxSymbol].isNullable = true;
124
+ return this;
125
+ }
126
+ /**
127
+ * Marks the schema as optional, allowing the value to be `undefined`.
128
+ *
129
+ * @returns {WithUndefined<this>}} The schema instance marked as optional.
130
+ */
131
+ optional() {
132
+ this[ctxSymbol].isOptional = true;
133
+ return this;
134
+ }
135
+ };
136
+
137
+ // src/schemas/NumberSchema.ts
138
+ var NumberSchema = class extends CommonSchema {
139
+ _number = 1;
140
+ /**
141
+ * Restricts the schema to exactly match the specified value and infers the literal value as the TypeScript type.
142
+ *
143
+ * @param expectedValue - The value that the schema must exactly match.
144
+ * @returns - The schema instance restricted to the specified value, with the literal value inferred as the TypeScript type
145
+ *
146
+ * @example - number().equalTo(42); // Infers the type 42
147
+ */
148
+ equalTo(expectedValue) {
149
+ return this.custom(equalTo(expectedValue));
150
+ }
151
+ };
152
+
153
+ // src/asserts/number/index.ts
154
+ function number() {
155
+ return new NumberSchema({ type: ["number"], requiredValidations: [] });
156
+ }
157
+
158
+ // src/schemas/ArraySchema.ts
159
+ var ArraySchema = class extends CommonSchema {
160
+ _array = 1;
161
+ constructor(ctx, arraySchema) {
162
+ super(ctx);
163
+ this.validateArrayEntry(arraySchema);
164
+ this[ctxSymbol].array = arraySchema;
165
+ }
166
+ validateArrayEntry(arraySchema) {
167
+ if (!arraySchema) throw new BuildSchemaError("Missing schema in array method");
168
+ if (!(arraySchema instanceof CommonSchema)) throw new BuildSchemaError("Invalid schema in array method");
169
+ }
170
+ };
171
+
172
+ // src/asserts/array/index.ts
173
+ function array(arraySchema) {
174
+ return new ArraySchema({ type: [], requiredValidations: [] }, arraySchema);
175
+ }
176
+
177
+ // src/__tests__/ArraySchema.test.ts
178
+ describe("ArraySchema", () => {
179
+ it("should be an array", () => {
180
+ const arrayOfNumbersSchema = array(number());
181
+ expectEqualTypes(true);
182
+ const myArray = [1, 2, 3];
183
+ expect(parseSchema(arrayOfNumbersSchema, myArray)).toBe(myArray);
184
+ expect(() => parseSchema(arrayOfNumbersSchema, 1)).toThrow("Expected an array but received a different type");
185
+ });
186
+ it("should not build with missing parameters", () => {
187
+ const invalidArraySchemaFunction = () => array();
188
+ expectEqualTypes(true);
189
+ expect(invalidArraySchemaFunction).toThrow("Missing schema in array method");
190
+ expect(invalidArraySchemaFunction).toThrow(BuildSchemaError);
191
+ });
192
+ it("should not build with invalid parameters", () => {
193
+ const invalidArraySchemaFunction = () => array("not a bguard schema");
194
+ expectEqualTypes(true);
195
+ expect(invalidArraySchemaFunction).toThrow("Invalid schema in array method");
196
+ expect(invalidArraySchemaFunction).toThrow(BuildSchemaError);
197
+ });
198
+ });
199
+ //# sourceMappingURL=ArraySchema.test.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../jest/setup.ts","../../src/exceptions.ts","../../src/core.ts","../../src/parseSchema.ts","../../src/asserts/common/equalTo.ts","../../src/schemas/CommonSchema.ts","../../src/schemas/NumberSchema.ts","../../src/asserts/number/index.ts","../../src/schemas/ArraySchema.ts","../../src/asserts/array/index.ts","../../src/__tests__/ArraySchema.test.ts"],"sourcesContent":["export type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;\n\nexport function expectEqualTypes<X, Y>(areEqual: Equal<X, Y>): void {\n expect(typeof areEqual).toBe('boolean');\n}\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 { InferType } from './InferType';\nimport { innerCheck } from './core';\nimport { ValidationError } from './exceptions';\nimport { CommonSchema } from './schemas/CommonSchema';\n\n/**\n * Parses and validates a value against the provided schema, returning a type-safe result.\n *\n * This function will throw a `ValidationError` if the value does not conform to the schema.\n * The inferred TypeScript type of the returned value will match the structure defined by the schema.\n *\n * @template T\n * @param {T} schema - The schema to validate the received value against. This schema dictates the expected structure and type of the value.\n * @param {unknown} receivedValue - The value to be validated and parsed according to the schema.\n * @returns {InferType<T>} The validated value, with its TypeScript type inferred from the schema.\n *\n * @throws {ValidationError} If the received value does not match the schema, a `ValidationError` will be thrown.\n * @throws {Error} If an unexpected error occurs during validation, an error will be thrown with a generic message.\n *\n * @example\n * const schema = object({\n * name: string(),\n * age: number(),\n * });\n *\n * const result = parseSchema(schema, { name: 'Alice', age: 30 });\n * // result will be inferred as { name: string; age: number }\n *\n * parseSchema(schema, { name: 'Alice', age: '30' });\n * // Throws ValidationError because 'age' should be a number, not a string.\n */\nexport function parseSchema<T extends CommonSchema>(schema: T, receivedValue: unknown): InferType<T> {\n try {\n return innerCheck(schema, receivedValue, '') as InferType<T>;\n } catch (e) {\n if (e instanceof ValidationError) throw e;\n /* istanbul ignore next */\n throw new Error('Something unexpected happened');\n }\n}\n","import { throwException } from '../../exceptions';\nimport { RequiredValidation } from '../../schemas/CommonSchema';\n\n/**\n * Creates a custom assertion that checks if a value is equal to the expected value.\n *\n * This assertion can be used within a `.custom` method to enforce that a value strictly matches the expected value.\n * It will throw a `ValidationError` if the values are not equal.\n *\n * @param {unknown} expected - The value that the received value is expected to match.\n * @returns {RequiredValidation} A function that takes the received value and the path to the error,\n * and throws a `ValidationError` if the received value does not equal the expected value.\n *\n * @throws {ValidationError} If the received value does not match the expected value.\n *\n * @example\n * import { number, equalTo } from 'bguard';\n *\n * // Define a schema with a custom assertion\n * const schema = number().custom(equalTo(5));\n *\n * // This will pass\n * parseSchema(schema, 5);\n *\n * // This will throw a ValidationError\n * parseSchema(schema, 3);\n */\nexport const equalTo =\n (expected: unknown): RequiredValidation =>\n (received: number, pathToError: string) => {\n if (expected !== received)\n throwException(expected, received, pathToError, 'The received value is not equal to expected');\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 { equalTo } from '../asserts/common/equalTo';\n\nimport { CommonSchema } from './CommonSchema';\n\nexport class NumberSchema extends CommonSchema {\n _number = 1;\n\n /**\n * Restricts the schema to exactly match the specified value and infers the literal value as the TypeScript type.\n *\n * @param expectedValue - The value that the schema must exactly match.\n * @returns - The schema instance restricted to the specified value, with the literal value inferred as the TypeScript type\n *\n * @example - number().equalTo(42); // Infers the type 42\n */\n equalTo<Y extends number>(expectedValue: Y): WithNumber<this, Y> {\n return this.custom(equalTo(expectedValue)) as WithNumber<this, Y>;\n }\n}\n\nexport type WithNumber<T extends NumberSchema, Y = number> = T & { validation_number: Y };\nexport type ExtractFromNumber<T> = T extends WithNumber<NumberSchema, infer Y> ? Y : never;\n","import { NumberSchema } from '../../schemas/NumberSchema';\n\n/**\n * Creates a new schema for validating number values.\n *\n * @returns {NumberSchema} A new instance of `NumberSchema` for validating numbers.\n *\n * @example\n * const schema = number();\n * parseSchema(schema, 42); // Validates successfully\n * parseSchema(schema, '42'); // Throws a validation error\n */\nexport function number(): NumberSchema {\n return new NumberSchema({ type: ['number'], requiredValidations: [] });\n}\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","import { ArraySchema, WithArray } from '../../schemas/ArraySchema';\nimport { CommonSchema } from '../../schemas/CommonSchema';\n\n/**\n * Creates a new schema for validating arrays where each element must match the specified schema.\n *\n * @template T\n * @param {T} arraySchema - The schema that each element of the array must match.\n * @returns {WithArray<T>} A new instance of `ArraySchema` for validating arrays of elements that match the specified schema.\n *\n * @example\n * const schema = array(string());\n * parseSchema(schema, ['hello', 'world']); // Validates successfully\n * parseSchema(schema, ['hello', 123]); // Throws a validation error\n */\nexport function array<T extends CommonSchema>(arraySchema: T): WithArray<T> {\n return new ArraySchema({ type: [], requiredValidations: [] }, arraySchema) as WithArray<T>;\n}\n","import { expectEqualTypes } from '../../jest/setup';\nimport { BuildSchemaError } from '../exceptions';\nimport { parseSchema, InferType, array, number } from '../';\n\ndescribe('ArraySchema', () => {\n it('should be an array', () => {\n const arrayOfNumbersSchema = array(number());\n expectEqualTypes<number[], InferType<typeof arrayOfNumbersSchema>>(true);\n const myArray = [1, 2, 3];\n expect(parseSchema(arrayOfNumbersSchema, myArray)).toBe(myArray);\n expect(() => parseSchema(arrayOfNumbersSchema, 1)).toThrow('Expected an array but received a different type');\n });\n\n it('should not build with missing parameters', () => {\n //@ts-expect-error testing missing parameters in runtime\n const invalidArraySchemaFunction = () => array();\n expectEqualTypes<unknown[], InferType<ReturnType<typeof invalidArraySchemaFunction>>>(true);\n expect(invalidArraySchemaFunction).toThrow('Missing schema in array method');\n expect(invalidArraySchemaFunction).toThrow(BuildSchemaError);\n });\n\n it('should not build with invalid parameters', () => {\n //@ts-expect-error testing invalid schema in runtime\n const invalidArraySchemaFunction = () => array('not a bguard schema');\n expectEqualTypes<unknown[], InferType<ReturnType<typeof invalidArraySchemaFunction>>>(true);\n expect(invalidArraySchemaFunction).toThrow('Invalid schema in array method');\n expect(invalidArraySchemaFunction).toThrow(BuildSchemaError);\n });\n});\n"],"mappings":";AAEO,SAAS,iBAAuB,UAA6B;AAClE,SAAO,OAAO,QAAQ,EAAE,KAAK,SAAS;AACxC;;;ACJO,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;AAEO,IAAM,mBAAN,cAA+B,MAAM;AAAC;AAEtC,SAAS,eAAe,UAAmB,UAAmB,aAAqB,SAAwB;AAChH,QAAM,IAAI,gBAAgB,UAAU,UAAU,aAAa,OAAO;AACpE;;;AClBO,IAAM,YAAY,OAAO,eAAe;AAKxC,SAAS,WAAW,QAAsB,eAAwB,aAAqB;AAC5F,QAAM,MAAM,OAAO,SAAS;AAC5B,MAAI,kBAAkB,QAAW;AAC/B,QAAI,CAAC,IAAI,WAAY,gBAAe,YAAY,eAAe,aAAa,+BAA+B;AAC3G,WAAO;AAAA,EACT;AAEA,MAAI,kBAAkB,MAAM;AAC1B,QAAI,CAAC,IAAI,WAAY,gBAAe,YAAY,eAAe,aAAa,0BAA0B;AACtG,WAAO;AAAA,EACT;AAEA,MAAI,IAAI,OAAO;AACb,QAAI,CAAC,MAAM,QAAQ,aAAa;AAC9B,qB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