@strapi/utils 5.13.0 → 5.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -1
- package/dist/rules-engine.d.ts +24 -0
- package/dist/rules-engine.d.ts.map +1 -0
- package/dist/rules-engine.js +75 -0
- package/dist/rules-engine.js.map +1 -0
- package/dist/rules-engine.mjs +72 -0
- package/dist/rules-engine.mjs.map +1 -0
- package/dist/zod.js +4 -4
- package/dist/zod.js.map +1 -1
- package/dist/zod.mjs +4 -4
- package/dist/zod.mjs.map +1 -1
- package/package.json +6 -4
package/dist/index.d.ts
CHANGED
|
@@ -22,6 +22,7 @@ export * as errors from './errors';
|
|
|
22
22
|
export * as contentTypes from './content-types';
|
|
23
23
|
export * as relations from './relations';
|
|
24
24
|
export * as hooks from './hooks';
|
|
25
|
+
export * as rulesEngine from './rules-engine';
|
|
25
26
|
export * from './zod';
|
|
26
27
|
export * from './primitives';
|
|
27
28
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,cAAc,CAAC;AACpD,OAAO,EAAE,OAAO,IAAI,GAAG,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,OAAO,IAAI,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AACnE,OAAO,EAAE,OAAO,IAAI,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAChE,OAAO,EAAE,OAAO,IAAI,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAC9D,OAAO,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,kB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,OAAO,IAAI,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAC9D,OAAO,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAC5D,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAEjD,OAAO,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AACxE,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAE3D,OAAO,KAAK,WAAW,MAAM,wBAAwB,CAAC;AACtD,OAAO,KAAK,QAAQ,MAAM,YAAY,CAAC;AACvC,OAAO,KAAK,QAAQ,MAAM,YAAY,CAAC;AACvC,OAAO,KAAK,UAAU,MAAM,cAAc,CAAC;AAC3C,OAAO,KAAK,cAAc,MAAM,mBAAmB,CAAC;AACpD,OAAO,KAAK,QAAQ,MAAM,YAAY,CAAC;AACvC,OAAO,KAAK,QAAQ,MAAM,YAAY,CAAC;AACvC,OAAO,KAAK,IAAI,MAAM,QAAQ,CAAC;AAC/B,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,OAAO,KAAK,MAAM,MAAM,UAAU,CAAC;AACnC,OAAO,KAAK,GAAG,MAAM,OAAO,CAAC;AAC7B,OAAO,KAAK,MAAM,MAAM,UAAU,CAAC;AACnC,OAAO,KAAK,YAAY,MAAM,iBAAiB,CAAC;AAChD,OAAO,KAAK,SAAS,MAAM,aAAa,CAAC;AACzC,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,OAAO,KAAK,WAAW,MAAM,gBAAgB,CAAC;AAC9C,cAAc,OAAO,CAAC;AAEtB,cAAc,cAAc,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -24,6 +24,7 @@ var errors = require('./errors.js');
|
|
|
24
24
|
var contentTypes = require('./content-types.js');
|
|
25
25
|
var relations = require('./relations.js');
|
|
26
26
|
var hooks = require('./hooks.js');
|
|
27
|
+
var rulesEngine = require('./rules-engine.js');
|
|
27
28
|
var zod = require('./zod.js');
|
|
28
29
|
var strings = require('./primitives/strings.js');
|
|
29
30
|
var arrays = require('./primitives/arrays.js');
|
|
@@ -58,6 +59,7 @@ exports.errors = errors;
|
|
|
58
59
|
exports.contentTypes = contentTypes;
|
|
59
60
|
exports.relations = relations;
|
|
60
61
|
exports.hooks = hooks;
|
|
62
|
+
exports.rulesEngine = rulesEngine;
|
|
61
63
|
exports.validateZod = zod.validateZod;
|
|
62
64
|
exports.strings = strings;
|
|
63
65
|
exports.arrays = arrays;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/index.mjs
CHANGED
|
@@ -37,6 +37,8 @@ import * as relations from './relations.mjs';
|
|
|
37
37
|
export { relations };
|
|
38
38
|
import * as hooks from './hooks.mjs';
|
|
39
39
|
export { hooks };
|
|
40
|
+
import * as rulesEngine from './rules-engine.mjs';
|
|
41
|
+
export { rulesEngine };
|
|
40
42
|
export { validateZod } from './zod.mjs';
|
|
41
43
|
import * as strings from './primitives/strings.mjs';
|
|
42
44
|
export { strings };
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import jsonLogic from 'json-logic-js';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
export declare const ConditionSchema: z.ZodObject<{
|
|
4
|
+
dependsOn: z.ZodString;
|
|
5
|
+
operator: z.ZodEnum<["is", "isNot"]>;
|
|
6
|
+
value: z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean]>;
|
|
7
|
+
}, "strip", z.ZodTypeAny, {
|
|
8
|
+
value: string | number | boolean;
|
|
9
|
+
dependsOn: string;
|
|
10
|
+
operator: "is" | "isNot";
|
|
11
|
+
}, {
|
|
12
|
+
value: string | number | boolean;
|
|
13
|
+
dependsOn: string;
|
|
14
|
+
operator: "is" | "isNot";
|
|
15
|
+
}>;
|
|
16
|
+
export type Condition = z.infer<typeof ConditionSchema>;
|
|
17
|
+
export type JsonLogicCondition = jsonLogic.RulesLogic<jsonLogic.AdditionalOperation>;
|
|
18
|
+
export type RulesEngine = {
|
|
19
|
+
generate: (condition: Condition) => JsonLogicCondition;
|
|
20
|
+
validate: (condition: Condition) => void;
|
|
21
|
+
evaluate: (condition: JsonLogicCondition, data: unknown) => boolean;
|
|
22
|
+
};
|
|
23
|
+
export declare function createRulesEngine(): RulesEngine;
|
|
24
|
+
//# sourceMappingURL=rules-engine.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rules-engine.d.ts","sourceRoot":"","sources":["../src/rules-engine.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,eAAO,MAAM,eAAe;;;;;;;;;;;;EAI1B,CAAC;AAEH,MAAM,MAAM,SAAS,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,eAAe,CAAC,CAAC;AACxD,MAAM,MAAM,kBAAkB,GAAG,SAAS,CAAC,UAAU,CAAC,SAAS,CAAC,mBAAmB,CAAC,CAAC;AACrF,MAAM,MAAM,WAAW,GAAG;IACxB,QAAQ,EAAE,CAAC,SAAS,EAAE,SAAS,KAAK,kBAAkB,CAAC;IACvD,QAAQ,EAAE,CAAC,SAAS,EAAE,SAAS,KAAK,IAAI,CAAC;IACzC,QAAQ,EAAE,CAAC,SAAS,EAAE,kBAAkB,EAAE,IAAI,EAAE,OAAO,KAAK,OAAO,CAAC;CACrE,CAAC;AAEF,wBAAgB,iBAAiB,IAAI,WAAW,CAuD/C"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var jsonLogic = require('json-logic-js');
|
|
4
|
+
var zod = require('zod');
|
|
5
|
+
|
|
6
|
+
const ConditionSchema = zod.z.object({
|
|
7
|
+
dependsOn: zod.z.string().min(1),
|
|
8
|
+
operator: zod.z.enum([
|
|
9
|
+
'is',
|
|
10
|
+
'isNot'
|
|
11
|
+
]),
|
|
12
|
+
value: zod.z.union([
|
|
13
|
+
zod.z.string(),
|
|
14
|
+
zod.z.number(),
|
|
15
|
+
zod.z.boolean()
|
|
16
|
+
])
|
|
17
|
+
});
|
|
18
|
+
function createRulesEngine() {
|
|
19
|
+
/**
|
|
20
|
+
* Transforms a high-level `Condition` object into a JSON Logic-compatible condition.
|
|
21
|
+
*
|
|
22
|
+
* Converts operators like 'is' and 'isNot' into their JSON Logic equivalents ('==' and '!=').
|
|
23
|
+
* Throws an error if the operator is not supported.
|
|
24
|
+
*
|
|
25
|
+
* @param condition - The condition object to convert.
|
|
26
|
+
* @returns A JSON Logic AST representing the condition.
|
|
27
|
+
* @throws {Error} If the operator is not recognized.
|
|
28
|
+
*/ const generate = (condition)=>{
|
|
29
|
+
const { dependsOn, operator, value } = condition;
|
|
30
|
+
const operatorsMap = {
|
|
31
|
+
is: '==',
|
|
32
|
+
isNot: '!='
|
|
33
|
+
};
|
|
34
|
+
if (!operatorsMap[operator]) {
|
|
35
|
+
throw new Error(`Invalid operator: ${operator}`);
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
[operatorsMap[operator]]: [
|
|
39
|
+
{
|
|
40
|
+
var: dependsOn
|
|
41
|
+
},
|
|
42
|
+
value
|
|
43
|
+
]
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Validates a condition object against the `ConditionSchema`.
|
|
48
|
+
*
|
|
49
|
+
* Ensures that the condition adheres to the expected structure and types.
|
|
50
|
+
*
|
|
51
|
+
* @param condition - The condition object to validate.
|
|
52
|
+
* @throws {ZodError} If the condition is invalid.
|
|
53
|
+
*/ const validate = (condition)=>{
|
|
54
|
+
ConditionSchema.parse(condition);
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Evaluates a JSON Logic condition against provided data.
|
|
58
|
+
* @throws {Error} If the condition is invalid.
|
|
59
|
+
*/ const evaluate = (condition, data)=>{
|
|
60
|
+
try {
|
|
61
|
+
return jsonLogic.apply(condition, data);
|
|
62
|
+
} catch (err) {
|
|
63
|
+
throw new Error(`Invalid condition: ${err.message}`);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
return {
|
|
67
|
+
generate,
|
|
68
|
+
validate,
|
|
69
|
+
evaluate
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
exports.ConditionSchema = ConditionSchema;
|
|
74
|
+
exports.createRulesEngine = createRulesEngine;
|
|
75
|
+
//# sourceMappingURL=rules-engine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rules-engine.js","sources":["../src/rules-engine.ts"],"sourcesContent":["import jsonLogic from 'json-logic-js';\nimport { z } from 'zod';\n\nexport const ConditionSchema = z.object({\n dependsOn: z.string().min(1),\n operator: z.enum(['is', 'isNot']),\n value: z.union([z.string(), z.number(), z.boolean()]),\n});\n\nexport type Condition = z.infer<typeof ConditionSchema>;\nexport type JsonLogicCondition = jsonLogic.RulesLogic<jsonLogic.AdditionalOperation>;\nexport type RulesEngine = {\n generate: (condition: Condition) => JsonLogicCondition;\n validate: (condition: Condition) => void;\n evaluate: (condition: JsonLogicCondition, data: unknown) => boolean;\n};\n\nexport function createRulesEngine(): RulesEngine {\n /**\n * Transforms a high-level `Condition` object into a JSON Logic-compatible condition.\n *\n * Converts operators like 'is' and 'isNot' into their JSON Logic equivalents ('==' and '!=').\n * Throws an error if the operator is not supported.\n *\n * @param condition - The condition object to convert.\n * @returns A JSON Logic AST representing the condition.\n * @throws {Error} If the operator is not recognized.\n */\n const generate = (condition: Condition): JsonLogicCondition => {\n const { dependsOn, operator, value } = condition;\n const operatorsMap = {\n is: '==',\n isNot: '!=',\n };\n if (!operatorsMap[operator]) {\n throw new Error(`Invalid operator: ${operator}`);\n }\n return { [operatorsMap[operator]]: [{ var: dependsOn }, value] };\n };\n\n /**\n * Validates a condition object against the `ConditionSchema`.\n *\n * Ensures that the condition adheres to the expected structure and types.\n *\n * @param condition - The condition object to validate.\n * @throws {ZodError} If the condition is invalid.\n */\n const validate = (condition: Condition) => {\n ConditionSchema.parse(condition);\n };\n\n /**\n * Evaluates a JSON Logic condition against provided data.\n * @throws {Error} If the condition is invalid.\n */\n const evaluate = (\n condition: jsonLogic.RulesLogic<jsonLogic.AdditionalOperation>,\n data: unknown\n ): boolean => {\n try {\n return jsonLogic.apply(condition, data);\n } catch (err: any) {\n throw new Error(`Invalid condition: ${err.message}`);\n }\n };\n\n return {\n generate,\n validate,\n evaluate,\n };\n}\n"],"names":["ConditionSchema","z","object","dependsOn","string","min","operator","enum","value","union","number","boolean","createRulesEngine","generate","condition","operatorsMap","is","isNot","Error","var","validate","parse","evaluate","data","jsonLogic","apply","err","message"],"mappings":";;;;;AAGaA,MAAAA,eAAAA,GAAkBC,KAAEC,CAAAA,MAAM,CAAC;AACtCC,IAAAA,SAAAA,EAAWF,KAAEG,CAAAA,MAAM,EAAGC,CAAAA,GAAG,CAAC,CAAA,CAAA;IAC1BC,QAAUL,EAAAA,KAAAA,CAAEM,IAAI,CAAC;AAAC,QAAA,IAAA;AAAM,QAAA;AAAQ,KAAA,CAAA;IAChCC,KAAOP,EAAAA,KAAAA,CAAEQ,KAAK,CAAC;AAACR,QAAAA,KAAAA,CAAEG,MAAM,EAAA;AAAIH,QAAAA,KAAA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const ConditionSchema = z.object({
|
|
5
|
+
dependsOn: z.string().min(1),
|
|
6
|
+
operator: z.enum([
|
|
7
|
+
'is',
|
|
8
|
+
'isNot'
|
|
9
|
+
]),
|
|
10
|
+
value: z.union([
|
|
11
|
+
z.string(),
|
|
12
|
+
z.number(),
|
|
13
|
+
z.boolean()
|
|
14
|
+
])
|
|
15
|
+
});
|
|
16
|
+
function createRulesEngine() {
|
|
17
|
+
/**
|
|
18
|
+
* Transforms a high-level `Condition` object into a JSON Logic-compatible condition.
|
|
19
|
+
*
|
|
20
|
+
* Converts operators like 'is' and 'isNot' into their JSON Logic equivalents ('==' and '!=').
|
|
21
|
+
* Throws an error if the operator is not supported.
|
|
22
|
+
*
|
|
23
|
+
* @param condition - The condition object to convert.
|
|
24
|
+
* @returns A JSON Logic AST representing the condition.
|
|
25
|
+
* @throws {Error} If the operator is not recognized.
|
|
26
|
+
*/ const generate = (condition)=>{
|
|
27
|
+
const { dependsOn, operator, value } = condition;
|
|
28
|
+
const operatorsMap = {
|
|
29
|
+
is: '==',
|
|
30
|
+
isNot: '!='
|
|
31
|
+
};
|
|
32
|
+
if (!operatorsMap[operator]) {
|
|
33
|
+
throw new Error(`Invalid operator: ${operator}`);
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
[operatorsMap[operator]]: [
|
|
37
|
+
{
|
|
38
|
+
var: dependsOn
|
|
39
|
+
},
|
|
40
|
+
value
|
|
41
|
+
]
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Validates a condition object against the `ConditionSchema`.
|
|
46
|
+
*
|
|
47
|
+
* Ensures that the condition adheres to the expected structure and types.
|
|
48
|
+
*
|
|
49
|
+
* @param condition - The condition object to validate.
|
|
50
|
+
* @throws {ZodError} If the condition is invalid.
|
|
51
|
+
*/ const validate = (condition)=>{
|
|
52
|
+
ConditionSchema.parse(condition);
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Evaluates a JSON Logic condition against provided data.
|
|
56
|
+
* @throws {Error} If the condition is invalid.
|
|
57
|
+
*/ const evaluate = (condition, data)=>{
|
|
58
|
+
try {
|
|
59
|
+
return jsonLogic.apply(condition, data);
|
|
60
|
+
} catch (err) {
|
|
61
|
+
throw new Error(`Invalid condition: ${err.message}`);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
return {
|
|
65
|
+
generate,
|
|
66
|
+
validate,
|
|
67
|
+
evaluate
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export { ConditionSchema, createRulesEngine };
|
|
72
|
+
//# sourceMappingURL=rules-engine.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rules-engine.mjs","sources":["../src/rules-engine.ts"],"sourcesContent":["import jsonLogic from 'json-logic-js';\nimport { z } from 'zod';\n\nexport const ConditionSchema = z.object({\n dependsOn: z.string().min(1),\n operator: z.enum(['is', 'isNot']),\n value: z.union([z.string(), z.number(), z.boolean()]),\n});\n\nexport type Condition = z.infer<typeof ConditionSchema>;\nexport type JsonLogicCondition = jsonLogic.RulesLogic<jsonLogic.AdditionalOperation>;\nexport type RulesEngine = {\n generate: (condition: Condition) => JsonLogicCondition;\n validate: (condition: Condition) => void;\n evaluate: (condition: JsonLogicCondition, data: unknown) => boolean;\n};\n\nexport function createRulesEngine(): RulesEngine {\n /**\n * Transforms a high-level `Condition` object into a JSON Logic-compatible condition.\n *\n * Converts operators like 'is' and 'isNot' into their JSON Logic equivalents ('==' and '!=').\n * Throws an error if the operator is not supported.\n *\n * @param condition - The condition object to convert.\n * @returns A JSON Logic AST representing the condition.\n * @throws {Error} If the operator is not recognized.\n */\n const generate = (condition: Condition): JsonLogicCondition => {\n const { dependsOn, operator, value } = condition;\n const operatorsMap = {\n is: '==',\n isNot: '!=',\n };\n if (!operatorsMap[operator]) {\n throw new Error(`Invalid operator: ${operator}`);\n }\n return { [operatorsMap[operator]]: [{ var: dependsOn }, value] };\n };\n\n /**\n * Validates a condition object against the `ConditionSchema`.\n *\n * Ensures that the condition adheres to the expected structure and types.\n *\n * @param condition - The condition object to validate.\n * @throws {ZodError} If the condition is invalid.\n */\n const validate = (condition: Condition) => {\n ConditionSchema.parse(condition);\n };\n\n /**\n * Evaluates a JSON Logic condition against provided data.\n * @throws {Error} If the condition is invalid.\n */\n const evaluate = (\n condition: jsonLogic.RulesLogic<jsonLogic.AdditionalOperation>,\n data: unknown\n ): boolean => {\n try {\n return jsonLogic.apply(condition, data);\n } catch (err: any) {\n throw new Error(`Invalid condition: ${err.message}`);\n }\n };\n\n return {\n generate,\n validate,\n evaluate,\n };\n}\n"],"names":["ConditionSchema","z","object","dependsOn","string","min","operator","enum","value","union","number","boolean","createRulesEngine","generate","condition","operatorsMap","is","isNot","Error","var","validate","parse","evaluate","data","jsonLogic","apply","err","message"],"mappings":";;;AAGaA,MAAAA,eAAAA,GAAkBC,CAAEC,CAAAA,MAAM,CAAC;AACtCC,IAAAA,SAAAA,EAAWF,CAAEG,CAAAA,MAAM,EAAGC,CAAAA,GAAG,CAAC,CAAA,CAAA;IAC1BC,QAAUL,EAAAA,CAAAA,CAAEM,IAAI,CAAC;AAAC,QAAA,IAAA;AAAM,QAAA;AAAQ,KAAA,CAAA;IAChCC,KAAOP,EAAAA,CAAAA,CAAEQ,KAAK,CAAC;AAACR,QAAAA,CAAAA,CAAEG,MAAM,EAAA;AAAIH,QAAAA,CAAAA,CAAES,MAAM,EAAA;AAAIT,QAAAA,CAAAA,CAAEU,OAAO;AAAG,KAAA;AACtD,CAAG;AAUI,SAASC,iBAAAA,GAAAA;AACd;;;;;;;;;MAUA,MAAMC,WAAW,CAACC,SAAAA,GAAAA;AAChB,QAAA,MAAM,EAAEX,SAAS,EAAEG,QAAQ,EAAEE,KAAK,EAAE,GAAGM,SAAAA;AACvC,QAAA,MAAMC,YAAe,GAAA;YACnBC,EAAI,EAAA,IAAA;YACJC,KAAO,EAAA;AACT,SAAA;AACA,QAAA,IAAI,CAACF,YAAY,CAACT,QAAAA,CAAS,EAAE;AAC3B,YAAA,MAAM,IAAIY,KAAM,CAAA,CAAC,kBAAkB,EAAEZ,SAAS,CAAC,CAAA;AACjD;QACA,OAAO;AAAE,YAAA,CAACS,YAAY,CAACT,QAAS,CAAA,GAAG;AAAC,gBAAA;oBAAEa,GAAKhB,EAAAA;AAAU,iBAAA;AAAGK,gBAAAA;AAAM;AAAC,SAAA;AACjE,KAAA;AAEA;;;;;;;MAQA,MAAMY,WAAW,CAACN,SAAAA,GAAAA;AAChBd,QAAAA,eAAAA,CAAgBqB,KAAK,CAACP,SAAAA,CAAAA;AACxB,KAAA;AAEA;;;MAIA,MAAMQ,QAAW,GAAA,CACfR,SACAS,EAAAA,IAAAA,GAAAA;QAEA,IAAI;YACF,OAAOC,SAAAA,CAAUC,KAAK,CAACX,SAAWS,EAAAA,IAAAA,CAAAA;AACpC,SAAA,CAAE,OAAOG,GAAU,EAAA;YACjB,MAAM,IAAIR,MAAM,CAAC,mBAAmB,EAAEQ,GAAIC,CAAAA,OAAO,CAAC,CAAC,CAAA;AACrD;AACF,KAAA;IAEA,OAAO;AACLd,QAAAA,QAAAA;AACAO,QAAAA,QAAAA;AACAE,QAAAA;AACF,KAAA;AACF;;;;"}
|
package/dist/zod.js
CHANGED
|
@@ -17,11 +17,11 @@ const validateZod = (schema)=>(data)=>{
|
|
|
17
17
|
}
|
|
18
18
|
};
|
|
19
19
|
const formatZodErrors = (zodError)=>({
|
|
20
|
-
errors: zodError.
|
|
20
|
+
errors: zodError.format((issue)=>{
|
|
21
21
|
return {
|
|
22
|
-
path:
|
|
23
|
-
message:
|
|
24
|
-
name:
|
|
22
|
+
path: issue.path,
|
|
23
|
+
message: issue.message,
|
|
24
|
+
name: issue.code
|
|
25
25
|
};
|
|
26
26
|
}),
|
|
27
27
|
message: 'Validation error'
|
package/dist/zod.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zod.js","sources":["../src/zod.ts"],"sourcesContent":["import { z } from 'zod';\n\nimport { ValidationError } from './errors';\n\nexport const validateZod =\n <T extends z.ZodTypeAny>(schema: T) =>\n (data: unknown): z.TypeOf<T> => {\n try {\n return schema.parse(data);\n } catch (error) {\n if (error instanceof z.ZodError) {\n const { message, errors } = formatZodErrors(error);\n throw new ValidationError(message, { errors });\n }\n\n throw error;\n }\n };\n\nconst formatZodErrors = (zodError: z.ZodError) => ({\n errors: zodError.
|
|
1
|
+
{"version":3,"file":"zod.js","sources":["../src/zod.ts"],"sourcesContent":["import { z } from 'zod';\n\nimport { ValidationError } from './errors';\n\nexport const validateZod =\n <T extends z.ZodTypeAny>(schema: T) =>\n (data: unknown): z.TypeOf<T> => {\n try {\n return schema.parse(data);\n } catch (error) {\n if (error instanceof z.ZodError) {\n const { message, errors } = formatZodErrors(error);\n throw new ValidationError(message, { errors });\n }\n\n throw error;\n }\n };\n\nconst formatZodErrors = (zodError: z.ZodError) => ({\n errors: zodError.format((issue) => {\n return {\n path: issue.path,\n message: issue.message,\n name: issue.code,\n };\n }),\n message: 'Validation error',\n});\n"],"names":["validateZod","schema","data","parse","error","z","ZodError","message","errors","formatZodErrors","ValidationError","zodError","format","issue","path","name","code"],"mappings":";;;;;AAIaA,MAAAA,WAAAA,GACX,CAAyBC,MAAAA,GACzB,CAACC,IAAAA,GAAAA;QACC,IAAI;YACF,OAAOD,MAAAA,CAAOE,KAAK,CAACD,IAAAA,CAAAA;AACtB,SAAA,CAAE,OAAOE,KAAO,EAAA;YACd,IAAIA,KAAAA,YAAiBC,KAAEC,CAAAA,QAAQ,EAAE;AAC/B,gBAAA,MAAM,EAAEC,OAAO,UAAEC,QAAM,EAAE,GAAGC,eAAgBL,CAAAA,KAAAA,CAAAA;gBAC5C,MAAM,IAAIM,uBAAgBH,OAAS,EAAA;AAAEC,4BAAAA;AAAO,iBAAA,CAAA;AAC9C;YAEA,MAAMJ,KAAAA;AACR;;AAGJ,MAAMK,eAAAA,GAAkB,CAACE,QAAAA,IAA0B;QACjDH,MAAQG,EAAAA,QAAAA,CAASC,MAAM,CAAC,CAACC,KAAAA,GAAAA;YACvB,OAAO;AACLC,gBAAAA,IAAAA,EAAMD,MAAMC,IAAI;AAChBP,gBAAAA,OAAAA,EAASM,MAAMN,OAAO;AACtBQ,gBAAAA,IAAAA,EAAMF,MAAMG;AACd,aAAA;AACF,SAAA,CAAA;QACAT,OAAS,EAAA;KACX,CAAA;;;;"}
|
package/dist/zod.mjs
CHANGED
|
@@ -15,11 +15,11 @@ const validateZod = (schema)=>(data)=>{
|
|
|
15
15
|
}
|
|
16
16
|
};
|
|
17
17
|
const formatZodErrors = (zodError)=>({
|
|
18
|
-
errors: zodError.
|
|
18
|
+
errors: zodError.format((issue)=>{
|
|
19
19
|
return {
|
|
20
|
-
path:
|
|
21
|
-
message:
|
|
22
|
-
name:
|
|
20
|
+
path: issue.path,
|
|
21
|
+
message: issue.message,
|
|
22
|
+
name: issue.code
|
|
23
23
|
};
|
|
24
24
|
}),
|
|
25
25
|
message: 'Validation error'
|
package/dist/zod.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zod.mjs","sources":["../src/zod.ts"],"sourcesContent":["import { z } from 'zod';\n\nimport { ValidationError } from './errors';\n\nexport const validateZod =\n <T extends z.ZodTypeAny>(schema: T) =>\n (data: unknown): z.TypeOf<T> => {\n try {\n return schema.parse(data);\n } catch (error) {\n if (error instanceof z.ZodError) {\n const { message, errors } = formatZodErrors(error);\n throw new ValidationError(message, { errors });\n }\n\n throw error;\n }\n };\n\nconst formatZodErrors = (zodError: z.ZodError) => ({\n errors: zodError.
|
|
1
|
+
{"version":3,"file":"zod.mjs","sources":["../src/zod.ts"],"sourcesContent":["import { z } from 'zod';\n\nimport { ValidationError } from './errors';\n\nexport const validateZod =\n <T extends z.ZodTypeAny>(schema: T) =>\n (data: unknown): z.TypeOf<T> => {\n try {\n return schema.parse(data);\n } catch (error) {\n if (error instanceof z.ZodError) {\n const { message, errors } = formatZodErrors(error);\n throw new ValidationError(message, { errors });\n }\n\n throw error;\n }\n };\n\nconst formatZodErrors = (zodError: z.ZodError) => ({\n errors: zodError.format((issue) => {\n return {\n path: issue.path,\n message: issue.message,\n name: issue.code,\n };\n }),\n message: 'Validation error',\n});\n"],"names":["validateZod","schema","data","parse","error","z","ZodError","message","errors","formatZodErrors","ValidationError","zodError","format","issue","path","name","code"],"mappings":";;;AAIaA,MAAAA,WAAAA,GACX,CAAyBC,MAAAA,GACzB,CAACC,IAAAA,GAAAA;QACC,IAAI;YACF,OAAOD,MAAAA,CAAOE,KAAK,CAACD,IAAAA,CAAAA;AACtB,SAAA,CAAE,OAAOE,KAAO,EAAA;YACd,IAAIA,KAAAA,YAAiBC,CAAEC,CAAAA,QAAQ,EAAE;AAC/B,gBAAA,MAAM,EAAEC,OAAO,EAAEC,MAAM,EAAE,GAAGC,eAAgBL,CAAAA,KAAAA,CAAAA;gBAC5C,MAAM,IAAIM,gBAAgBH,OAAS,EAAA;AAAEC,oBAAAA;AAAO,iBAAA,CAAA;AAC9C;YAEA,MAAMJ,KAAAA;AACR;;AAGJ,MAAMK,eAAAA,GAAkB,CAACE,QAAAA,IAA0B;QACjDH,MAAQG,EAAAA,QAAAA,CAASC,MAAM,CAAC,CAACC,KAAAA,GAAAA;YACvB,OAAO;AACLC,gBAAAA,IAAAA,EAAMD,MAAMC,IAAI;AAChBP,gBAAAA,OAAAA,EAASM,MAAMN,OAAO;AACtBQ,gBAAAA,IAAAA,EAAMF,MAAMG;AACd,aAAA;AACF,SAAA,CAAA;QACAT,OAAS,EAAA;KACX,CAAA;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@strapi/utils",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.14.0",
|
|
4
4
|
"description": "Shared utilities for the Strapi packages",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"strapi",
|
|
@@ -50,21 +50,23 @@
|
|
|
50
50
|
"date-fns": "2.30.0",
|
|
51
51
|
"execa": "5.1.1",
|
|
52
52
|
"http-errors": "2.0.0",
|
|
53
|
+
"json-logic-js": "2.0.5",
|
|
53
54
|
"lodash": "4.17.21",
|
|
54
55
|
"node-machine-id": "1.1.12",
|
|
55
56
|
"p-map": "4.0.0",
|
|
56
57
|
"preferred-pm": "3.1.2",
|
|
57
58
|
"yup": "0.32.9",
|
|
58
|
-
"zod": "
|
|
59
|
+
"zod": "3.24.2"
|
|
59
60
|
},
|
|
60
61
|
"devDependencies": {
|
|
61
62
|
"@types/http-errors": "2.0.4",
|
|
63
|
+
"@types/json-logic-js": "2.0.8",
|
|
62
64
|
"@types/koa": "2.13.4",
|
|
63
65
|
"@types/node": "18.19.24",
|
|
64
|
-
"eslint-config-custom": "5.
|
|
66
|
+
"eslint-config-custom": "5.14.0",
|
|
65
67
|
"koa": "2.15.4",
|
|
66
68
|
"koa-body": "6.0.1",
|
|
67
|
-
"tsconfig": "5.
|
|
69
|
+
"tsconfig": "5.14.0"
|
|
68
70
|
},
|
|
69
71
|
"engines": {
|
|
70
72
|
"node": ">=18.0.0 <=22.x.x",
|