bguard 0.0.2 → 0.0.4
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.
- package/lib/CommonSchema-DeTBsQhn.d.mts +57 -0
- package/lib/CommonSchema-DeTBsQhn.d.ts +57 -0
- package/lib/asserts/array/index.d.mts +2 -0
- package/lib/asserts/array/index.d.ts +2 -0
- package/lib/asserts/array/index.js +90 -0
- package/lib/asserts/array/index.js.map +1 -0
- package/lib/asserts/array/index.mjs +63 -0
- package/lib/asserts/array/index.mjs.map +1 -0
- package/lib/asserts/boolean/index.d.mts +2 -0
- package/lib/asserts/boolean/index.d.ts +2 -0
- package/lib/asserts/boolean/index.js +118 -0
- package/lib/asserts/boolean/index.js.map +1 -0
- package/lib/asserts/boolean/index.mjs +91 -0
- package/lib/asserts/boolean/index.mjs.map +1 -0
- package/lib/asserts/common/equalTo.d.mts +29 -0
- package/lib/asserts/common/equalTo.d.ts +29 -0
- package/lib/asserts/common/equalTo.js +54 -0
- package/lib/asserts/common/equalTo.js.map +1 -0
- package/lib/asserts/common/equalTo.mjs +27 -0
- package/lib/asserts/common/equalTo.mjs.map +1 -0
- package/lib/asserts/common/index.d.mts +18 -0
- package/lib/asserts/common/index.d.ts +18 -0
- package/lib/asserts/common/index.js +72 -0
- package/lib/asserts/common/index.js.map +1 -0
- package/lib/asserts/common/index.mjs +45 -0
- package/lib/asserts/common/index.mjs.map +1 -0
- package/lib/asserts/number/index.d.mts +2 -0
- package/lib/asserts/number/index.d.ts +2 -0
- package/lib/asserts/number/index.js +112 -0
- package/lib/asserts/number/index.js.map +1 -0
- package/lib/asserts/number/index.mjs +85 -0
- package/lib/asserts/number/index.mjs.map +1 -0
- package/lib/asserts/number/max.d.mts +17 -0
- package/lib/asserts/number/max.d.ts +17 -0
- package/lib/asserts/number/max.js +54 -0
- package/lib/asserts/number/max.js.map +1 -0
- package/lib/asserts/number/max.mjs +27 -0
- package/lib/asserts/number/max.mjs.map +1 -0
- package/lib/asserts/number/maxExcluded.d.mts +17 -0
- package/lib/asserts/number/maxExcluded.d.ts +17 -0
- package/lib/asserts/number/maxExcluded.js +54 -0
- package/lib/asserts/number/maxExcluded.js.map +1 -0
- package/lib/asserts/number/maxExcluded.mjs +27 -0
- package/lib/asserts/number/maxExcluded.mjs.map +1 -0
- package/lib/asserts/number/min.d.mts +17 -0
- package/lib/asserts/number/min.d.ts +17 -0
- package/lib/asserts/number/min.js +54 -0
- package/lib/asserts/number/min.js.map +1 -0
- package/lib/asserts/number/min.mjs +27 -0
- package/lib/asserts/number/min.mjs.map +1 -0
- package/lib/asserts/number/minExcluded.d.mts +17 -0
- package/lib/asserts/number/minExcluded.d.ts +17 -0
- package/lib/asserts/number/minExcluded.js +54 -0
- package/lib/asserts/number/minExcluded.js.map +1 -0
- package/lib/asserts/number/minExcluded.mjs +27 -0
- package/lib/asserts/number/minExcluded.mjs.map +1 -0
- package/lib/asserts/number/negative.d.mts +16 -0
- package/lib/asserts/number/negative.d.ts +16 -0
- package/lib/asserts/number/negative.js +53 -0
- package/lib/asserts/number/negative.js.map +1 -0
- package/lib/asserts/number/negative.mjs +26 -0
- package/lib/asserts/number/negative.mjs.map +1 -0
- package/lib/asserts/number/positive.d.mts +16 -0
- package/lib/asserts/number/positive.d.ts +16 -0
- package/lib/asserts/number/positive.js +53 -0
- package/lib/asserts/number/positive.js.map +1 -0
- package/lib/asserts/number/positive.mjs +26 -0
- package/lib/asserts/number/positive.mjs.map +1 -0
- package/lib/asserts/object/index.d.mts +2 -0
- package/lib/asserts/object/index.d.ts +2 -0
- package/lib/asserts/object/index.js +94 -0
- package/lib/asserts/object/index.js.map +1 -0
- package/lib/asserts/object/index.mjs +67 -0
- package/lib/asserts/object/index.mjs.map +1 -0
- package/lib/asserts/string/email.d.mts +16 -0
- package/lib/asserts/string/email.d.ts +16 -0
- package/lib/asserts/string/email.js +55 -0
- package/lib/asserts/string/email.js.map +1 -0
- package/lib/asserts/string/email.mjs +28 -0
- package/lib/asserts/string/email.mjs.map +1 -0
- package/lib/asserts/string/index.d.mts +2 -0
- package/lib/asserts/string/index.d.ts +2 -0
- package/lib/asserts/string/index.js +112 -0
- package/lib/asserts/string/index.js.map +1 -0
- package/lib/asserts/string/index.mjs +85 -0
- package/lib/asserts/string/index.mjs.map +1 -0
- package/lib/asserts/string/maxLength.d.mts +16 -0
- package/lib/asserts/string/maxLength.d.ts +16 -0
- package/lib/asserts/string/maxLength.js +54 -0
- package/lib/asserts/string/maxLength.js.map +1 -0
- package/lib/asserts/string/maxLength.mjs +27 -0
- package/lib/asserts/string/maxLength.mjs.map +1 -0
- package/lib/asserts/string/minLength.d.mts +16 -0
- package/lib/asserts/string/minLength.d.ts +16 -0
- package/lib/asserts/string/minLength.js +54 -0
- package/lib/asserts/string/minLength.js.map +1 -0
- package/lib/asserts/string/minLength.mjs +27 -0
- package/lib/asserts/string/minLength.mjs.map +1 -0
- package/lib/asserts/string/regExp.d.mts +16 -0
- package/lib/asserts/string/regExp.d.ts +16 -0
- package/lib/asserts/string/regExp.js +54 -0
- package/lib/asserts/string/regExp.js.map +1 -0
- package/lib/asserts/string/regExp.mjs +27 -0
- package/lib/asserts/string/regExp.mjs.map +1 -0
- package/lib/index-B1yBez0g.d.ts +39 -0
- package/lib/index-B63cXDOm.d.mts +32 -0
- package/lib/index-BEyBEBSm.d.ts +32 -0
- package/lib/index-CK_g_aEC.d.ts +32 -0
- package/lib/index-D3R2euaT.d.mts +29 -0
- package/lib/index-DPkImfCy.d.ts +29 -0
- package/lib/index-DitgsWDO.d.ts +27 -0
- package/lib/index-T3KBmyyn.d.mts +27 -0
- package/lib/index-aBEYpJdZ.d.mts +32 -0
- package/lib/index-nzqBrOHf.d.mts +39 -0
- package/lib/index.d.mts +52 -0
- package/lib/index.d.ts +52 -0
- package/lib/index.js +295 -0
- package/lib/index.js.map +1 -0
- package/lib/index.mjs +262 -0
- package/lib/index.mjs.map +1 -0
- package/package.json +39 -3
|
@@ -0,0 +1,54 @@
|
|
|
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/asserts/common/equalTo.ts
|
|
21
|
+
var equalTo_exports = {};
|
|
22
|
+
__export(equalTo_exports, {
|
|
23
|
+
equalTo: () => equalTo
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(equalTo_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/asserts/common/equalTo.ts
|
|
46
|
+
var equalTo = (expected) => (received, pathToError) => {
|
|
47
|
+
if (expected !== received)
|
|
48
|
+
throwException(expected, received, pathToError, "The received value is not equal to expected");
|
|
49
|
+
};
|
|
50
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
51
|
+
0 && (module.exports = {
|
|
52
|
+
equalTo
|
|
53
|
+
});
|
|
54
|
+
//# sourceMappingURL=equalTo.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/asserts/common/equalTo.ts","../../../src/exceptions.ts"],"sourcesContent":["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","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"],"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;;;ADSO,IAAM,UACX,CAAC,aACD,CAAC,UAAkB,gBAAwB;AACzC,MAAI,aAAa;AACf,mBAAe,UAAU,UAAU,aAAa,6CAA6C;AACjG;","names":[]}
|
|
@@ -0,0 +1,27 @@
|
|
|
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/asserts/common/equalTo.ts
|
|
20
|
+
var equalTo = (expected) => (received, pathToError) => {
|
|
21
|
+
if (expected !== received)
|
|
22
|
+
throwException(expected, received, pathToError, "The received value is not equal to expected");
|
|
23
|
+
};
|
|
24
|
+
export {
|
|
25
|
+
equalTo
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=equalTo.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/exceptions.ts","../../../src/asserts/common/equalTo.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","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"],"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;;;ACSO,IAAM,UACX,CAAC,aACD,CAAC,UAAkB,gBAAwB;AACzC,MAAI,aAAa;AACf,mBAAe,UAAU,UAAU,aAAa,6CAA6C;AACjG;","names":[]}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { P as PrimitiveType, b as WithMix, M as MapMixTypes } from '../../CommonSchema-DeTBsQhn.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates a new schema for validating values that can match any one of the specified primitive types.
|
|
5
|
+
*
|
|
6
|
+
* @template T
|
|
7
|
+
* @param {T} valueTypes - An array of primitive types that the value can match.
|
|
8
|
+
* @returns {WithMix<MapMixTypes<T>>} A new schema for validating values that can match any of the specified types.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const schema = oneOfTypes(['string', 'number']);
|
|
12
|
+
* parseSchema(schema, 'hello'); // Validates successfully
|
|
13
|
+
* parseSchema(schema, 42); // Validates successfully
|
|
14
|
+
* parseSchema(schema, true); // Throws a validation error
|
|
15
|
+
*/
|
|
16
|
+
declare function oneOfTypes<T extends PrimitiveType[]>(valueTypes: T): WithMix<MapMixTypes<T>>;
|
|
17
|
+
|
|
18
|
+
export { oneOfTypes };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { P as PrimitiveType, b as WithMix, M as MapMixTypes } from '../../CommonSchema-DeTBsQhn.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates a new schema for validating values that can match any one of the specified primitive types.
|
|
5
|
+
*
|
|
6
|
+
* @template T
|
|
7
|
+
* @param {T} valueTypes - An array of primitive types that the value can match.
|
|
8
|
+
* @returns {WithMix<MapMixTypes<T>>} A new schema for validating values that can match any of the specified types.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const schema = oneOfTypes(['string', 'number']);
|
|
12
|
+
* parseSchema(schema, 'hello'); // Validates successfully
|
|
13
|
+
* parseSchema(schema, 42); // Validates successfully
|
|
14
|
+
* parseSchema(schema, true); // Throws a validation error
|
|
15
|
+
*/
|
|
16
|
+
declare function oneOfTypes<T extends PrimitiveType[]>(valueTypes: T): WithMix<MapMixTypes<T>>;
|
|
17
|
+
|
|
18
|
+
export { oneOfTypes };
|
|
@@ -0,0 +1,72 @@
|
|
|
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/asserts/common/index.ts
|
|
21
|
+
var common_exports = {};
|
|
22
|
+
__export(common_exports, {
|
|
23
|
+
oneOfTypes: () => oneOfTypes
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(common_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
|
+
|
|
64
|
+
// src/asserts/common/index.ts
|
|
65
|
+
function oneOfTypes(valueTypes) {
|
|
66
|
+
return new CommonSchema({ type: valueTypes, requiredValidations: [] });
|
|
67
|
+
}
|
|
68
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
69
|
+
0 && (module.exports = {
|
|
70
|
+
oneOfTypes
|
|
71
|
+
});
|
|
72
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/asserts/common/index.ts","../../../src/core.ts","../../../src/schemas/CommonSchema.ts"],"sourcesContent":["import { CommonSchema, MapMixTypes, PrimitiveType, WithMix } from '../../schemas/CommonSchema';\n\n/**\n * Creates a new schema for validating values that can match any one of the specified primitive types.\n *\n * @template T\n * @param {T} valueTypes - An array of primitive types that the value can match.\n * @returns {WithMix<MapMixTypes<T>>} A new schema for validating values that can match any of the specified types.\n *\n * @example\n * const schema = oneOfTypes(['string', 'number']);\n * parseSchema(schema, 'hello'); // Validates successfully\n * parseSchema(schema, 42); // Validates successfully\n * parseSchema(schema, true); // Throws a validation error\n */\nexport function oneOfTypes<T extends PrimitiveType[]>(valueTypes: T): WithMix<MapMixTypes<T>> {\n return new CommonSchema({ type: valueTypes, requiredValidations: [] }) as WithMix<MapMixTypes<T>>;\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,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;;;AFnCO,SAAS,WAAsC,YAAwC;AAC5F,SAAO,IAAI,aAAa,EAAE,MAAM,YAAY,qBAAqB,CAAC,EAAE,CAAC;AACvE;","names":[]}
|
|
@@ -0,0 +1,45 @@
|
|
|
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
|
+
|
|
38
|
+
// src/asserts/common/index.ts
|
|
39
|
+
function oneOfTypes(valueTypes) {
|
|
40
|
+
return new CommonSchema({ type: valueTypes, requiredValidations: [] });
|
|
41
|
+
}
|
|
42
|
+
export {
|
|
43
|
+
oneOfTypes
|
|
44
|
+
};
|
|
45
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/core.ts","../../../src/schemas/CommonSchema.ts","../../../src/asserts/common/index.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","import { CommonSchema, MapMixTypes, PrimitiveType, WithMix } from '../../schemas/CommonSchema';\n\n/**\n * Creates a new schema for validating values that can match any one of the specified primitive types.\n *\n * @template T\n * @param {T} valueTypes - An array of primitive types that the value can match.\n * @returns {WithMix<MapMixTypes<T>>} A new schema for validating values that can match any of the specified types.\n *\n * @example\n * const schema = oneOfTypes(['string', 'number']);\n * parseSchema(schema, 'hello'); // Validates successfully\n * parseSchema(schema, 42); // Validates successfully\n * parseSchema(schema, true); // Throws a validation error\n */\nexport function oneOfTypes<T extends PrimitiveType[]>(valueTypes: T): WithMix<MapMixTypes<T>> {\n return new CommonSchema({ type: valueTypes, requiredValidations: [] }) as WithMix<MapMixTypes<T>>;\n}\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;;;ACnCO,SAAS,WAAsC,YAAwC;AAC5F,SAAO,IAAI,aAAa,EAAE,MAAM,YAAY,qBAAqB,CAAC,EAAE,CAAC;AACvE;","names":[]}
|
|
@@ -0,0 +1,112 @@
|
|
|
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/asserts/number/index.ts
|
|
21
|
+
var number_exports = {};
|
|
22
|
+
__export(number_exports, {
|
|
23
|
+
number: () => number
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(number_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/asserts/common/equalTo.ts
|
|
46
|
+
var equalTo = (expected) => (received, pathToError) => {
|
|
47
|
+
if (expected !== received)
|
|
48
|
+
throwException(expected, received, pathToError, "The received value is not equal to expected");
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// src/core.ts
|
|
52
|
+
var ctxSymbol = Symbol("contextSymbol");
|
|
53
|
+
|
|
54
|
+
// src/schemas/CommonSchema.ts
|
|
55
|
+
var CommonSchema = class {
|
|
56
|
+
[ctxSymbol];
|
|
57
|
+
constructor(ctx) {
|
|
58
|
+
this[ctxSymbol] = ctx;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* @param validators - One or more custom validation functions.
|
|
62
|
+
* @returns {this} The schema instance with the added custom validation.
|
|
63
|
+
*/
|
|
64
|
+
custom(...validators) {
|
|
65
|
+
this[ctxSymbol].requiredValidations.push(...validators);
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Marks the schema as nullable, allowing the value to be `null`.
|
|
70
|
+
*
|
|
71
|
+
* @returns {WithNull<this>} The schema instance marked as nullable.
|
|
72
|
+
*/
|
|
73
|
+
nullable() {
|
|
74
|
+
this[ctxSymbol].isNullable = true;
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Marks the schema as optional, allowing the value to be `undefined`.
|
|
79
|
+
*
|
|
80
|
+
* @returns {WithUndefined<this>}} The schema instance marked as optional.
|
|
81
|
+
*/
|
|
82
|
+
optional() {
|
|
83
|
+
this[ctxSymbol].isOptional = true;
|
|
84
|
+
return this;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// src/schemas/NumberSchema.ts
|
|
89
|
+
var NumberSchema = class extends CommonSchema {
|
|
90
|
+
_number = 1;
|
|
91
|
+
/**
|
|
92
|
+
* Restricts the schema to exactly match the specified value and infers the literal value as the TypeScript type.
|
|
93
|
+
*
|
|
94
|
+
* @param expectedValue - The value that the schema must exactly match.
|
|
95
|
+
* @returns - The schema instance restricted to the specified value, with the literal value inferred as the TypeScript type
|
|
96
|
+
*
|
|
97
|
+
* @example - number().equalTo(42); // Infers the type 42
|
|
98
|
+
*/
|
|
99
|
+
equalTo(expectedValue) {
|
|
100
|
+
return this.custom(equalTo(expectedValue));
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
// src/asserts/number/index.ts
|
|
105
|
+
function number() {
|
|
106
|
+
return new NumberSchema({ type: ["number"], requiredValidations: [] });
|
|
107
|
+
}
|
|
108
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
109
|
+
0 && (module.exports = {
|
|
110
|
+
number
|
|
111
|
+
});
|
|
112
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/asserts/number/index.ts","../../../src/exceptions.ts","../../../src/asserts/common/equalTo.ts","../../../src/core.ts","../../../src/schemas/CommonSchema.ts","../../../src/schemas/NumberSchema.ts"],"sourcesContent":["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","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","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","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 { 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"],"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;;;ACSO,IAAM,UACX,CAAC,aACD,CAAC,UAAkB,gBAAwB;AACzC,MAAI,aAAa;AACf,mBAAe,UAAU,UAAU,aAAa,6CAA6C;AACjG;;;AChCK,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,eAAN,cAA2B,aAAa;AAAA,EAC7C,UAAU;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUV,QAA0B,eAAuC;AAC/D,WAAO,KAAK,OAAO,QAAQ,aAAa,CAAC;AAAA,EAC3C;AACF;;;ALNO,SAAS,SAAuB;AACrC,SAAO,IAAI,aAAa,EAAE,MAAM,CAAC,QAAQ,GAAG,qBAAqB,CAAC,EAAE,CAAC;AACvE;","names":[]}
|
|
@@ -0,0 +1,85 @@
|
|
|
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/asserts/common/equalTo.ts
|
|
20
|
+
var equalTo = (expected) => (received, pathToError) => {
|
|
21
|
+
if (expected !== received)
|
|
22
|
+
throwException(expected, received, pathToError, "The received value is not equal to expected");
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// src/core.ts
|
|
26
|
+
var ctxSymbol = Symbol("contextSymbol");
|
|
27
|
+
|
|
28
|
+
// src/schemas/CommonSchema.ts
|
|
29
|
+
var CommonSchema = class {
|
|
30
|
+
[ctxSymbol];
|
|
31
|
+
constructor(ctx) {
|
|
32
|
+
this[ctxSymbol] = ctx;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* @param validators - One or more custom validation functions.
|
|
36
|
+
* @returns {this} The schema instance with the added custom validation.
|
|
37
|
+
*/
|
|
38
|
+
custom(...validators) {
|
|
39
|
+
this[ctxSymbol].requiredValidations.push(...validators);
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Marks the schema as nullable, allowing the value to be `null`.
|
|
44
|
+
*
|
|
45
|
+
* @returns {WithNull<this>} The schema instance marked as nullable.
|
|
46
|
+
*/
|
|
47
|
+
nullable() {
|
|
48
|
+
this[ctxSymbol].isNullable = true;
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Marks the schema as optional, allowing the value to be `undefined`.
|
|
53
|
+
*
|
|
54
|
+
* @returns {WithUndefined<this>}} The schema instance marked as optional.
|
|
55
|
+
*/
|
|
56
|
+
optional() {
|
|
57
|
+
this[ctxSymbol].isOptional = true;
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
// src/schemas/NumberSchema.ts
|
|
63
|
+
var NumberSchema = class extends CommonSchema {
|
|
64
|
+
_number = 1;
|
|
65
|
+
/**
|
|
66
|
+
* Restricts the schema to exactly match the specified value and infers the literal value as the TypeScript type.
|
|
67
|
+
*
|
|
68
|
+
* @param expectedValue - The value that the schema must exactly match.
|
|
69
|
+
* @returns - The schema instance restricted to the specified value, with the literal value inferred as the TypeScript type
|
|
70
|
+
*
|
|
71
|
+
* @example - number().equalTo(42); // Infers the type 42
|
|
72
|
+
*/
|
|
73
|
+
equalTo(expectedValue) {
|
|
74
|
+
return this.custom(equalTo(expectedValue));
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// src/asserts/number/index.ts
|
|
79
|
+
function number() {
|
|
80
|
+
return new NumberSchema({ type: ["number"], requiredValidations: [] });
|
|
81
|
+
}
|
|
82
|
+
export {
|
|
83
|
+
number
|
|
84
|
+
};
|
|
85
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/exceptions.ts","../../../src/asserts/common/equalTo.ts","../../../src/core.ts","../../../src/schemas/CommonSchema.ts","../../../src/schemas/NumberSchema.ts","../../../src/asserts/number/index.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","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","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 { 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"],"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;;;ACSO,IAAM,UACX,CAAC,aACD,CAAC,UAAkB,gBAAwB;AACzC,MAAI,aAAa;AACf,mBAAe,UAAU,UAAU,aAAa,6CAA6C;AACjG;;;AChCK,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,eAAN,cAA2B,aAAa;AAAA,EAC7C,UAAU;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUV,QAA0B,eAAuC;AAC/D,WAAO,KAAK,OAAO,QAAQ,aAAa,CAAC;AAAA,EAC3C;AACF;;;ACNO,SAAS,SAAuB;AACrC,SAAO,IAAI,aAAa,EAAE,MAAM,CAAC,QAAQ,GAAG,qBAAqB,CAAC,EAAE,CAAC;AACvE;","names":[]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { R as RequiredValidation } from '../../CommonSchema-DeTBsQhn.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Asserts that a number value does not exceed a specified maximum value.
|
|
5
|
+
*
|
|
6
|
+
* @param {number} expected - The maximum allowable value.
|
|
7
|
+
* @returns {RequiredValidation} - A validation function that takes a received number and a path to the error message. Throws an error if the received value exceeds the expected maximum value.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const schema = number().custom(max(100));
|
|
11
|
+
* parseSchema(schema, 99); // Valid
|
|
12
|
+
* parseSchema(schema, 100); // Valid
|
|
13
|
+
* parseSchema(schema, 101); // Throws an error: 'The received value is greater than expected'
|
|
14
|
+
*/
|
|
15
|
+
declare const max: (expected: number) => RequiredValidation;
|
|
16
|
+
|
|
17
|
+
export { max };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { R as RequiredValidation } from '../../CommonSchema-DeTBsQhn.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Asserts that a number value does not exceed a specified maximum value.
|
|
5
|
+
*
|
|
6
|
+
* @param {number} expected - The maximum allowable value.
|
|
7
|
+
* @returns {RequiredValidation} - A validation function that takes a received number and a path to the error message. Throws an error if the received value exceeds the expected maximum value.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const schema = number().custom(max(100));
|
|
11
|
+
* parseSchema(schema, 99); // Valid
|
|
12
|
+
* parseSchema(schema, 100); // Valid
|
|
13
|
+
* parseSchema(schema, 101); // Throws an error: 'The received value is greater than expected'
|
|
14
|
+
*/
|
|
15
|
+
declare const max: (expected: number) => RequiredValidation;
|
|
16
|
+
|
|
17
|
+
export { max };
|
|
@@ -0,0 +1,54 @@
|
|
|
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/asserts/number/max.ts
|
|
21
|
+
var max_exports = {};
|
|
22
|
+
__export(max_exports, {
|
|
23
|
+
max: () => max
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(max_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/asserts/number/max.ts
|
|
46
|
+
var max = (expected) => (received, pathToError) => {
|
|
47
|
+
if (expected < received)
|
|
48
|
+
throwException(expected, received, pathToError, "The received value is greater than expected");
|
|
49
|
+
};
|
|
50
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
51
|
+
0 && (module.exports = {
|
|
52
|
+
max
|
|
53
|
+
});
|
|
54
|
+
//# sourceMappingURL=max.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/asserts/number/max.ts","../../../src/exceptions.ts"],"sourcesContent":["import { throwException } from '../../exceptions';\nimport type { RequiredValidation } from '../../schemas/CommonSchema';\n\n/**\n * Asserts that a number value does not exceed a specified maximum value.\n *\n * @param {number} expected - The maximum allowable value.\n * @returns {RequiredValidation} - A validation function that takes a received number and a path to the error message. Throws an error if the received value exceeds the expected maximum value.\n *\n * @example\n * const schema = number().custom(max(100));\n * parseSchema(schema, 99); // Valid\n * parseSchema(schema, 100); // Valid\n * parseSchema(schema, 101); // Throws an error: 'The received value is greater than expected'\n */\nexport const max =\n (expected: number): RequiredValidation =>\n (received: number, pathToError: string) => {\n if (expected < received)\n throwException(expected, received, pathToError, 'The received value is greater than expected');\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"],"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;;;ADHO,IAAM,MACX,CAAC,aACD,CAAC,UAAkB,gBAAwB;AACzC,MAAI,WAAW;AACb,mBAAe,UAAU,UAAU,aAAa,6CAA6C;AACjG;","names":[]}
|