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.
- package/lib/{CommonSchema-DeTBsQhn.d.mts → CommonSchema-BIe85W_A.d.mts} +3 -1
- package/lib/{CommonSchema-DeTBsQhn.d.ts → CommonSchema-BIe85W_A.d.ts} +3 -1
- package/lib/InferType.d.mts +18 -0
- package/lib/InferType.d.ts +18 -0
- package/lib/InferType.js +19 -0
- package/lib/InferType.js.map +1 -0
- package/lib/InferType.mjs +1 -0
- package/lib/InferType.mjs.map +1 -0
- package/lib/__tests__/ArraySchema.test.d.mts +2 -0
- package/lib/__tests__/ArraySchema.test.d.ts +2 -0
- package/lib/__tests__/ArraySchema.test.js +201 -0
- package/lib/__tests__/ArraySchema.test.js.map +1 -0
- package/lib/__tests__/ArraySchema.test.mjs +199 -0
- package/lib/__tests__/ArraySchema.test.mjs.map +1 -0
- package/lib/__tests__/BooleanSchema.test.d.mts +2 -0
- package/lib/__tests__/BooleanSchema.test.d.ts +2 -0
- package/lib/__tests__/BooleanSchema.test.js +219 -0
- package/lib/__tests__/BooleanSchema.test.js.map +1 -0
- package/lib/__tests__/BooleanSchema.test.mjs +217 -0
- package/lib/__tests__/BooleanSchema.test.mjs.map +1 -0
- package/lib/__tests__/CommonSchema.test.d.mts +2 -0
- package/lib/__tests__/CommonSchema.test.d.ts +2 -0
- package/lib/__tests__/CommonSchema.test.js +253 -0
- package/lib/__tests__/CommonSchema.test.js.map +1 -0
- package/lib/__tests__/CommonSchema.test.mjs +251 -0
- package/lib/__tests__/CommonSchema.test.mjs.map +1 -0
- package/lib/__tests__/MixTypes.test.d.mts +2 -0
- package/lib/__tests__/MixTypes.test.d.ts +2 -0
- package/lib/__tests__/MixTypes.test.js +165 -0
- package/lib/__tests__/MixTypes.test.js.map +1 -0
- package/lib/__tests__/MixTypes.test.mjs +163 -0
- package/lib/__tests__/MixTypes.test.mjs.map +1 -0
- package/lib/__tests__/NumberSchema.test.d.mts +2 -0
- package/lib/__tests__/NumberSchema.test.d.ts +2 -0
- package/lib/__tests__/NumberSchema.test.js +253 -0
- package/lib/__tests__/NumberSchema.test.js.map +1 -0
- package/lib/__tests__/NumberSchema.test.mjs +251 -0
- package/lib/__tests__/NumberSchema.test.mjs.map +1 -0
- package/lib/__tests__/ObjectSchema.test.d.mts +2 -0
- package/lib/__tests__/ObjectSchema.test.d.ts +2 -0
- package/lib/__tests__/ObjectSchema.test.js +356 -0
- package/lib/__tests__/ObjectSchema.test.js.map +1 -0
- package/lib/__tests__/ObjectSchema.test.mjs +354 -0
- package/lib/__tests__/ObjectSchema.test.mjs.map +1 -0
- package/lib/__tests__/StringSchema.test.d.mts +2 -0
- package/lib/__tests__/StringSchema.test.d.ts +2 -0
- package/lib/__tests__/StringSchema.test.js +228 -0
- package/lib/__tests__/StringSchema.test.js.map +1 -0
- package/lib/__tests__/StringSchema.test.mjs +226 -0
- package/lib/__tests__/StringSchema.test.mjs.map +1 -0
- package/lib/asserts/array/index.d.mts +18 -2
- package/lib/asserts/array/index.d.ts +18 -2
- package/lib/asserts/boolean/index.d.mts +16 -2
- package/lib/asserts/boolean/index.d.ts +16 -2
- package/lib/asserts/common/equalTo.d.mts +1 -1
- package/lib/asserts/common/equalTo.d.ts +1 -1
- package/lib/asserts/common/index.d.mts +1 -1
- package/lib/asserts/common/index.d.ts +1 -1
- package/lib/asserts/number/index.d.mts +16 -2
- package/lib/asserts/number/index.d.ts +16 -2
- package/lib/asserts/number/max.d.mts +1 -1
- package/lib/asserts/number/max.d.ts +1 -1
- package/lib/asserts/number/maxExcluded.d.mts +1 -1
- package/lib/asserts/number/maxExcluded.d.ts +1 -1
- package/lib/asserts/number/min.d.mts +1 -1
- package/lib/asserts/number/min.d.ts +1 -1
- package/lib/asserts/number/minExcluded.d.mts +1 -1
- package/lib/asserts/number/minExcluded.d.ts +1 -1
- package/lib/asserts/number/negative.d.mts +1 -1
- package/lib/asserts/number/negative.d.ts +1 -1
- package/lib/asserts/number/positive.d.mts +1 -1
- package/lib/asserts/number/positive.d.ts +1 -1
- package/lib/asserts/object/index.d.mts +21 -2
- package/lib/asserts/object/index.d.ts +21 -2
- package/lib/asserts/string/email.d.mts +1 -1
- package/lib/asserts/string/email.d.ts +1 -1
- package/lib/asserts/string/index.d.mts +16 -2
- package/lib/asserts/string/index.d.ts +16 -2
- package/lib/asserts/string/maxLength.d.mts +1 -1
- package/lib/asserts/string/maxLength.d.ts +1 -1
- package/lib/asserts/string/minLength.d.mts +1 -1
- package/lib/asserts/string/minLength.d.ts +1 -1
- package/lib/asserts/string/regExp.d.mts +1 -1
- package/lib/asserts/string/regExp.d.ts +1 -1
- package/lib/core.d.mts +1 -0
- package/lib/core.d.ts +1 -0
- package/lib/core.js +111 -0
- package/lib/core.js.map +1 -0
- package/lib/core.mjs +83 -0
- package/lib/core.mjs.map +1 -0
- package/lib/exceptions.d.mts +12 -0
- package/lib/exceptions.d.ts +12 -0
- package/lib/exceptions.js +52 -0
- package/lib/exceptions.js.map +1 -0
- package/lib/exceptions.mjs +25 -0
- package/lib/exceptions.mjs.map +1 -0
- package/lib/index.d.mts +14 -51
- package/lib/index.d.ts +14 -51
- package/lib/index.js +8 -2
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +4 -1
- package/lib/parseSchema.d.mts +37 -0
- package/lib/parseSchema.d.ts +37 -0
- package/lib/parseSchema.js +119 -0
- package/lib/parseSchema.js.map +1 -0
- package/lib/parseSchema.mjs +92 -0
- package/lib/parseSchema.mjs.map +1 -0
- package/lib/schemas/ArraySchema.d.mts +13 -0
- package/lib/schemas/ArraySchema.d.ts +13 -0
- package/lib/schemas/ArraySchema.js +85 -0
- package/lib/schemas/ArraySchema.js.map +1 -0
- package/lib/schemas/ArraySchema.mjs +58 -0
- package/lib/schemas/ArraySchema.mjs.map +1 -0
- package/lib/{index-nzqBrOHf.d.mts → schemas/BooleanSchema.d.mts} +2 -14
- package/lib/{index-B1yBez0g.d.ts → schemas/BooleanSchema.d.ts} +2 -14
- package/lib/schemas/BooleanSchema.js +113 -0
- package/lib/schemas/BooleanSchema.js.map +1 -0
- package/lib/schemas/BooleanSchema.mjs +86 -0
- package/lib/schemas/BooleanSchema.mjs.map +1 -0
- package/lib/schemas/CommonSchema.d.mts +1 -0
- package/lib/schemas/CommonSchema.d.ts +1 -0
- package/lib/schemas/CommonSchema.js +67 -0
- package/lib/schemas/CommonSchema.js.map +1 -0
- package/lib/schemas/CommonSchema.mjs +40 -0
- package/lib/schemas/CommonSchema.mjs.map +1 -0
- package/lib/{index-B63cXDOm.d.mts → schemas/NumberSchema.d.mts} +2 -14
- package/lib/{index-CK_g_aEC.d.ts → schemas/NumberSchema.d.ts} +2 -14
- package/lib/schemas/NumberSchema.js +107 -0
- package/lib/schemas/NumberSchema.js.map +1 -0
- package/lib/schemas/NumberSchema.mjs +80 -0
- package/lib/schemas/NumberSchema.mjs.map +1 -0
- package/lib/schemas/ObjectSchema.d.mts +12 -0
- package/lib/schemas/ObjectSchema.d.ts +12 -0
- package/lib/schemas/ObjectSchema.js +89 -0
- package/lib/schemas/ObjectSchema.js.map +1 -0
- package/lib/schemas/ObjectSchema.mjs +62 -0
- package/lib/schemas/ObjectSchema.mjs.map +1 -0
- package/lib/{index-aBEYpJdZ.d.mts → schemas/StringSchema.d.mts} +2 -14
- package/lib/{index-BEyBEBSm.d.ts → schemas/StringSchema.d.ts} +2 -14
- package/lib/schemas/StringSchema.js +107 -0
- package/lib/schemas/StringSchema.js.map +1 -0
- package/lib/schemas/StringSchema.mjs +80 -0
- package/lib/schemas/StringSchema.mjs.map +1 -0
- package/package.json +28 -27
- package/lib/index-D3R2euaT.d.mts +0 -29
- package/lib/index-DPkImfCy.d.ts +0 -29
- package/lib/index-DitgsWDO.d.ts +0 -27
- package/lib/index-T3KBmyyn.d.mts +0 -27
|
@@ -1,2 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
import '../../CommonSchema-
|
|
1
|
+
import { StringSchema } from '../../schemas/StringSchema.mjs';
|
|
2
|
+
import '../../CommonSchema-BIe85W_A.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Creates a new schema for validating string values.
|
|
6
|
+
*
|
|
7
|
+
* @returns {StringSchema} A new instance of `StringSchema` for validating strings.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const schema = string();
|
|
11
|
+
* parseSchema(schema, 'hello'); // Validates successfully
|
|
12
|
+
* parseSchema(schema, 123); // Throws a validation error
|
|
13
|
+
*/
|
|
14
|
+
declare function string(): StringSchema;
|
|
15
|
+
|
|
16
|
+
export { string };
|
|
@@ -1,2 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
import '../../CommonSchema-
|
|
1
|
+
import { StringSchema } from '../../schemas/StringSchema.js';
|
|
2
|
+
import '../../CommonSchema-BIe85W_A.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Creates a new schema for validating string values.
|
|
6
|
+
*
|
|
7
|
+
* @returns {StringSchema} A new instance of `StringSchema` for validating strings.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const schema = string();
|
|
11
|
+
* parseSchema(schema, 'hello'); // Validates successfully
|
|
12
|
+
* parseSchema(schema, 123); // Throws a validation error
|
|
13
|
+
*/
|
|
14
|
+
declare function string(): StringSchema;
|
|
15
|
+
|
|
16
|
+
export { string };
|
package/lib/core.d.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { c as ctxSymbol, i as innerCheck } from './CommonSchema-BIe85W_A.mjs';
|
package/lib/core.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { c as ctxSymbol, i as innerCheck } from './CommonSchema-BIe85W_A.js';
|
package/lib/core.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
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/core.ts
|
|
21
|
+
var core_exports = {};
|
|
22
|
+
__export(core_exports, {
|
|
23
|
+
ctxSymbol: () => ctxSymbol,
|
|
24
|
+
innerCheck: () => innerCheck
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(core_exports);
|
|
27
|
+
|
|
28
|
+
// src/exceptions.ts
|
|
29
|
+
var ValidationError = class extends Error {
|
|
30
|
+
expected;
|
|
31
|
+
received;
|
|
32
|
+
pathToError;
|
|
33
|
+
message;
|
|
34
|
+
constructor(expected, received, pathToError, message) {
|
|
35
|
+
super();
|
|
36
|
+
this.expected = expected;
|
|
37
|
+
this.received = received;
|
|
38
|
+
this.pathToError = pathToError;
|
|
39
|
+
this.message = message;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
function throwException(expected, received, pathToError, message) {
|
|
43
|
+
throw new ValidationError(expected, received, pathToError, message);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// src/core.ts
|
|
47
|
+
var ctxSymbol = Symbol("contextSymbol");
|
|
48
|
+
function innerCheck(schema, receivedValue, pathToError) {
|
|
49
|
+
const ctx = schema[ctxSymbol];
|
|
50
|
+
if (receivedValue === void 0) {
|
|
51
|
+
if (!ctx.isOptional) throwException("Required", receivedValue, pathToError, "The required value is missing");
|
|
52
|
+
return receivedValue;
|
|
53
|
+
}
|
|
54
|
+
if (receivedValue === null) {
|
|
55
|
+
if (!ctx.isNullable) throwException("Not null", receivedValue, pathToError, "Value should not be null");
|
|
56
|
+
return receivedValue;
|
|
57
|
+
}
|
|
58
|
+
if (ctx.array) {
|
|
59
|
+
if (!Array.isArray(receivedValue))
|
|
60
|
+
throwException("Array", receivedValue, pathToError, "Expected an array but received a different type");
|
|
61
|
+
const schema2 = ctx.array;
|
|
62
|
+
receivedValue.forEach((elem, i) => {
|
|
63
|
+
innerCheck(schema2, elem, `${pathToError}[${i}]`);
|
|
64
|
+
});
|
|
65
|
+
return receivedValue;
|
|
66
|
+
}
|
|
67
|
+
const typeOfVal = typeof receivedValue;
|
|
68
|
+
if (ctx.object) {
|
|
69
|
+
if (typeOfVal !== "object")
|
|
70
|
+
throwException("Object", receivedValue, pathToError, "Expected an object but received a different type");
|
|
71
|
+
if (Array.isArray(receivedValue))
|
|
72
|
+
throwException(
|
|
73
|
+
"Object",
|
|
74
|
+
receivedValue,
|
|
75
|
+
pathToError,
|
|
76
|
+
"Expected an object but received an array. Invalid type of data"
|
|
77
|
+
);
|
|
78
|
+
const shapeSchema = ctx.object;
|
|
79
|
+
for (const keyPerReceivedValue of Object.keys(receivedValue)) {
|
|
80
|
+
if (shapeSchema[keyPerReceivedValue] === void 0)
|
|
81
|
+
throwException("Not allowed", keyPerReceivedValue, pathToError, "This key is not allowed in the object");
|
|
82
|
+
}
|
|
83
|
+
for (const [keyOfSchema, valueOfSchema] of Object.entries(shapeSchema)) {
|
|
84
|
+
const receivedObjectValuePropery = receivedValue[keyOfSchema];
|
|
85
|
+
if (receivedObjectValuePropery === void 0) {
|
|
86
|
+
if (!valueOfSchema[ctxSymbol].isOptional)
|
|
87
|
+
throwException(
|
|
88
|
+
"Required",
|
|
89
|
+
receivedObjectValuePropery,
|
|
90
|
+
pathToError,
|
|
91
|
+
"Missing required property in the object"
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
innerCheck(valueOfSchema, receivedObjectValuePropery, `${pathToError}.${keyOfSchema}`);
|
|
95
|
+
}
|
|
96
|
+
return receivedValue;
|
|
97
|
+
}
|
|
98
|
+
if (ctx.type.length) {
|
|
99
|
+
if (!ctx.type.includes(typeOfVal)) throwException(ctx.type, typeOfVal, pathToError, "Invalid type of data");
|
|
100
|
+
}
|
|
101
|
+
ctx.requiredValidations.forEach((requiredValidation) => {
|
|
102
|
+
requiredValidation(receivedValue, pathToError);
|
|
103
|
+
});
|
|
104
|
+
return receivedValue;
|
|
105
|
+
}
|
|
106
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
107
|
+
0 && (module.exports = {
|
|
108
|
+
ctxSymbol,
|
|
109
|
+
innerCheck
|
|
110
|
+
});
|
|
111
|
+
//# sourceMappingURL=core.js.map
|
package/lib/core.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/core.ts","../src/exceptions.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","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;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;;;ADlBO,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;AAEzB,MAAI,IAAI,QAAQ;AACd,QAAI,cAAc;AAChB,qBAAe,UAAU,eAAe,aAAa,kDAAkD;AACzG,QAAI,MAAM,QAAQ,aAAa;AAC7B;AAAA,QACE;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACF;AACF,UAAM,cAAc,IAAI;AACxB,eAAW,uBAAuB,OAAO,KAAK,aAAa,GAAG;AAC5D,UAAI,YAAY,mBAAmB,MAAM;AACvC,uBAAe,eAAe,qBAAqB,aAAa,uCAAuC;AAAA,IAC3G;AAEA,eAAW,CAAC,aAAa,aAAa,KAAK,OAAO,QAAQ,WAAW,GAAG;AACtE,YAAM,6BAA8B,cAA0C,WAAW;AACzF,UAAI,+BAA+B,QAAW;AAC5C,YAAI,CAAC,cAAc,SAAS,EAAE;AAC5B;AAAA,YACE;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,UACF;AAAA,MACJ;AACA,iBAAW,eAAe,4BAA4B,GAAG,WAAW,IAAI,WAAW,EAAE;AAAA,IACvF;AAEA,WAAO;AAAA,EACT;AAEA,MAAI,IAAI,KAAK,QAAQ;AACnB,QAAI,CAAC,IAAI,KAAK,SAAS,SAAS,EAAG,gBAAe,IAAI,MAAM,WAAW,aAAa,sBAAsB;AAAA,EAC5G;AAEA,MAAI,oBAAoB,QAAQ,CAAC,uBAAuB;AACtD,uBAAmB,eAAe,WAAW;AAAA,EAC/C,CAAC;AAED,SAAO;AACT;","names":["schema"]}
|
package/lib/core.mjs
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// src/exceptions.ts
|
|
2
|
+
var ValidationError = class extends Error {
|
|
3
|
+
expected;
|
|
4
|
+
received;
|
|
5
|
+
pathToError;
|
|
6
|
+
message;
|
|
7
|
+
constructor(expected, received, pathToError, message) {
|
|
8
|
+
super();
|
|
9
|
+
this.expected = expected;
|
|
10
|
+
this.received = received;
|
|
11
|
+
this.pathToError = pathToError;
|
|
12
|
+
this.message = message;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
function throwException(expected, received, pathToError, message) {
|
|
16
|
+
throw new ValidationError(expected, received, pathToError, message);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// src/core.ts
|
|
20
|
+
var ctxSymbol = Symbol("contextSymbol");
|
|
21
|
+
function innerCheck(schema, receivedValue, pathToError) {
|
|
22
|
+
const ctx = schema[ctxSymbol];
|
|
23
|
+
if (receivedValue === void 0) {
|
|
24
|
+
if (!ctx.isOptional) throwException("Required", receivedValue, pathToError, "The required value is missing");
|
|
25
|
+
return receivedValue;
|
|
26
|
+
}
|
|
27
|
+
if (receivedValue === null) {
|
|
28
|
+
if (!ctx.isNullable) throwException("Not null", receivedValue, pathToError, "Value should not be null");
|
|
29
|
+
return receivedValue;
|
|
30
|
+
}
|
|
31
|
+
if (ctx.array) {
|
|
32
|
+
if (!Array.isArray(receivedValue))
|
|
33
|
+
throwException("Array", receivedValue, pathToError, "Expected an array but received a different type");
|
|
34
|
+
const schema2 = ctx.array;
|
|
35
|
+
receivedValue.forEach((elem, i) => {
|
|
36
|
+
innerCheck(schema2, elem, `${pathToError}[${i}]`);
|
|
37
|
+
});
|
|
38
|
+
return receivedValue;
|
|
39
|
+
}
|
|
40
|
+
const typeOfVal = typeof receivedValue;
|
|
41
|
+
if (ctx.object) {
|
|
42
|
+
if (typeOfVal !== "object")
|
|
43
|
+
throwException("Object", receivedValue, pathToError, "Expected an object but received a different type");
|
|
44
|
+
if (Array.isArray(receivedValue))
|
|
45
|
+
throwException(
|
|
46
|
+
"Object",
|
|
47
|
+
receivedValue,
|
|
48
|
+
pathToError,
|
|
49
|
+
"Expected an object but received an array. Invalid type of data"
|
|
50
|
+
);
|
|
51
|
+
const shapeSchema = ctx.object;
|
|
52
|
+
for (const keyPerReceivedValue of Object.keys(receivedValue)) {
|
|
53
|
+
if (shapeSchema[keyPerReceivedValue] === void 0)
|
|
54
|
+
throwException("Not allowed", keyPerReceivedValue, pathToError, "This key is not allowed in the object");
|
|
55
|
+
}
|
|
56
|
+
for (const [keyOfSchema, valueOfSchema] of Object.entries(shapeSchema)) {
|
|
57
|
+
const receivedObjectValuePropery = receivedValue[keyOfSchema];
|
|
58
|
+
if (receivedObjectValuePropery === void 0) {
|
|
59
|
+
if (!valueOfSchema[ctxSymbol].isOptional)
|
|
60
|
+
throwException(
|
|
61
|
+
"Required",
|
|
62
|
+
receivedObjectValuePropery,
|
|
63
|
+
pathToError,
|
|
64
|
+
"Missing required property in the object"
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
innerCheck(valueOfSchema, receivedObjectValuePropery, `${pathToError}.${keyOfSchema}`);
|
|
68
|
+
}
|
|
69
|
+
return receivedValue;
|
|
70
|
+
}
|
|
71
|
+
if (ctx.type.length) {
|
|
72
|
+
if (!ctx.type.includes(typeOfVal)) throwException(ctx.type, typeOfVal, pathToError, "Invalid type of data");
|
|
73
|
+
}
|
|
74
|
+
ctx.requiredValidations.forEach((requiredValidation) => {
|
|
75
|
+
requiredValidation(receivedValue, pathToError);
|
|
76
|
+
});
|
|
77
|
+
return receivedValue;
|
|
78
|
+
}
|
|
79
|
+
export {
|
|
80
|
+
ctxSymbol,
|
|
81
|
+
innerCheck
|
|
82
|
+
};
|
|
83
|
+
//# sourceMappingURL=core.mjs.map
|
package/lib/core.mjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/exceptions.ts","../src/core.ts"],"sourcesContent":["export class ValidationError extends Error {\n expected: unknown;\n received: unknown;\n pathToError: string;\n message: string;\n constructor(expected: unknown, received: unknown, pathToError: string, message: string) {\n super();\n this.expected = expected;\n this.received = received;\n this.pathToError = pathToError;\n this.message = message;\n }\n}\n\nexport class BuildSchemaError extends Error {}\n\nexport function throwException(expected: unknown, received: unknown, pathToError: string, message: string): never {\n throw new ValidationError(expected, received, pathToError, message);\n}\n","export const ctxSymbol = Symbol('contextSymbol');\n\nimport { throwException } from './exceptions';\nimport type { CommonSchema } from './schemas/CommonSchema';\n\nexport function innerCheck(schema: CommonSchema, receivedValue: unknown, pathToError: string) {\n const ctx = schema[ctxSymbol];\n if (receivedValue === undefined) {\n if (!ctx.isOptional) throwException('Required', receivedValue, pathToError, 'The required value is missing');\n return receivedValue;\n }\n\n if (receivedValue === null) {\n if (!ctx.isNullable) throwException('Not null', receivedValue, pathToError, 'Value should not be null');\n return receivedValue;\n }\n\n if (ctx.array) {\n if (!Array.isArray(receivedValue))\n throwException('Array', receivedValue, pathToError, 'Expected an array but received a different type');\n const schema = ctx.array;\n\n receivedValue.forEach((elem, i) => {\n innerCheck(schema, elem, `${pathToError}[${i}]`);\n });\n\n return receivedValue;\n }\n\n const typeOfVal = typeof receivedValue;\n\n if (ctx.object) {\n if (typeOfVal !== 'object')\n throwException('Object', receivedValue, pathToError, 'Expected an object but received a different type');\n if (Array.isArray(receivedValue))\n throwException(\n 'Object',\n receivedValue,\n pathToError,\n 'Expected an object but received an array. Invalid type of data',\n );\n const shapeSchema = ctx.object;\n for (const keyPerReceivedValue of Object.keys(receivedValue)) {\n if (shapeSchema[keyPerReceivedValue] === undefined)\n throwException('Not allowed', keyPerReceivedValue, pathToError, 'This key is not allowed in the object');\n }\n\n for (const [keyOfSchema, valueOfSchema] of Object.entries(shapeSchema)) {\n const receivedObjectValuePropery = (receivedValue as Record<string, unknown>)[keyOfSchema];\n if (receivedObjectValuePropery === undefined) {\n if (!valueOfSchema[ctxSymbol].isOptional)\n throwException(\n 'Required',\n receivedObjectValuePropery,\n pathToError,\n 'Missing required property in the object',\n );\n }\n innerCheck(valueOfSchema, receivedObjectValuePropery, `${pathToError}.${keyOfSchema}`);\n }\n\n return receivedValue;\n }\n\n if (ctx.type.length) {\n if (!ctx.type.includes(typeOfVal)) throwException(ctx.type, typeOfVal, pathToError, 'Invalid type of data');\n }\n\n ctx.requiredValidations.forEach((requiredValidation) => {\n requiredValidation(receivedValue, pathToError);\n });\n\n return receivedValue;\n}\n"],"mappings":";AAAO,IAAM,kBAAN,cAA8B,MAAM;AAAA,EACzC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,YAAY,UAAmB,UAAmB,aAAqB,SAAiB;AACtF,UAAM;AACN,SAAK,WAAW;AAChB,SAAK,WAAW;AAChB,SAAK,cAAc;AACnB,SAAK,UAAU;AAAA,EACjB;AACF;AAIO,SAAS,eAAe,UAAmB,UAAmB,aAAqB,SAAwB;AAChH,QAAM,IAAI,gBAAgB,UAAU,UAAU,aAAa,OAAO;AACpE;;;AClBO,IAAM,YAAY,OAAO,eAAe;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;AAEzB,MAAI,IAAI,QAAQ;AACd,QAAI,cAAc;AAChB,qBAAe,UAAU,eAAe,aAAa,kDAAkD;AACzG,QAAI,MAAM,QAAQ,aAAa;AAC7B;AAAA,QACE;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACF;AACF,UAAM,cAAc,IAAI;AACxB,eAAW,uBAAuB,OAAO,KAAK,aAAa,GAAG;AAC5D,UAAI,YAAY,mBAAmB,MAAM;AACvC,uBAAe,eAAe,qBAAqB,aAAa,uCAAuC;AAAA,IAC3G;AAEA,eAAW,CAAC,aAAa,aAAa,KAAK,OAAO,QAAQ,WAAW,GAAG;AACtE,YAAM,6BAA8B,cAA0C,WAAW;AACzF,UAAI,+BAA+B,QAAW;AAC5C,YAAI,CAAC,cAAc,SAAS,EAAE;AAC5B;AAAA,YACE;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,UACF;AAAA,MACJ;AACA,iBAAW,eAAe,4BAA4B,GAAG,WAAW,IAAI,WAAW,EAAE;AAAA,IACvF;AAEA,WAAO;AAAA,EACT;AAEA,MAAI,IAAI,KAAK,QAAQ;AACnB,QAAI,CAAC,IAAI,KAAK,SAAS,SAAS,EAAG,gBAAe,IAAI,MAAM,WAAW,aAAa,sBAAsB;AAAA,EAC5G;AAEA,MAAI,oBAAoB,QAAQ,CAAC,uBAAuB;AACtD,uBAAmB,eAAe,WAAW;AAAA,EAC/C,CAAC;AAED,SAAO;AACT;","names":["schema"]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
declare class ValidationError extends Error {
|
|
2
|
+
expected: unknown;
|
|
3
|
+
received: unknown;
|
|
4
|
+
pathToError: string;
|
|
5
|
+
message: string;
|
|
6
|
+
constructor(expected: unknown, received: unknown, pathToError: string, message: string);
|
|
7
|
+
}
|
|
8
|
+
declare class BuildSchemaError extends Error {
|
|
9
|
+
}
|
|
10
|
+
declare function throwException(expected: unknown, received: unknown, pathToError: string, message: string): never;
|
|
11
|
+
|
|
12
|
+
export { BuildSchemaError, ValidationError, throwException };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
declare class ValidationError extends Error {
|
|
2
|
+
expected: unknown;
|
|
3
|
+
received: unknown;
|
|
4
|
+
pathToError: string;
|
|
5
|
+
message: string;
|
|
6
|
+
constructor(expected: unknown, received: unknown, pathToError: string, message: string);
|
|
7
|
+
}
|
|
8
|
+
declare class BuildSchemaError extends Error {
|
|
9
|
+
}
|
|
10
|
+
declare function throwException(expected: unknown, received: unknown, pathToError: string, message: string): never;
|
|
11
|
+
|
|
12
|
+
export { BuildSchemaError, ValidationError, throwException };
|
|
@@ -0,0 +1,52 @@
|
|
|
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/exceptions.ts
|
|
21
|
+
var exceptions_exports = {};
|
|
22
|
+
__export(exceptions_exports, {
|
|
23
|
+
BuildSchemaError: () => BuildSchemaError,
|
|
24
|
+
ValidationError: () => ValidationError,
|
|
25
|
+
throwException: () => throwException
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(exceptions_exports);
|
|
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
|
+
var BuildSchemaError = class extends Error {
|
|
42
|
+
};
|
|
43
|
+
function throwException(expected, received, pathToError, message) {
|
|
44
|
+
throw new ValidationError(expected, received, pathToError, message);
|
|
45
|
+
}
|
|
46
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
47
|
+
0 && (module.exports = {
|
|
48
|
+
BuildSchemaError,
|
|
49
|
+
ValidationError,
|
|
50
|
+
throwException
|
|
51
|
+
});
|
|
52
|
+
//# sourceMappingURL=exceptions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/exceptions.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"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;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;AAEO,IAAM,mBAAN,cAA+B,MAAM;AAAC;AAEtC,SAAS,eAAe,UAAmB,UAAmB,aAAqB,SAAwB;AAChH,QAAM,IAAI,gBAAgB,UAAU,UAAU,aAAa,OAAO;AACpE;","names":[]}
|
|
@@ -0,0 +1,25 @@
|
|
|
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
|
+
var BuildSchemaError = class extends Error {
|
|
16
|
+
};
|
|
17
|
+
function throwException(expected, received, pathToError, message) {
|
|
18
|
+
throw new ValidationError(expected, received, pathToError, message);
|
|
19
|
+
}
|
|
20
|
+
export {
|
|
21
|
+
BuildSchemaError,
|
|
22
|
+
ValidationError,
|
|
23
|
+
throwException
|
|
24
|
+
};
|
|
25
|
+
//# sourceMappingURL=exceptions.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/exceptions.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"],"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;AAEO,IAAM,mBAAN,cAA+B,MAAM;AAAC;AAEtC,SAAS,eAAe,UAAmB,UAAmB,aAAqB,SAAwB;AAChH,QAAM,IAAI,gBAAgB,UAAU,UAAU,aAAa,OAAO;AACpE;","names":[]}
|
package/lib/index.d.mts
CHANGED
|
@@ -1,52 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
export {
|
|
3
|
-
|
|
4
|
-
export {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export {
|
|
8
|
-
import { O as ObjectSchema, W as WithObject } from './index-D3R2euaT.mjs';
|
|
9
|
-
export { o as object } from './index-D3R2euaT.mjs';
|
|
10
|
-
import { W as WithString, S as StringSchema, E as ExtractFromString } from './index-aBEYpJdZ.mjs';
|
|
11
|
-
export { s as string } from './index-aBEYpJdZ.mjs';
|
|
1
|
+
export { InferType } from './InferType.mjs';
|
|
2
|
+
export { parseSchema } from './parseSchema.mjs';
|
|
3
|
+
export { number } from './asserts/number/index.mjs';
|
|
4
|
+
export { string } from './asserts/string/index.mjs';
|
|
5
|
+
export { boolean } from './asserts/boolean/index.mjs';
|
|
6
|
+
export { array } from './asserts/array/index.mjs';
|
|
7
|
+
export { object } from './asserts/object/index.mjs';
|
|
12
8
|
export { oneOfTypes } from './asserts/common/index.mjs';
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
} & {
|
|
21
|
-
[K in keyof X as X[K] extends WithUndefined<CommonSchema> ? K : never]?: InferType<X[K]>;
|
|
22
|
-
}> : unknown;
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Parses and validates a value against the provided schema, returning a type-safe result.
|
|
26
|
-
*
|
|
27
|
-
* This function will throw a `ValidationError` if the value does not conform to the schema.
|
|
28
|
-
* The inferred TypeScript type of the returned value will match the structure defined by the schema.
|
|
29
|
-
*
|
|
30
|
-
* @template T
|
|
31
|
-
* @param {T} schema - The schema to validate the received value against. This schema dictates the expected structure and type of the value.
|
|
32
|
-
* @param {unknown} receivedValue - The value to be validated and parsed according to the schema.
|
|
33
|
-
* @returns {InferType<T>} The validated value, with its TypeScript type inferred from the schema.
|
|
34
|
-
*
|
|
35
|
-
* @throws {ValidationError} If the received value does not match the schema, a `ValidationError` will be thrown.
|
|
36
|
-
* @throws {Error} If an unexpected error occurs during validation, an error will be thrown with a generic message.
|
|
37
|
-
*
|
|
38
|
-
* @example
|
|
39
|
-
* const schema = object({
|
|
40
|
-
* name: string(),
|
|
41
|
-
* age: number(),
|
|
42
|
-
* });
|
|
43
|
-
*
|
|
44
|
-
* const result = parseSchema(schema, { name: 'Alice', age: 30 });
|
|
45
|
-
* // result will be inferred as { name: string; age: number }
|
|
46
|
-
*
|
|
47
|
-
* parseSchema(schema, { name: 'Alice', age: '30' });
|
|
48
|
-
* // Throws ValidationError because 'age' should be a number, not a string.
|
|
49
|
-
*/
|
|
50
|
-
declare function parseSchema<T extends CommonSchema>(schema: T, receivedValue: unknown): InferType<T>;
|
|
51
|
-
|
|
52
|
-
export { type InferType, parseSchema };
|
|
9
|
+
export { BuildSchemaError, ValidationError, throwException } from './exceptions.mjs';
|
|
10
|
+
import './schemas/ArraySchema.mjs';
|
|
11
|
+
import './CommonSchema-BIe85W_A.mjs';
|
|
12
|
+
import './schemas/BooleanSchema.mjs';
|
|
13
|
+
import './schemas/NumberSchema.mjs';
|
|
14
|
+
import './schemas/ObjectSchema.mjs';
|
|
15
|
+
import './schemas/StringSchema.mjs';
|
package/lib/index.d.ts
CHANGED
|
@@ -1,52 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
export {
|
|
3
|
-
|
|
4
|
-
export {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export {
|
|
8
|
-
import { O as ObjectSchema, W as WithObject } from './index-DPkImfCy.js';
|
|
9
|
-
export { o as object } from './index-DPkImfCy.js';
|
|
10
|
-
import { W as WithString, S as StringSchema, E as ExtractFromString } from './index-BEyBEBSm.js';
|
|
11
|
-
export { s as string } from './index-BEyBEBSm.js';
|
|
1
|
+
export { InferType } from './InferType.js';
|
|
2
|
+
export { parseSchema } from './parseSchema.js';
|
|
3
|
+
export { number } from './asserts/number/index.js';
|
|
4
|
+
export { string } from './asserts/string/index.js';
|
|
5
|
+
export { boolean } from './asserts/boolean/index.js';
|
|
6
|
+
export { array } from './asserts/array/index.js';
|
|
7
|
+
export { object } from './asserts/object/index.js';
|
|
12
8
|
export { oneOfTypes } from './asserts/common/index.js';
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
} & {
|
|
21
|
-
[K in keyof X as X[K] extends WithUndefined<CommonSchema> ? K : never]?: InferType<X[K]>;
|
|
22
|
-
}> : unknown;
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Parses and validates a value against the provided schema, returning a type-safe result.
|
|
26
|
-
*
|
|
27
|
-
* This function will throw a `ValidationError` if the value does not conform to the schema.
|
|
28
|
-
* The inferred TypeScript type of the returned value will match the structure defined by the schema.
|
|
29
|
-
*
|
|
30
|
-
* @template T
|
|
31
|
-
* @param {T} schema - The schema to validate the received value against. This schema dictates the expected structure and type of the value.
|
|
32
|
-
* @param {unknown} receivedValue - The value to be validated and parsed according to the schema.
|
|
33
|
-
* @returns {InferType<T>} The validated value, with its TypeScript type inferred from the schema.
|
|
34
|
-
*
|
|
35
|
-
* @throws {ValidationError} If the received value does not match the schema, a `ValidationError` will be thrown.
|
|
36
|
-
* @throws {Error} If an unexpected error occurs during validation, an error will be thrown with a generic message.
|
|
37
|
-
*
|
|
38
|
-
* @example
|
|
39
|
-
* const schema = object({
|
|
40
|
-
* name: string(),
|
|
41
|
-
* age: number(),
|
|
42
|
-
* });
|
|
43
|
-
*
|
|
44
|
-
* const result = parseSchema(schema, { name: 'Alice', age: 30 });
|
|
45
|
-
* // result will be inferred as { name: string; age: number }
|
|
46
|
-
*
|
|
47
|
-
* parseSchema(schema, { name: 'Alice', age: '30' });
|
|
48
|
-
* // Throws ValidationError because 'age' should be a number, not a string.
|
|
49
|
-
*/
|
|
50
|
-
declare function parseSchema<T extends CommonSchema>(schema: T, receivedValue: unknown): InferType<T>;
|
|
51
|
-
|
|
52
|
-
export { type InferType, parseSchema };
|
|
9
|
+
export { BuildSchemaError, ValidationError, throwException } from './exceptions.js';
|
|
10
|
+
import './schemas/ArraySchema.js';
|
|
11
|
+
import './CommonSchema-BIe85W_A.js';
|
|
12
|
+
import './schemas/BooleanSchema.js';
|
|
13
|
+
import './schemas/NumberSchema.js';
|
|
14
|
+
import './schemas/ObjectSchema.js';
|
|
15
|
+
import './schemas/StringSchema.js';
|
package/lib/index.js
CHANGED
|
@@ -20,13 +20,16 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
|
+
BuildSchemaError: () => BuildSchemaError,
|
|
24
|
+
ValidationError: () => ValidationError,
|
|
23
25
|
array: () => array,
|
|
24
26
|
boolean: () => boolean,
|
|
25
27
|
number: () => number,
|
|
26
28
|
object: () => object,
|
|
27
29
|
oneOfTypes: () => oneOfTypes,
|
|
28
30
|
parseSchema: () => parseSchema,
|
|
29
|
-
string: () => string
|
|
31
|
+
string: () => string,
|
|
32
|
+
throwException: () => throwException
|
|
30
33
|
});
|
|
31
34
|
module.exports = __toCommonJS(src_exports);
|
|
32
35
|
|
|
@@ -284,12 +287,15 @@ function oneOfTypes(valueTypes) {
|
|
|
284
287
|
}
|
|
285
288
|
// Annotate the CommonJS export names for ESM import in node:
|
|
286
289
|
0 && (module.exports = {
|
|
290
|
+
BuildSchemaError,
|
|
291
|
+
ValidationError,
|
|
287
292
|
array,
|
|
288
293
|
boolean,
|
|
289
294
|
number,
|
|
290
295
|
object,
|
|
291
296
|
oneOfTypes,
|
|
292
297
|
parseSchema,
|
|
293
|
-
string
|
|
298
|
+
string,
|
|
299
|
+
throwException
|
|
294
300
|
});
|
|
295
301
|
//# sourceMappingURL=index.js.map
|