yuppi 1.0.2 → 1.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/README.md +15 -14
- package/dist/Yuppi.class.js +40 -21
- package/dist/Yuppi.class.mjs +40 -21
- package/dist/barrels/Patterns.barrel.js +1 -1
- package/dist/barrels/Patterns.barrel.mjs +1 -1
- package/dist/main.js +41 -22
- package/dist/main.mjs +41 -22
- package/dist/patterns/Email.pattern.js +1 -1
- package/dist/patterns/Email.pattern.mjs +1 -1
- package/dist/utils/ConvertToYup.util.js +40 -21
- package/dist/utils/ConvertToYup.util.mjs +40 -21
- package/package.json +3 -4
package/README.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
[String]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
|
|
2
2
|
[Number]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number
|
|
3
|
+
[Boolean]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean
|
|
3
4
|
[Date]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
|
|
4
5
|
[Object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
|
|
5
6
|
[Array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
|
|
6
|
-
[Boolean]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean
|
|
7
7
|
[Buffer]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
|
|
8
8
|
[Function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function
|
|
9
9
|
[Promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
|
|
@@ -57,8 +57,9 @@ Schemas that can be converted to Yup and JSON Schema.
|
|
|
57
57
|
## Features
|
|
58
58
|
|
|
59
59
|
- Easy and understandable schema
|
|
60
|
+
- Contains ready regex patterns
|
|
61
|
+
- Portable schemas as a JSON file
|
|
60
62
|
- Works with Yup, stable and secure
|
|
61
|
-
- Targeted for API endpoints
|
|
62
63
|
- It is strict and therefore does not accept more than one type
|
|
63
64
|
- Error messages are ready to be understood but can be edited if desired
|
|
64
65
|
- Can be converted to Yup and [JSON Schema](https://json-schema.org). JSON Schema is OpenAPI compatible
|
|
@@ -153,10 +154,10 @@ Yuppi schema builder.
|
|
|
153
154
|
|
|
154
155
|
Validate the properties with your Yuppi schema.
|
|
155
156
|
|
|
156
|
-
> | Parameter | Description |
|
|
157
|
-
> | ---------- | ------------------------------------------ |
|
|
158
|
-
> | schema | [Schema]<br/>Yuppi schema. |
|
|
159
|
-
> | properties | [AnyObject]<br/>Properties to be validate. |
|
|
157
|
+
> | Parameter | Default | Description |
|
|
158
|
+
> | ---------- | ------- | ------------------------------------------ |
|
|
159
|
+
> | schema | | [Schema]<br/>Yuppi schema. |
|
|
160
|
+
> | properties | | [AnyObject]<br/>Properties to be validate. |
|
|
160
161
|
>
|
|
161
162
|
> returns [Promise]<[AnyObject]>
|
|
162
163
|
>
|
|
@@ -208,7 +209,7 @@ Validate the properties with your Yuppi schema.
|
|
|
208
209
|
> */
|
|
209
210
|
> })
|
|
210
211
|
> .catch((error: YuppiTypes.ValidationError) => {
|
|
211
|
-
> console.log(properties); // "Field email must match the required pattern ^[a-zA-Z0-9._%-]+@[a-zA-Z0-9
|
|
212
|
+
> console.log(properties); // "Field email must match the required pattern ^[a-zA-Z0-9._%-]+@([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$"
|
|
212
213
|
> });
|
|
213
214
|
> ```
|
|
214
215
|
|
|
@@ -218,9 +219,9 @@ Validate the properties with your Yuppi schema.
|
|
|
218
219
|
|
|
219
220
|
Convert your Yuppi schema into Yup schema.
|
|
220
221
|
|
|
221
|
-
> | Parameter | Description |
|
|
222
|
-
> | --------- | -------------------------- |
|
|
223
|
-
> | schema | [Schema]<br/>Yuppi schema. |
|
|
222
|
+
> | Parameter | Default | Description |
|
|
223
|
+
> | --------- | ------- | -------------------------- |
|
|
224
|
+
> | schema | | [Schema]<br/>Yuppi schema. |
|
|
224
225
|
>
|
|
225
226
|
> returns [AnyObject]
|
|
226
227
|
>
|
|
@@ -263,9 +264,9 @@ Convert your Yuppi schema into Yup schema.
|
|
|
263
264
|
|
|
264
265
|
Convert your Yuppi schema into [JSON Schema](https://json-schema.org).
|
|
265
266
|
|
|
266
|
-
> | Parameter | Description |
|
|
267
|
-
> | --------- | -------------------------- |
|
|
268
|
-
> | schema | [Schema]<br/>Yuppi schema. |
|
|
267
|
+
> | Parameter | Default | Description |
|
|
268
|
+
> | --------- | ------- | -------------------------- |
|
|
269
|
+
> | schema | | [Schema]<br/>Yuppi schema. |
|
|
269
270
|
>
|
|
270
271
|
> returns [AnyObject]
|
|
271
272
|
>
|
|
@@ -318,7 +319,7 @@ Convert your Yuppi schema into [JSON Schema](https://json-schema.org).
|
|
|
318
319
|
> },
|
|
319
320
|
> email: {
|
|
320
321
|
> type: "string",
|
|
321
|
-
> pattern: "^[a-zA-Z0-9._%-]+@[a-zA-Z0-9
|
|
322
|
+
> pattern: "^[a-zA-Z0-9._%-]+@([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}$",
|
|
322
323
|
> }
|
|
323
324
|
> },
|
|
324
325
|
> required: [
|
package/dist/Yuppi.class.js
CHANGED
|
@@ -38,11 +38,34 @@ var import_lodash = __toESM(require("lodash"));
|
|
|
38
38
|
// src/utils/ConvertToYup.util.ts
|
|
39
39
|
var Yup = __toESM(require("yup"));
|
|
40
40
|
var convertToYup = (schema, error_messages) => {
|
|
41
|
+
const base = (schema2, key, config) => {
|
|
42
|
+
schema2 = schema2.nullable();
|
|
43
|
+
if (config.required)
|
|
44
|
+
schema2 = schema2.test(
|
|
45
|
+
"required",
|
|
46
|
+
({ path }) => (error_messages?.base?.required ?? "").split("{path}").join(path),
|
|
47
|
+
(property) => {
|
|
48
|
+
if (property === void 0) return false;
|
|
49
|
+
if (typeof property === "string" && property.trim() === "") return false;
|
|
50
|
+
if (Array.isArray(property) && property.length === 0) return false;
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
);
|
|
54
|
+
if (!config.nullable) schema2 = schema2.nonNullable(({ path }) => (error_messages?.base?.nullable ?? "").split("{path}").join(path));
|
|
55
|
+
if (config.default !== void 0) schema2 = schema2.default(config.default);
|
|
56
|
+
if (config.pattern !== void 0 && schema2.matches !== void 0)
|
|
57
|
+
schema2 = schema2.matches(
|
|
58
|
+
new RegExp(config.pattern ?? "[\\s\\S]*"),
|
|
59
|
+
({ path }) => (error_messages?.base?.pattern ?? "").split("{path}").join(path).split("{pattern}").join(new RegExp(config.pattern ?? "[\\s\\S]*").source)
|
|
60
|
+
);
|
|
61
|
+
return schema2;
|
|
62
|
+
};
|
|
41
63
|
const build = (key, config) => {
|
|
42
64
|
let schema2;
|
|
43
65
|
if (config.type === "string") {
|
|
44
66
|
schema2 = Yup.string().typeError(({ path }) => (error_messages?.string?.type ?? "").split("{path}").join(path));
|
|
45
|
-
schema2 = schema2
|
|
67
|
+
schema2 = base(schema2, key, config);
|
|
68
|
+
schema2 = schema2.transform((property) => typeof property === "string" ? property.trim() : property);
|
|
46
69
|
if (config.min !== void 0)
|
|
47
70
|
schema2 = schema2.min(
|
|
48
71
|
config.min,
|
|
@@ -53,28 +76,38 @@ var convertToYup = (schema, error_messages) => {
|
|
|
53
76
|
config.max,
|
|
54
77
|
({ path, max }) => (error_messages?.string?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()).split("{plural_suffix}").join(max > 1 ? "s" : "")
|
|
55
78
|
);
|
|
56
|
-
if (config.lowercase === true) schema2 = schema2.
|
|
57
|
-
if (config.uppercase === true) schema2 = schema2.
|
|
79
|
+
if (config.lowercase === true) schema2 = schema2.transform((property) => typeof property === "string" ? property.toLowerCase() : property);
|
|
80
|
+
if (config.uppercase === true) schema2 = schema2.transform((property) => typeof property === "string" ? property.toUpperCase() : property);
|
|
81
|
+
return schema2;
|
|
58
82
|
} else if (config.type === "number") {
|
|
59
83
|
schema2 = Yup.number().typeError(({ path }) => (error_messages?.number?.type ?? "").split("{path}").join(path));
|
|
84
|
+
schema2 = base(schema2, key, config);
|
|
60
85
|
if (config.min !== void 0) schema2 = schema2.min(config.min, ({ path, min }) => (error_messages?.number?.min ?? "").split("{path}").join(path).split("{min}").join(min.toString()));
|
|
61
86
|
if (config.max !== void 0) schema2 = schema2.max(config.max, ({ path, max }) => (error_messages?.number?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()));
|
|
62
87
|
if (config.integer === true) schema2 = schema2.integer(({ path }) => (error_messages?.number?.integer ?? "").split("{path}").join(path));
|
|
63
88
|
if (config.positive === true) schema2 = schema2.positive(({ path }) => (error_messages?.number?.positive ?? "").split("{path}").join(path));
|
|
64
89
|
if (config.negative === true) schema2 = schema2.negative(({ path }) => (error_messages?.number?.negative ?? "").split("{path}").join(path));
|
|
90
|
+
return schema2;
|
|
65
91
|
} else if (config.type === "boolean") {
|
|
66
92
|
schema2 = Yup.boolean().typeError(({ path }) => (error_messages?.boolean?.type ?? "").split("{path}").join(path));
|
|
93
|
+
schema2 = base(schema2, key, config);
|
|
94
|
+
return schema2;
|
|
67
95
|
} else if (config.type === "date") {
|
|
68
96
|
schema2 = Yup.date().typeError(({ path }) => (error_messages?.date?.type ?? "").split("{path}").join(path));
|
|
97
|
+
schema2 = base(schema2, key, config);
|
|
69
98
|
if (config.min !== void 0) schema2 = schema2.min(config.min, ({ path, min }) => (error_messages?.date?.min ?? "").split("{path}").join(path).split("{min}").join(new Date(min).toISOString()));
|
|
70
99
|
if (config.max !== void 0) schema2 = schema2.max(config.max, ({ path, max }) => (error_messages?.date?.max ?? "").split("{path}").join(path).split("{max}").join(new Date(max).toISOString()));
|
|
100
|
+
return schema2;
|
|
71
101
|
} else if (config.type === "object") {
|
|
72
102
|
schema2 = Yup.object().typeError(({ path }) => (error_messages?.object?.type ?? "").split("{path}").join(path));
|
|
103
|
+
schema2 = base(schema2, key, config);
|
|
73
104
|
const nested_properties = {};
|
|
74
105
|
for (const [nested_key, nested_config] of Object.entries(config.properties)) nested_properties[nested_key] = build(nested_key, nested_config);
|
|
75
106
|
schema2 = schema2.shape(nested_properties);
|
|
107
|
+
return schema2;
|
|
76
108
|
} else if (config.type === "array") {
|
|
77
109
|
schema2 = Yup.array().typeError(({ path }) => (error_messages?.array?.type ?? "").split("{path}").join(path));
|
|
110
|
+
schema2 = base(schema2, key, config);
|
|
78
111
|
if (config.min !== void 0)
|
|
79
112
|
schema2 = schema2.min(
|
|
80
113
|
config.min,
|
|
@@ -86,26 +119,12 @@ var convertToYup = (schema, error_messages) => {
|
|
|
86
119
|
({ path, max }) => (error_messages?.array?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()).split("{plural_suffix}").join(max > 1 ? "s" : "")
|
|
87
120
|
);
|
|
88
121
|
schema2 = schema2.of(build(key, config.items));
|
|
122
|
+
return schema2;
|
|
89
123
|
} else throw new Error(`Unsupported schema type for ${key}`);
|
|
90
|
-
schema2 = schema2.nullable();
|
|
91
|
-
if (config.pattern !== void 0 && schema2.matches !== void 0)
|
|
92
|
-
schema2 = schema2.matches(
|
|
93
|
-
new RegExp(config.pattern ?? "[\\s\\S]*"),
|
|
94
|
-
({ path }) => (error_messages?.base?.pattern ?? "").split("{path}").join(path).split("{pattern}").join(new RegExp(config.pattern ?? "[\\s\\S]*").source)
|
|
95
|
-
);
|
|
96
|
-
if (config.default !== void 0) schema2 = schema2.default(config.default);
|
|
97
|
-
if (!config.nullable && config.default !== null)
|
|
98
|
-
schema2 = schema2.test(
|
|
99
|
-
"nullable",
|
|
100
|
-
({ path }) => (error_messages?.base?.nullable ?? "").split("{path}").join(path),
|
|
101
|
-
(value) => value !== null
|
|
102
|
-
);
|
|
103
|
-
if (config.required) schema2 = schema2.required(({ path }) => (error_messages?.base?.required ?? "").split("{path}").join(path));
|
|
104
|
-
return schema2;
|
|
105
124
|
};
|
|
106
|
-
const
|
|
107
|
-
for (const [key, config] of Object.entries(schema))
|
|
108
|
-
return Yup.object().shape(
|
|
125
|
+
const properties = {};
|
|
126
|
+
for (const [key, config] of Object.entries(schema)) properties[key] = build(key, config);
|
|
127
|
+
return Yup.object().shape(properties);
|
|
109
128
|
};
|
|
110
129
|
|
|
111
130
|
// src/utils/ConvertToJSONSchema.util.ts
|
package/dist/Yuppi.class.mjs
CHANGED
|
@@ -4,11 +4,34 @@ import _ from "lodash";
|
|
|
4
4
|
// src/utils/ConvertToYup.util.ts
|
|
5
5
|
import * as Yup from "yup";
|
|
6
6
|
var convertToYup = (schema, error_messages) => {
|
|
7
|
+
const base = (schema2, key, config) => {
|
|
8
|
+
schema2 = schema2.nullable();
|
|
9
|
+
if (config.required)
|
|
10
|
+
schema2 = schema2.test(
|
|
11
|
+
"required",
|
|
12
|
+
({ path }) => (error_messages?.base?.required ?? "").split("{path}").join(path),
|
|
13
|
+
(property) => {
|
|
14
|
+
if (property === void 0) return false;
|
|
15
|
+
if (typeof property === "string" && property.trim() === "") return false;
|
|
16
|
+
if (Array.isArray(property) && property.length === 0) return false;
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
);
|
|
20
|
+
if (!config.nullable) schema2 = schema2.nonNullable(({ path }) => (error_messages?.base?.nullable ?? "").split("{path}").join(path));
|
|
21
|
+
if (config.default !== void 0) schema2 = schema2.default(config.default);
|
|
22
|
+
if (config.pattern !== void 0 && schema2.matches !== void 0)
|
|
23
|
+
schema2 = schema2.matches(
|
|
24
|
+
new RegExp(config.pattern ?? "[\\s\\S]*"),
|
|
25
|
+
({ path }) => (error_messages?.base?.pattern ?? "").split("{path}").join(path).split("{pattern}").join(new RegExp(config.pattern ?? "[\\s\\S]*").source)
|
|
26
|
+
);
|
|
27
|
+
return schema2;
|
|
28
|
+
};
|
|
7
29
|
const build = (key, config) => {
|
|
8
30
|
let schema2;
|
|
9
31
|
if (config.type === "string") {
|
|
10
32
|
schema2 = Yup.string().typeError(({ path }) => (error_messages?.string?.type ?? "").split("{path}").join(path));
|
|
11
|
-
schema2 = schema2
|
|
33
|
+
schema2 = base(schema2, key, config);
|
|
34
|
+
schema2 = schema2.transform((property) => typeof property === "string" ? property.trim() : property);
|
|
12
35
|
if (config.min !== void 0)
|
|
13
36
|
schema2 = schema2.min(
|
|
14
37
|
config.min,
|
|
@@ -19,28 +42,38 @@ var convertToYup = (schema, error_messages) => {
|
|
|
19
42
|
config.max,
|
|
20
43
|
({ path, max }) => (error_messages?.string?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()).split("{plural_suffix}").join(max > 1 ? "s" : "")
|
|
21
44
|
);
|
|
22
|
-
if (config.lowercase === true) schema2 = schema2.
|
|
23
|
-
if (config.uppercase === true) schema2 = schema2.
|
|
45
|
+
if (config.lowercase === true) schema2 = schema2.transform((property) => typeof property === "string" ? property.toLowerCase() : property);
|
|
46
|
+
if (config.uppercase === true) schema2 = schema2.transform((property) => typeof property === "string" ? property.toUpperCase() : property);
|
|
47
|
+
return schema2;
|
|
24
48
|
} else if (config.type === "number") {
|
|
25
49
|
schema2 = Yup.number().typeError(({ path }) => (error_messages?.number?.type ?? "").split("{path}").join(path));
|
|
50
|
+
schema2 = base(schema2, key, config);
|
|
26
51
|
if (config.min !== void 0) schema2 = schema2.min(config.min, ({ path, min }) => (error_messages?.number?.min ?? "").split("{path}").join(path).split("{min}").join(min.toString()));
|
|
27
52
|
if (config.max !== void 0) schema2 = schema2.max(config.max, ({ path, max }) => (error_messages?.number?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()));
|
|
28
53
|
if (config.integer === true) schema2 = schema2.integer(({ path }) => (error_messages?.number?.integer ?? "").split("{path}").join(path));
|
|
29
54
|
if (config.positive === true) schema2 = schema2.positive(({ path }) => (error_messages?.number?.positive ?? "").split("{path}").join(path));
|
|
30
55
|
if (config.negative === true) schema2 = schema2.negative(({ path }) => (error_messages?.number?.negative ?? "").split("{path}").join(path));
|
|
56
|
+
return schema2;
|
|
31
57
|
} else if (config.type === "boolean") {
|
|
32
58
|
schema2 = Yup.boolean().typeError(({ path }) => (error_messages?.boolean?.type ?? "").split("{path}").join(path));
|
|
59
|
+
schema2 = base(schema2, key, config);
|
|
60
|
+
return schema2;
|
|
33
61
|
} else if (config.type === "date") {
|
|
34
62
|
schema2 = Yup.date().typeError(({ path }) => (error_messages?.date?.type ?? "").split("{path}").join(path));
|
|
63
|
+
schema2 = base(schema2, key, config);
|
|
35
64
|
if (config.min !== void 0) schema2 = schema2.min(config.min, ({ path, min }) => (error_messages?.date?.min ?? "").split("{path}").join(path).split("{min}").join(new Date(min).toISOString()));
|
|
36
65
|
if (config.max !== void 0) schema2 = schema2.max(config.max, ({ path, max }) => (error_messages?.date?.max ?? "").split("{path}").join(path).split("{max}").join(new Date(max).toISOString()));
|
|
66
|
+
return schema2;
|
|
37
67
|
} else if (config.type === "object") {
|
|
38
68
|
schema2 = Yup.object().typeError(({ path }) => (error_messages?.object?.type ?? "").split("{path}").join(path));
|
|
69
|
+
schema2 = base(schema2, key, config);
|
|
39
70
|
const nested_properties = {};
|
|
40
71
|
for (const [nested_key, nested_config] of Object.entries(config.properties)) nested_properties[nested_key] = build(nested_key, nested_config);
|
|
41
72
|
schema2 = schema2.shape(nested_properties);
|
|
73
|
+
return schema2;
|
|
42
74
|
} else if (config.type === "array") {
|
|
43
75
|
schema2 = Yup.array().typeError(({ path }) => (error_messages?.array?.type ?? "").split("{path}").join(path));
|
|
76
|
+
schema2 = base(schema2, key, config);
|
|
44
77
|
if (config.min !== void 0)
|
|
45
78
|
schema2 = schema2.min(
|
|
46
79
|
config.min,
|
|
@@ -52,26 +85,12 @@ var convertToYup = (schema, error_messages) => {
|
|
|
52
85
|
({ path, max }) => (error_messages?.array?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()).split("{plural_suffix}").join(max > 1 ? "s" : "")
|
|
53
86
|
);
|
|
54
87
|
schema2 = schema2.of(build(key, config.items));
|
|
88
|
+
return schema2;
|
|
55
89
|
} else throw new Error(`Unsupported schema type for ${key}`);
|
|
56
|
-
schema2 = schema2.nullable();
|
|
57
|
-
if (config.pattern !== void 0 && schema2.matches !== void 0)
|
|
58
|
-
schema2 = schema2.matches(
|
|
59
|
-
new RegExp(config.pattern ?? "[\\s\\S]*"),
|
|
60
|
-
({ path }) => (error_messages?.base?.pattern ?? "").split("{path}").join(path).split("{pattern}").join(new RegExp(config.pattern ?? "[\\s\\S]*").source)
|
|
61
|
-
);
|
|
62
|
-
if (config.default !== void 0) schema2 = schema2.default(config.default);
|
|
63
|
-
if (!config.nullable && config.default !== null)
|
|
64
|
-
schema2 = schema2.test(
|
|
65
|
-
"nullable",
|
|
66
|
-
({ path }) => (error_messages?.base?.nullable ?? "").split("{path}").join(path),
|
|
67
|
-
(value) => value !== null
|
|
68
|
-
);
|
|
69
|
-
if (config.required) schema2 = schema2.required(({ path }) => (error_messages?.base?.required ?? "").split("{path}").join(path));
|
|
70
|
-
return schema2;
|
|
71
90
|
};
|
|
72
|
-
const
|
|
73
|
-
for (const [key, config] of Object.entries(schema))
|
|
74
|
-
return Yup.object().shape(
|
|
91
|
+
const properties = {};
|
|
92
|
+
for (const [key, config] of Object.entries(schema)) properties[key] = build(key, config);
|
|
93
|
+
return Yup.object().shape(properties);
|
|
75
94
|
};
|
|
76
95
|
|
|
77
96
|
// src/utils/ConvertToJSONSchema.util.ts
|
|
@@ -28,7 +28,7 @@ __export(Patterns_barrel_exports, {
|
|
|
28
28
|
module.exports = __toCommonJS(Patterns_barrel_exports);
|
|
29
29
|
|
|
30
30
|
// src/patterns/Email.pattern.ts
|
|
31
|
-
var Email = "^[a-zA-Z0-9._%-]+@[a-zA-Z0-9
|
|
31
|
+
var Email = "^[a-zA-Z0-9._%-]+@([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}$";
|
|
32
32
|
|
|
33
33
|
// src/patterns/PhoneNumber.pattern.ts
|
|
34
34
|
var PhoneNumber = "^\\d{4}\\d{7,12}$";
|
package/dist/main.js
CHANGED
|
@@ -41,11 +41,34 @@ var import_lodash = __toESM(require("lodash"));
|
|
|
41
41
|
// src/utils/ConvertToYup.util.ts
|
|
42
42
|
var Yup = __toESM(require("yup"));
|
|
43
43
|
var convertToYup = (schema, error_messages) => {
|
|
44
|
+
const base = (schema2, key, config) => {
|
|
45
|
+
schema2 = schema2.nullable();
|
|
46
|
+
if (config.required)
|
|
47
|
+
schema2 = schema2.test(
|
|
48
|
+
"required",
|
|
49
|
+
({ path }) => (error_messages?.base?.required ?? "").split("{path}").join(path),
|
|
50
|
+
(property) => {
|
|
51
|
+
if (property === void 0) return false;
|
|
52
|
+
if (typeof property === "string" && property.trim() === "") return false;
|
|
53
|
+
if (Array.isArray(property) && property.length === 0) return false;
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
);
|
|
57
|
+
if (!config.nullable) schema2 = schema2.nonNullable(({ path }) => (error_messages?.base?.nullable ?? "").split("{path}").join(path));
|
|
58
|
+
if (config.default !== void 0) schema2 = schema2.default(config.default);
|
|
59
|
+
if (config.pattern !== void 0 && schema2.matches !== void 0)
|
|
60
|
+
schema2 = schema2.matches(
|
|
61
|
+
new RegExp(config.pattern ?? "[\\s\\S]*"),
|
|
62
|
+
({ path }) => (error_messages?.base?.pattern ?? "").split("{path}").join(path).split("{pattern}").join(new RegExp(config.pattern ?? "[\\s\\S]*").source)
|
|
63
|
+
);
|
|
64
|
+
return schema2;
|
|
65
|
+
};
|
|
44
66
|
const build = (key, config) => {
|
|
45
67
|
let schema2;
|
|
46
68
|
if (config.type === "string") {
|
|
47
69
|
schema2 = Yup.string().typeError(({ path }) => (error_messages?.string?.type ?? "").split("{path}").join(path));
|
|
48
|
-
schema2 = schema2
|
|
70
|
+
schema2 = base(schema2, key, config);
|
|
71
|
+
schema2 = schema2.transform((property) => typeof property === "string" ? property.trim() : property);
|
|
49
72
|
if (config.min !== void 0)
|
|
50
73
|
schema2 = schema2.min(
|
|
51
74
|
config.min,
|
|
@@ -56,28 +79,38 @@ var convertToYup = (schema, error_messages) => {
|
|
|
56
79
|
config.max,
|
|
57
80
|
({ path, max }) => (error_messages?.string?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()).split("{plural_suffix}").join(max > 1 ? "s" : "")
|
|
58
81
|
);
|
|
59
|
-
if (config.lowercase === true) schema2 = schema2.
|
|
60
|
-
if (config.uppercase === true) schema2 = schema2.
|
|
82
|
+
if (config.lowercase === true) schema2 = schema2.transform((property) => typeof property === "string" ? property.toLowerCase() : property);
|
|
83
|
+
if (config.uppercase === true) schema2 = schema2.transform((property) => typeof property === "string" ? property.toUpperCase() : property);
|
|
84
|
+
return schema2;
|
|
61
85
|
} else if (config.type === "number") {
|
|
62
86
|
schema2 = Yup.number().typeError(({ path }) => (error_messages?.number?.type ?? "").split("{path}").join(path));
|
|
87
|
+
schema2 = base(schema2, key, config);
|
|
63
88
|
if (config.min !== void 0) schema2 = schema2.min(config.min, ({ path, min }) => (error_messages?.number?.min ?? "").split("{path}").join(path).split("{min}").join(min.toString()));
|
|
64
89
|
if (config.max !== void 0) schema2 = schema2.max(config.max, ({ path, max }) => (error_messages?.number?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()));
|
|
65
90
|
if (config.integer === true) schema2 = schema2.integer(({ path }) => (error_messages?.number?.integer ?? "").split("{path}").join(path));
|
|
66
91
|
if (config.positive === true) schema2 = schema2.positive(({ path }) => (error_messages?.number?.positive ?? "").split("{path}").join(path));
|
|
67
92
|
if (config.negative === true) schema2 = schema2.negative(({ path }) => (error_messages?.number?.negative ?? "").split("{path}").join(path));
|
|
93
|
+
return schema2;
|
|
68
94
|
} else if (config.type === "boolean") {
|
|
69
95
|
schema2 = Yup.boolean().typeError(({ path }) => (error_messages?.boolean?.type ?? "").split("{path}").join(path));
|
|
96
|
+
schema2 = base(schema2, key, config);
|
|
97
|
+
return schema2;
|
|
70
98
|
} else if (config.type === "date") {
|
|
71
99
|
schema2 = Yup.date().typeError(({ path }) => (error_messages?.date?.type ?? "").split("{path}").join(path));
|
|
100
|
+
schema2 = base(schema2, key, config);
|
|
72
101
|
if (config.min !== void 0) schema2 = schema2.min(config.min, ({ path, min }) => (error_messages?.date?.min ?? "").split("{path}").join(path).split("{min}").join(new Date(min).toISOString()));
|
|
73
102
|
if (config.max !== void 0) schema2 = schema2.max(config.max, ({ path, max }) => (error_messages?.date?.max ?? "").split("{path}").join(path).split("{max}").join(new Date(max).toISOString()));
|
|
103
|
+
return schema2;
|
|
74
104
|
} else if (config.type === "object") {
|
|
75
105
|
schema2 = Yup.object().typeError(({ path }) => (error_messages?.object?.type ?? "").split("{path}").join(path));
|
|
106
|
+
schema2 = base(schema2, key, config);
|
|
76
107
|
const nested_properties = {};
|
|
77
108
|
for (const [nested_key, nested_config] of Object.entries(config.properties)) nested_properties[nested_key] = build(nested_key, nested_config);
|
|
78
109
|
schema2 = schema2.shape(nested_properties);
|
|
110
|
+
return schema2;
|
|
79
111
|
} else if (config.type === "array") {
|
|
80
112
|
schema2 = Yup.array().typeError(({ path }) => (error_messages?.array?.type ?? "").split("{path}").join(path));
|
|
113
|
+
schema2 = base(schema2, key, config);
|
|
81
114
|
if (config.min !== void 0)
|
|
82
115
|
schema2 = schema2.min(
|
|
83
116
|
config.min,
|
|
@@ -89,26 +122,12 @@ var convertToYup = (schema, error_messages) => {
|
|
|
89
122
|
({ path, max }) => (error_messages?.array?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()).split("{plural_suffix}").join(max > 1 ? "s" : "")
|
|
90
123
|
);
|
|
91
124
|
schema2 = schema2.of(build(key, config.items));
|
|
125
|
+
return schema2;
|
|
92
126
|
} else throw new Error(`Unsupported schema type for ${key}`);
|
|
93
|
-
schema2 = schema2.nullable();
|
|
94
|
-
if (config.pattern !== void 0 && schema2.matches !== void 0)
|
|
95
|
-
schema2 = schema2.matches(
|
|
96
|
-
new RegExp(config.pattern ?? "[\\s\\S]*"),
|
|
97
|
-
({ path }) => (error_messages?.base?.pattern ?? "").split("{path}").join(path).split("{pattern}").join(new RegExp(config.pattern ?? "[\\s\\S]*").source)
|
|
98
|
-
);
|
|
99
|
-
if (config.default !== void 0) schema2 = schema2.default(config.default);
|
|
100
|
-
if (!config.nullable && config.default !== null)
|
|
101
|
-
schema2 = schema2.test(
|
|
102
|
-
"nullable",
|
|
103
|
-
({ path }) => (error_messages?.base?.nullable ?? "").split("{path}").join(path),
|
|
104
|
-
(value) => value !== null
|
|
105
|
-
);
|
|
106
|
-
if (config.required) schema2 = schema2.required(({ path }) => (error_messages?.base?.required ?? "").split("{path}").join(path));
|
|
107
|
-
return schema2;
|
|
108
127
|
};
|
|
109
|
-
const
|
|
110
|
-
for (const [key, config] of Object.entries(schema))
|
|
111
|
-
return Yup.object().shape(
|
|
128
|
+
const properties = {};
|
|
129
|
+
for (const [key, config] of Object.entries(schema)) properties[key] = build(key, config);
|
|
130
|
+
return Yup.object().shape(properties);
|
|
112
131
|
};
|
|
113
132
|
|
|
114
133
|
// src/utils/ConvertToJSONSchema.util.ts
|
|
@@ -220,7 +239,7 @@ __export(Patterns_barrel_exports, {
|
|
|
220
239
|
});
|
|
221
240
|
|
|
222
241
|
// src/patterns/Email.pattern.ts
|
|
223
|
-
var Email = "^[a-zA-Z0-9._%-]+@[a-zA-Z0-9
|
|
242
|
+
var Email = "^[a-zA-Z0-9._%-]+@([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}$";
|
|
224
243
|
|
|
225
244
|
// src/patterns/PhoneNumber.pattern.ts
|
|
226
245
|
var PhoneNumber = "^\\d{4}\\d{7,12}$";
|
package/dist/main.mjs
CHANGED
|
@@ -10,11 +10,34 @@ import _ from "lodash";
|
|
|
10
10
|
// src/utils/ConvertToYup.util.ts
|
|
11
11
|
import * as Yup from "yup";
|
|
12
12
|
var convertToYup = (schema, error_messages) => {
|
|
13
|
+
const base = (schema2, key, config) => {
|
|
14
|
+
schema2 = schema2.nullable();
|
|
15
|
+
if (config.required)
|
|
16
|
+
schema2 = schema2.test(
|
|
17
|
+
"required",
|
|
18
|
+
({ path }) => (error_messages?.base?.required ?? "").split("{path}").join(path),
|
|
19
|
+
(property) => {
|
|
20
|
+
if (property === void 0) return false;
|
|
21
|
+
if (typeof property === "string" && property.trim() === "") return false;
|
|
22
|
+
if (Array.isArray(property) && property.length === 0) return false;
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
);
|
|
26
|
+
if (!config.nullable) schema2 = schema2.nonNullable(({ path }) => (error_messages?.base?.nullable ?? "").split("{path}").join(path));
|
|
27
|
+
if (config.default !== void 0) schema2 = schema2.default(config.default);
|
|
28
|
+
if (config.pattern !== void 0 && schema2.matches !== void 0)
|
|
29
|
+
schema2 = schema2.matches(
|
|
30
|
+
new RegExp(config.pattern ?? "[\\s\\S]*"),
|
|
31
|
+
({ path }) => (error_messages?.base?.pattern ?? "").split("{path}").join(path).split("{pattern}").join(new RegExp(config.pattern ?? "[\\s\\S]*").source)
|
|
32
|
+
);
|
|
33
|
+
return schema2;
|
|
34
|
+
};
|
|
13
35
|
const build = (key, config) => {
|
|
14
36
|
let schema2;
|
|
15
37
|
if (config.type === "string") {
|
|
16
38
|
schema2 = Yup.string().typeError(({ path }) => (error_messages?.string?.type ?? "").split("{path}").join(path));
|
|
17
|
-
schema2 = schema2
|
|
39
|
+
schema2 = base(schema2, key, config);
|
|
40
|
+
schema2 = schema2.transform((property) => typeof property === "string" ? property.trim() : property);
|
|
18
41
|
if (config.min !== void 0)
|
|
19
42
|
schema2 = schema2.min(
|
|
20
43
|
config.min,
|
|
@@ -25,28 +48,38 @@ var convertToYup = (schema, error_messages) => {
|
|
|
25
48
|
config.max,
|
|
26
49
|
({ path, max }) => (error_messages?.string?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()).split("{plural_suffix}").join(max > 1 ? "s" : "")
|
|
27
50
|
);
|
|
28
|
-
if (config.lowercase === true) schema2 = schema2.
|
|
29
|
-
if (config.uppercase === true) schema2 = schema2.
|
|
51
|
+
if (config.lowercase === true) schema2 = schema2.transform((property) => typeof property === "string" ? property.toLowerCase() : property);
|
|
52
|
+
if (config.uppercase === true) schema2 = schema2.transform((property) => typeof property === "string" ? property.toUpperCase() : property);
|
|
53
|
+
return schema2;
|
|
30
54
|
} else if (config.type === "number") {
|
|
31
55
|
schema2 = Yup.number().typeError(({ path }) => (error_messages?.number?.type ?? "").split("{path}").join(path));
|
|
56
|
+
schema2 = base(schema2, key, config);
|
|
32
57
|
if (config.min !== void 0) schema2 = schema2.min(config.min, ({ path, min }) => (error_messages?.number?.min ?? "").split("{path}").join(path).split("{min}").join(min.toString()));
|
|
33
58
|
if (config.max !== void 0) schema2 = schema2.max(config.max, ({ path, max }) => (error_messages?.number?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()));
|
|
34
59
|
if (config.integer === true) schema2 = schema2.integer(({ path }) => (error_messages?.number?.integer ?? "").split("{path}").join(path));
|
|
35
60
|
if (config.positive === true) schema2 = schema2.positive(({ path }) => (error_messages?.number?.positive ?? "").split("{path}").join(path));
|
|
36
61
|
if (config.negative === true) schema2 = schema2.negative(({ path }) => (error_messages?.number?.negative ?? "").split("{path}").join(path));
|
|
62
|
+
return schema2;
|
|
37
63
|
} else if (config.type === "boolean") {
|
|
38
64
|
schema2 = Yup.boolean().typeError(({ path }) => (error_messages?.boolean?.type ?? "").split("{path}").join(path));
|
|
65
|
+
schema2 = base(schema2, key, config);
|
|
66
|
+
return schema2;
|
|
39
67
|
} else if (config.type === "date") {
|
|
40
68
|
schema2 = Yup.date().typeError(({ path }) => (error_messages?.date?.type ?? "").split("{path}").join(path));
|
|
69
|
+
schema2 = base(schema2, key, config);
|
|
41
70
|
if (config.min !== void 0) schema2 = schema2.min(config.min, ({ path, min }) => (error_messages?.date?.min ?? "").split("{path}").join(path).split("{min}").join(new Date(min).toISOString()));
|
|
42
71
|
if (config.max !== void 0) schema2 = schema2.max(config.max, ({ path, max }) => (error_messages?.date?.max ?? "").split("{path}").join(path).split("{max}").join(new Date(max).toISOString()));
|
|
72
|
+
return schema2;
|
|
43
73
|
} else if (config.type === "object") {
|
|
44
74
|
schema2 = Yup.object().typeError(({ path }) => (error_messages?.object?.type ?? "").split("{path}").join(path));
|
|
75
|
+
schema2 = base(schema2, key, config);
|
|
45
76
|
const nested_properties = {};
|
|
46
77
|
for (const [nested_key, nested_config] of Object.entries(config.properties)) nested_properties[nested_key] = build(nested_key, nested_config);
|
|
47
78
|
schema2 = schema2.shape(nested_properties);
|
|
79
|
+
return schema2;
|
|
48
80
|
} else if (config.type === "array") {
|
|
49
81
|
schema2 = Yup.array().typeError(({ path }) => (error_messages?.array?.type ?? "").split("{path}").join(path));
|
|
82
|
+
schema2 = base(schema2, key, config);
|
|
50
83
|
if (config.min !== void 0)
|
|
51
84
|
schema2 = schema2.min(
|
|
52
85
|
config.min,
|
|
@@ -58,26 +91,12 @@ var convertToYup = (schema, error_messages) => {
|
|
|
58
91
|
({ path, max }) => (error_messages?.array?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()).split("{plural_suffix}").join(max > 1 ? "s" : "")
|
|
59
92
|
);
|
|
60
93
|
schema2 = schema2.of(build(key, config.items));
|
|
94
|
+
return schema2;
|
|
61
95
|
} else throw new Error(`Unsupported schema type for ${key}`);
|
|
62
|
-
schema2 = schema2.nullable();
|
|
63
|
-
if (config.pattern !== void 0 && schema2.matches !== void 0)
|
|
64
|
-
schema2 = schema2.matches(
|
|
65
|
-
new RegExp(config.pattern ?? "[\\s\\S]*"),
|
|
66
|
-
({ path }) => (error_messages?.base?.pattern ?? "").split("{path}").join(path).split("{pattern}").join(new RegExp(config.pattern ?? "[\\s\\S]*").source)
|
|
67
|
-
);
|
|
68
|
-
if (config.default !== void 0) schema2 = schema2.default(config.default);
|
|
69
|
-
if (!config.nullable && config.default !== null)
|
|
70
|
-
schema2 = schema2.test(
|
|
71
|
-
"nullable",
|
|
72
|
-
({ path }) => (error_messages?.base?.nullable ?? "").split("{path}").join(path),
|
|
73
|
-
(value) => value !== null
|
|
74
|
-
);
|
|
75
|
-
if (config.required) schema2 = schema2.required(({ path }) => (error_messages?.base?.required ?? "").split("{path}").join(path));
|
|
76
|
-
return schema2;
|
|
77
96
|
};
|
|
78
|
-
const
|
|
79
|
-
for (const [key, config] of Object.entries(schema))
|
|
80
|
-
return Yup.object().shape(
|
|
97
|
+
const properties = {};
|
|
98
|
+
for (const [key, config] of Object.entries(schema)) properties[key] = build(key, config);
|
|
99
|
+
return Yup.object().shape(properties);
|
|
81
100
|
};
|
|
82
101
|
|
|
83
102
|
// src/utils/ConvertToJSONSchema.util.ts
|
|
@@ -189,7 +208,7 @@ __export(Patterns_barrel_exports, {
|
|
|
189
208
|
});
|
|
190
209
|
|
|
191
210
|
// src/patterns/Email.pattern.ts
|
|
192
|
-
var Email = "^[a-zA-Z0-9._%-]+@[a-zA-Z0-9
|
|
211
|
+
var Email = "^[a-zA-Z0-9._%-]+@([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}$";
|
|
193
212
|
|
|
194
213
|
// src/patterns/PhoneNumber.pattern.ts
|
|
195
214
|
var PhoneNumber = "^\\d{4}\\d{7,12}$";
|
|
@@ -23,7 +23,7 @@ __export(Email_pattern_exports, {
|
|
|
23
23
|
Email: () => Email
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(Email_pattern_exports);
|
|
26
|
-
var Email = "^[a-zA-Z0-9._%-]+@[a-zA-Z0-9
|
|
26
|
+
var Email = "^[a-zA-Z0-9._%-]+@([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}$";
|
|
27
27
|
// Annotate the CommonJS export names for ESM import in node:
|
|
28
28
|
0 && (module.exports = {
|
|
29
29
|
Email
|
|
@@ -35,11 +35,34 @@ __export(ConvertToYup_util_exports, {
|
|
|
35
35
|
module.exports = __toCommonJS(ConvertToYup_util_exports);
|
|
36
36
|
var Yup = __toESM(require("yup"));
|
|
37
37
|
var convertToYup = (schema, error_messages) => {
|
|
38
|
+
const base = (schema2, key, config) => {
|
|
39
|
+
schema2 = schema2.nullable();
|
|
40
|
+
if (config.required)
|
|
41
|
+
schema2 = schema2.test(
|
|
42
|
+
"required",
|
|
43
|
+
({ path }) => (error_messages?.base?.required ?? "").split("{path}").join(path),
|
|
44
|
+
(property) => {
|
|
45
|
+
if (property === void 0) return false;
|
|
46
|
+
if (typeof property === "string" && property.trim() === "") return false;
|
|
47
|
+
if (Array.isArray(property) && property.length === 0) return false;
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
);
|
|
51
|
+
if (!config.nullable) schema2 = schema2.nonNullable(({ path }) => (error_messages?.base?.nullable ?? "").split("{path}").join(path));
|
|
52
|
+
if (config.default !== void 0) schema2 = schema2.default(config.default);
|
|
53
|
+
if (config.pattern !== void 0 && schema2.matches !== void 0)
|
|
54
|
+
schema2 = schema2.matches(
|
|
55
|
+
new RegExp(config.pattern ?? "[\\s\\S]*"),
|
|
56
|
+
({ path }) => (error_messages?.base?.pattern ?? "").split("{path}").join(path).split("{pattern}").join(new RegExp(config.pattern ?? "[\\s\\S]*").source)
|
|
57
|
+
);
|
|
58
|
+
return schema2;
|
|
59
|
+
};
|
|
38
60
|
const build = (key, config) => {
|
|
39
61
|
let schema2;
|
|
40
62
|
if (config.type === "string") {
|
|
41
63
|
schema2 = Yup.string().typeError(({ path }) => (error_messages?.string?.type ?? "").split("{path}").join(path));
|
|
42
|
-
schema2 = schema2
|
|
64
|
+
schema2 = base(schema2, key, config);
|
|
65
|
+
schema2 = schema2.transform((property) => typeof property === "string" ? property.trim() : property);
|
|
43
66
|
if (config.min !== void 0)
|
|
44
67
|
schema2 = schema2.min(
|
|
45
68
|
config.min,
|
|
@@ -50,28 +73,38 @@ var convertToYup = (schema, error_messages) => {
|
|
|
50
73
|
config.max,
|
|
51
74
|
({ path, max }) => (error_messages?.string?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()).split("{plural_suffix}").join(max > 1 ? "s" : "")
|
|
52
75
|
);
|
|
53
|
-
if (config.lowercase === true) schema2 = schema2.
|
|
54
|
-
if (config.uppercase === true) schema2 = schema2.
|
|
76
|
+
if (config.lowercase === true) schema2 = schema2.transform((property) => typeof property === "string" ? property.toLowerCase() : property);
|
|
77
|
+
if (config.uppercase === true) schema2 = schema2.transform((property) => typeof property === "string" ? property.toUpperCase() : property);
|
|
78
|
+
return schema2;
|
|
55
79
|
} else if (config.type === "number") {
|
|
56
80
|
schema2 = Yup.number().typeError(({ path }) => (error_messages?.number?.type ?? "").split("{path}").join(path));
|
|
81
|
+
schema2 = base(schema2, key, config);
|
|
57
82
|
if (config.min !== void 0) schema2 = schema2.min(config.min, ({ path, min }) => (error_messages?.number?.min ?? "").split("{path}").join(path).split("{min}").join(min.toString()));
|
|
58
83
|
if (config.max !== void 0) schema2 = schema2.max(config.max, ({ path, max }) => (error_messages?.number?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()));
|
|
59
84
|
if (config.integer === true) schema2 = schema2.integer(({ path }) => (error_messages?.number?.integer ?? "").split("{path}").join(path));
|
|
60
85
|
if (config.positive === true) schema2 = schema2.positive(({ path }) => (error_messages?.number?.positive ?? "").split("{path}").join(path));
|
|
61
86
|
if (config.negative === true) schema2 = schema2.negative(({ path }) => (error_messages?.number?.negative ?? "").split("{path}").join(path));
|
|
87
|
+
return schema2;
|
|
62
88
|
} else if (config.type === "boolean") {
|
|
63
89
|
schema2 = Yup.boolean().typeError(({ path }) => (error_messages?.boolean?.type ?? "").split("{path}").join(path));
|
|
90
|
+
schema2 = base(schema2, key, config);
|
|
91
|
+
return schema2;
|
|
64
92
|
} else if (config.type === "date") {
|
|
65
93
|
schema2 = Yup.date().typeError(({ path }) => (error_messages?.date?.type ?? "").split("{path}").join(path));
|
|
94
|
+
schema2 = base(schema2, key, config);
|
|
66
95
|
if (config.min !== void 0) schema2 = schema2.min(config.min, ({ path, min }) => (error_messages?.date?.min ?? "").split("{path}").join(path).split("{min}").join(new Date(min).toISOString()));
|
|
67
96
|
if (config.max !== void 0) schema2 = schema2.max(config.max, ({ path, max }) => (error_messages?.date?.max ?? "").split("{path}").join(path).split("{max}").join(new Date(max).toISOString()));
|
|
97
|
+
return schema2;
|
|
68
98
|
} else if (config.type === "object") {
|
|
69
99
|
schema2 = Yup.object().typeError(({ path }) => (error_messages?.object?.type ?? "").split("{path}").join(path));
|
|
100
|
+
schema2 = base(schema2, key, config);
|
|
70
101
|
const nested_properties = {};
|
|
71
102
|
for (const [nested_key, nested_config] of Object.entries(config.properties)) nested_properties[nested_key] = build(nested_key, nested_config);
|
|
72
103
|
schema2 = schema2.shape(nested_properties);
|
|
104
|
+
return schema2;
|
|
73
105
|
} else if (config.type === "array") {
|
|
74
106
|
schema2 = Yup.array().typeError(({ path }) => (error_messages?.array?.type ?? "").split("{path}").join(path));
|
|
107
|
+
schema2 = base(schema2, key, config);
|
|
75
108
|
if (config.min !== void 0)
|
|
76
109
|
schema2 = schema2.min(
|
|
77
110
|
config.min,
|
|
@@ -83,26 +116,12 @@ var convertToYup = (schema, error_messages) => {
|
|
|
83
116
|
({ path, max }) => (error_messages?.array?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()).split("{plural_suffix}").join(max > 1 ? "s" : "")
|
|
84
117
|
);
|
|
85
118
|
schema2 = schema2.of(build(key, config.items));
|
|
119
|
+
return schema2;
|
|
86
120
|
} else throw new Error(`Unsupported schema type for ${key}`);
|
|
87
|
-
schema2 = schema2.nullable();
|
|
88
|
-
if (config.pattern !== void 0 && schema2.matches !== void 0)
|
|
89
|
-
schema2 = schema2.matches(
|
|
90
|
-
new RegExp(config.pattern ?? "[\\s\\S]*"),
|
|
91
|
-
({ path }) => (error_messages?.base?.pattern ?? "").split("{path}").join(path).split("{pattern}").join(new RegExp(config.pattern ?? "[\\s\\S]*").source)
|
|
92
|
-
);
|
|
93
|
-
if (config.default !== void 0) schema2 = schema2.default(config.default);
|
|
94
|
-
if (!config.nullable && config.default !== null)
|
|
95
|
-
schema2 = schema2.test(
|
|
96
|
-
"nullable",
|
|
97
|
-
({ path }) => (error_messages?.base?.nullable ?? "").split("{path}").join(path),
|
|
98
|
-
(value) => value !== null
|
|
99
|
-
);
|
|
100
|
-
if (config.required) schema2 = schema2.required(({ path }) => (error_messages?.base?.required ?? "").split("{path}").join(path));
|
|
101
|
-
return schema2;
|
|
102
121
|
};
|
|
103
|
-
const
|
|
104
|
-
for (const [key, config] of Object.entries(schema))
|
|
105
|
-
return Yup.object().shape(
|
|
122
|
+
const properties = {};
|
|
123
|
+
for (const [key, config] of Object.entries(schema)) properties[key] = build(key, config);
|
|
124
|
+
return Yup.object().shape(properties);
|
|
106
125
|
};
|
|
107
126
|
// Annotate the CommonJS export names for ESM import in node:
|
|
108
127
|
0 && (module.exports = {
|
|
@@ -1,11 +1,34 @@
|
|
|
1
1
|
// src/utils/ConvertToYup.util.ts
|
|
2
2
|
import * as Yup from "yup";
|
|
3
3
|
var convertToYup = (schema, error_messages) => {
|
|
4
|
+
const base = (schema2, key, config) => {
|
|
5
|
+
schema2 = schema2.nullable();
|
|
6
|
+
if (config.required)
|
|
7
|
+
schema2 = schema2.test(
|
|
8
|
+
"required",
|
|
9
|
+
({ path }) => (error_messages?.base?.required ?? "").split("{path}").join(path),
|
|
10
|
+
(property) => {
|
|
11
|
+
if (property === void 0) return false;
|
|
12
|
+
if (typeof property === "string" && property.trim() === "") return false;
|
|
13
|
+
if (Array.isArray(property) && property.length === 0) return false;
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
);
|
|
17
|
+
if (!config.nullable) schema2 = schema2.nonNullable(({ path }) => (error_messages?.base?.nullable ?? "").split("{path}").join(path));
|
|
18
|
+
if (config.default !== void 0) schema2 = schema2.default(config.default);
|
|
19
|
+
if (config.pattern !== void 0 && schema2.matches !== void 0)
|
|
20
|
+
schema2 = schema2.matches(
|
|
21
|
+
new RegExp(config.pattern ?? "[\\s\\S]*"),
|
|
22
|
+
({ path }) => (error_messages?.base?.pattern ?? "").split("{path}").join(path).split("{pattern}").join(new RegExp(config.pattern ?? "[\\s\\S]*").source)
|
|
23
|
+
);
|
|
24
|
+
return schema2;
|
|
25
|
+
};
|
|
4
26
|
const build = (key, config) => {
|
|
5
27
|
let schema2;
|
|
6
28
|
if (config.type === "string") {
|
|
7
29
|
schema2 = Yup.string().typeError(({ path }) => (error_messages?.string?.type ?? "").split("{path}").join(path));
|
|
8
|
-
schema2 = schema2
|
|
30
|
+
schema2 = base(schema2, key, config);
|
|
31
|
+
schema2 = schema2.transform((property) => typeof property === "string" ? property.trim() : property);
|
|
9
32
|
if (config.min !== void 0)
|
|
10
33
|
schema2 = schema2.min(
|
|
11
34
|
config.min,
|
|
@@ -16,28 +39,38 @@ var convertToYup = (schema, error_messages) => {
|
|
|
16
39
|
config.max,
|
|
17
40
|
({ path, max }) => (error_messages?.string?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()).split("{plural_suffix}").join(max > 1 ? "s" : "")
|
|
18
41
|
);
|
|
19
|
-
if (config.lowercase === true) schema2 = schema2.
|
|
20
|
-
if (config.uppercase === true) schema2 = schema2.
|
|
42
|
+
if (config.lowercase === true) schema2 = schema2.transform((property) => typeof property === "string" ? property.toLowerCase() : property);
|
|
43
|
+
if (config.uppercase === true) schema2 = schema2.transform((property) => typeof property === "string" ? property.toUpperCase() : property);
|
|
44
|
+
return schema2;
|
|
21
45
|
} else if (config.type === "number") {
|
|
22
46
|
schema2 = Yup.number().typeError(({ path }) => (error_messages?.number?.type ?? "").split("{path}").join(path));
|
|
47
|
+
schema2 = base(schema2, key, config);
|
|
23
48
|
if (config.min !== void 0) schema2 = schema2.min(config.min, ({ path, min }) => (error_messages?.number?.min ?? "").split("{path}").join(path).split("{min}").join(min.toString()));
|
|
24
49
|
if (config.max !== void 0) schema2 = schema2.max(config.max, ({ path, max }) => (error_messages?.number?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()));
|
|
25
50
|
if (config.integer === true) schema2 = schema2.integer(({ path }) => (error_messages?.number?.integer ?? "").split("{path}").join(path));
|
|
26
51
|
if (config.positive === true) schema2 = schema2.positive(({ path }) => (error_messages?.number?.positive ?? "").split("{path}").join(path));
|
|
27
52
|
if (config.negative === true) schema2 = schema2.negative(({ path }) => (error_messages?.number?.negative ?? "").split("{path}").join(path));
|
|
53
|
+
return schema2;
|
|
28
54
|
} else if (config.type === "boolean") {
|
|
29
55
|
schema2 = Yup.boolean().typeError(({ path }) => (error_messages?.boolean?.type ?? "").split("{path}").join(path));
|
|
56
|
+
schema2 = base(schema2, key, config);
|
|
57
|
+
return schema2;
|
|
30
58
|
} else if (config.type === "date") {
|
|
31
59
|
schema2 = Yup.date().typeError(({ path }) => (error_messages?.date?.type ?? "").split("{path}").join(path));
|
|
60
|
+
schema2 = base(schema2, key, config);
|
|
32
61
|
if (config.min !== void 0) schema2 = schema2.min(config.min, ({ path, min }) => (error_messages?.date?.min ?? "").split("{path}").join(path).split("{min}").join(new Date(min).toISOString()));
|
|
33
62
|
if (config.max !== void 0) schema2 = schema2.max(config.max, ({ path, max }) => (error_messages?.date?.max ?? "").split("{path}").join(path).split("{max}").join(new Date(max).toISOString()));
|
|
63
|
+
return schema2;
|
|
34
64
|
} else if (config.type === "object") {
|
|
35
65
|
schema2 = Yup.object().typeError(({ path }) => (error_messages?.object?.type ?? "").split("{path}").join(path));
|
|
66
|
+
schema2 = base(schema2, key, config);
|
|
36
67
|
const nested_properties = {};
|
|
37
68
|
for (const [nested_key, nested_config] of Object.entries(config.properties)) nested_properties[nested_key] = build(nested_key, nested_config);
|
|
38
69
|
schema2 = schema2.shape(nested_properties);
|
|
70
|
+
return schema2;
|
|
39
71
|
} else if (config.type === "array") {
|
|
40
72
|
schema2 = Yup.array().typeError(({ path }) => (error_messages?.array?.type ?? "").split("{path}").join(path));
|
|
73
|
+
schema2 = base(schema2, key, config);
|
|
41
74
|
if (config.min !== void 0)
|
|
42
75
|
schema2 = schema2.min(
|
|
43
76
|
config.min,
|
|
@@ -49,26 +82,12 @@ var convertToYup = (schema, error_messages) => {
|
|
|
49
82
|
({ path, max }) => (error_messages?.array?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()).split("{plural_suffix}").join(max > 1 ? "s" : "")
|
|
50
83
|
);
|
|
51
84
|
schema2 = schema2.of(build(key, config.items));
|
|
85
|
+
return schema2;
|
|
52
86
|
} else throw new Error(`Unsupported schema type for ${key}`);
|
|
53
|
-
schema2 = schema2.nullable();
|
|
54
|
-
if (config.pattern !== void 0 && schema2.matches !== void 0)
|
|
55
|
-
schema2 = schema2.matches(
|
|
56
|
-
new RegExp(config.pattern ?? "[\\s\\S]*"),
|
|
57
|
-
({ path }) => (error_messages?.base?.pattern ?? "").split("{path}").join(path).split("{pattern}").join(new RegExp(config.pattern ?? "[\\s\\S]*").source)
|
|
58
|
-
);
|
|
59
|
-
if (config.default !== void 0) schema2 = schema2.default(config.default);
|
|
60
|
-
if (!config.nullable && config.default !== null)
|
|
61
|
-
schema2 = schema2.test(
|
|
62
|
-
"nullable",
|
|
63
|
-
({ path }) => (error_messages?.base?.nullable ?? "").split("{path}").join(path),
|
|
64
|
-
(value) => value !== null
|
|
65
|
-
);
|
|
66
|
-
if (config.required) schema2 = schema2.required(({ path }) => (error_messages?.base?.required ?? "").split("{path}").join(path));
|
|
67
|
-
return schema2;
|
|
68
87
|
};
|
|
69
|
-
const
|
|
70
|
-
for (const [key, config] of Object.entries(schema))
|
|
71
|
-
return Yup.object().shape(
|
|
88
|
+
const properties = {};
|
|
89
|
+
for (const [key, config] of Object.entries(schema)) properties[key] = build(key, config);
|
|
90
|
+
return Yup.object().shape(properties);
|
|
72
91
|
};
|
|
73
92
|
export {
|
|
74
93
|
convertToYup
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "yuppi",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "Schemas that can be converted to Yup and JSON Schema.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://github.com/keift/yuppi",
|
|
@@ -9,15 +9,14 @@
|
|
|
9
9
|
"module": "./dist/main.mjs",
|
|
10
10
|
"types": "./dist/main.d.ts",
|
|
11
11
|
"scripts": {
|
|
12
|
-
"
|
|
13
|
-
"tests": "bun run lint && npm run ./tests/ConvertToJSONSchema.test.ts && npm run ./tests/Validate.test.ts && npm run ./tests/ValidationError.test.ts",
|
|
12
|
+
"tests": "bun run lint && bun test",
|
|
14
13
|
"prettier": "prettier --write ./",
|
|
15
14
|
"build": "tsup",
|
|
16
15
|
"lint": "eslint ./"
|
|
17
16
|
},
|
|
18
17
|
"dependencies": {
|
|
19
18
|
"@sinclair/typebox": "^0.34.35",
|
|
20
|
-
"@types/lodash": "^4.17.
|
|
19
|
+
"@types/lodash": "^4.17.18",
|
|
21
20
|
"lodash": "^4.17.21",
|
|
22
21
|
"yup": "^1.6.1"
|
|
23
22
|
},
|