yuppi 1.2.7 → 1.2.8
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 +4 -2
- package/dist/main.d.mts +4 -4
- package/dist/main.d.ts +4 -4
- package/dist/main.js +47 -42
- package/dist/main.mjs +47 -42
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -203,7 +203,9 @@ Validate the properties with your Yuppi schema.
|
|
|
203
203
|
> }
|
|
204
204
|
> */
|
|
205
205
|
> } catch (error) {
|
|
206
|
-
>
|
|
206
|
+
> const message = (error as ValidationError).message;
|
|
207
|
+
>
|
|
208
|
+
> console.log(message); // "Field email must match the required pattern ^[a-zA-Z0-9._-]+@([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}$"
|
|
207
209
|
> }
|
|
208
210
|
> ```
|
|
209
211
|
|
|
@@ -305,7 +307,7 @@ Convert your Yuppi schema into [JSON Schema](https://json-schema.org).
|
|
|
305
307
|
> maxLength: 32,
|
|
306
308
|
> pattern: "[\\s\\S]*"
|
|
307
309
|
> },
|
|
308
|
-
> username
|
|
310
|
+
> username: {
|
|
309
311
|
> type: "string",
|
|
310
312
|
> minLength: 3,
|
|
311
313
|
> maxLength: 16,
|
package/dist/main.d.mts
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import * as yup from 'yup';
|
|
2
2
|
import { AnyObject as AnyObject$1, ValidateOptions as ValidateOptions$1, ValidationError as ValidationError$1 } from 'yup';
|
|
3
|
-
import { TObject } from '@sinclair/typebox';
|
|
3
|
+
import { TObject, TAnySchema } from '@sinclair/typebox';
|
|
4
4
|
|
|
5
5
|
type AnyObject = AnyObject$1;
|
|
6
6
|
|
|
7
|
-
type JSONSchema = TObject
|
|
7
|
+
type JSONSchema = TObject<Record<string, TAnySchema>>;
|
|
8
8
|
|
|
9
9
|
type Base = {
|
|
10
|
-
pattern?: string;
|
|
11
10
|
default?: unknown;
|
|
12
11
|
nullable: boolean;
|
|
13
12
|
required: boolean;
|
|
@@ -15,6 +14,7 @@ type Base = {
|
|
|
15
14
|
type String = Base & {
|
|
16
15
|
type: 'string';
|
|
17
16
|
enum?: string[];
|
|
17
|
+
pattern?: string;
|
|
18
18
|
min?: number;
|
|
19
19
|
max?: number;
|
|
20
20
|
lowercase?: boolean;
|
|
@@ -55,13 +55,13 @@ type ValidateOptions = ValidateOptions$1;
|
|
|
55
55
|
type YuppiOptions = {
|
|
56
56
|
error_messages?: {
|
|
57
57
|
base?: {
|
|
58
|
-
pattern?: string;
|
|
59
58
|
nullable?: string;
|
|
60
59
|
required?: string;
|
|
61
60
|
};
|
|
62
61
|
string?: {
|
|
63
62
|
type?: string;
|
|
64
63
|
enum?: string;
|
|
64
|
+
pattern?: string;
|
|
65
65
|
min?: string;
|
|
66
66
|
max?: string;
|
|
67
67
|
};
|
package/dist/main.d.ts
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import * as yup from 'yup';
|
|
2
2
|
import { AnyObject as AnyObject$1, ValidateOptions as ValidateOptions$1, ValidationError as ValidationError$1 } from 'yup';
|
|
3
|
-
import { TObject } from '@sinclair/typebox';
|
|
3
|
+
import { TObject, TAnySchema } from '@sinclair/typebox';
|
|
4
4
|
|
|
5
5
|
type AnyObject = AnyObject$1;
|
|
6
6
|
|
|
7
|
-
type JSONSchema = TObject
|
|
7
|
+
type JSONSchema = TObject<Record<string, TAnySchema>>;
|
|
8
8
|
|
|
9
9
|
type Base = {
|
|
10
|
-
pattern?: string;
|
|
11
10
|
default?: unknown;
|
|
12
11
|
nullable: boolean;
|
|
13
12
|
required: boolean;
|
|
@@ -15,6 +14,7 @@ type Base = {
|
|
|
15
14
|
type String = Base & {
|
|
16
15
|
type: 'string';
|
|
17
16
|
enum?: string[];
|
|
17
|
+
pattern?: string;
|
|
18
18
|
min?: number;
|
|
19
19
|
max?: number;
|
|
20
20
|
lowercase?: boolean;
|
|
@@ -55,13 +55,13 @@ type ValidateOptions = ValidateOptions$1;
|
|
|
55
55
|
type YuppiOptions = {
|
|
56
56
|
error_messages?: {
|
|
57
57
|
base?: {
|
|
58
|
-
pattern?: string;
|
|
59
58
|
nullable?: string;
|
|
60
59
|
required?: string;
|
|
61
60
|
};
|
|
62
61
|
string?: {
|
|
63
62
|
type?: string;
|
|
64
63
|
enum?: string;
|
|
64
|
+
pattern?: string;
|
|
65
65
|
min?: string;
|
|
66
66
|
max?: string;
|
|
67
67
|
};
|
package/dist/main.js
CHANGED
|
@@ -60,12 +60,7 @@ var convertToYup = (schema, error_messages) => {
|
|
|
60
60
|
}
|
|
61
61
|
);
|
|
62
62
|
if (!config.nullable && config.default !== null) schema2 = schema2.nonNullable(({ path }) => (error_messages?.base?.nullable ?? "").split("{path}").join(path));
|
|
63
|
-
if (config.default
|
|
64
|
-
if (config.pattern !== void 0 && schema2.matches !== void 0)
|
|
65
|
-
schema2 = schema2.matches(
|
|
66
|
-
new RegExp(config.pattern ?? Any),
|
|
67
|
-
({ path }) => (error_messages?.base?.pattern ?? "").split("{path}").join(path).split("{pattern}").join(new RegExp(config.pattern ?? Any).source)
|
|
68
|
-
);
|
|
63
|
+
if (config.default) schema2 = schema2.default(config.default);
|
|
69
64
|
return schema2;
|
|
70
65
|
};
|
|
71
66
|
const build = (key, config) => {
|
|
@@ -74,33 +69,38 @@ var convertToYup = (schema, error_messages) => {
|
|
|
74
69
|
schema2 = Yup.string().typeError(({ path }) => (error_messages?.string?.type ?? "").split("{path}").join(path));
|
|
75
70
|
schema2 = base(schema2, key, config);
|
|
76
71
|
schema2 = schema2.transform((property) => typeof property === "string" ? property.trim() : property);
|
|
77
|
-
if (config.enum
|
|
72
|
+
if (config.enum)
|
|
78
73
|
schema2 = schema2.oneOf(
|
|
79
74
|
config.enum.map((item) => item.trim()),
|
|
80
75
|
({ path }) => (error_messages?.string?.enum ?? "").split("{path}").join(path)
|
|
81
76
|
);
|
|
82
|
-
if (config.
|
|
77
|
+
if (config.pattern)
|
|
78
|
+
schema2 = schema2.matches(
|
|
79
|
+
new RegExp(config.pattern ?? Any),
|
|
80
|
+
({ path }) => (error_messages?.string?.pattern ?? "").split("{path}").join(path).split("{pattern}").join(new RegExp(config.pattern ?? Any).source)
|
|
81
|
+
);
|
|
82
|
+
if (config.min)
|
|
83
83
|
schema2 = schema2.min(
|
|
84
84
|
config.min,
|
|
85
85
|
({ path, min }) => (error_messages?.string?.min ?? "").split("{path}").join(path).split("{min}").join(min.toString()).split("{plural_suffix}").join(min > 1 ? "s" : "")
|
|
86
86
|
);
|
|
87
|
-
if (config.max
|
|
87
|
+
if (config.max)
|
|
88
88
|
schema2 = schema2.max(
|
|
89
89
|
config.max,
|
|
90
90
|
({ path, max }) => (error_messages?.string?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()).split("{plural_suffix}").join(max > 1 ? "s" : "")
|
|
91
91
|
);
|
|
92
|
-
if (config.lowercase
|
|
93
|
-
if (config.uppercase
|
|
92
|
+
if (config.lowercase) schema2 = schema2.transform((property) => typeof property === "string" ? property.toLowerCase() : property);
|
|
93
|
+
if (config.uppercase) schema2 = schema2.transform((property) => typeof property === "string" ? property.toUpperCase() : property);
|
|
94
94
|
return schema2;
|
|
95
95
|
} else if (config.type === "number") {
|
|
96
96
|
schema2 = Yup.number().typeError(({ path }) => (error_messages?.number?.type ?? "").split("{path}").join(path));
|
|
97
97
|
schema2 = base(schema2, key, config);
|
|
98
|
-
if (config.enum
|
|
99
|
-
if (config.min
|
|
100
|
-
if (config.max
|
|
101
|
-
if (config.integer
|
|
102
|
-
if (config.positive
|
|
103
|
-
if (config.negative
|
|
98
|
+
if (config.enum) schema2 = schema2.oneOf(config.enum, ({ path }) => (error_messages?.number?.enum ?? "").split("{path}").join(path));
|
|
99
|
+
if (config.min) schema2 = schema2.min(config.min, ({ path, min }) => (error_messages?.number?.min ?? "").split("{path}").join(path).split("{min}").join(min.toString()));
|
|
100
|
+
if (config.max) schema2 = schema2.max(config.max, ({ path, max }) => (error_messages?.number?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()));
|
|
101
|
+
if (config.integer) schema2 = schema2.integer(({ path }) => (error_messages?.number?.integer ?? "").split("{path}").join(path));
|
|
102
|
+
if (config.positive) schema2 = schema2.positive(({ path }) => (error_messages?.number?.positive ?? "").split("{path}").join(path));
|
|
103
|
+
if (config.negative) schema2 = schema2.negative(({ path }) => (error_messages?.number?.negative ?? "").split("{path}").join(path));
|
|
104
104
|
return schema2;
|
|
105
105
|
} else if (config.type === "boolean") {
|
|
106
106
|
schema2 = Yup.boolean().typeError(({ path }) => (error_messages?.boolean?.type ?? "").split("{path}").join(path));
|
|
@@ -109,8 +109,8 @@ var convertToYup = (schema, error_messages) => {
|
|
|
109
109
|
} else if (config.type === "date") {
|
|
110
110
|
schema2 = Yup.date().typeError(({ path }) => (error_messages?.date?.type ?? "").split("{path}").join(path));
|
|
111
111
|
schema2 = base(schema2, key, config);
|
|
112
|
-
if (config.min
|
|
113
|
-
if (config.max
|
|
112
|
+
if (config.min) schema2 = schema2.min(config.min, ({ path, min }) => (error_messages?.date?.min ?? "").split("{path}").join(path).split("{min}").join(new Date(min).toISOString()));
|
|
113
|
+
if (config.max) schema2 = schema2.max(config.max, ({ path, max }) => (error_messages?.date?.max ?? "").split("{path}").join(path).split("{max}").join(new Date(max).toISOString()));
|
|
114
114
|
return schema2;
|
|
115
115
|
} else if (config.type === "object") {
|
|
116
116
|
schema2 = Yup.object().typeError(({ path }) => (error_messages?.object?.type ?? "").split("{path}").join(path));
|
|
@@ -122,12 +122,12 @@ var convertToYup = (schema, error_messages) => {
|
|
|
122
122
|
} else if (config.type === "array") {
|
|
123
123
|
schema2 = Yup.array().typeError(({ path }) => (error_messages?.array?.type ?? "").split("{path}").join(path));
|
|
124
124
|
schema2 = base(schema2, key, config);
|
|
125
|
-
if (config.min
|
|
125
|
+
if (config.min)
|
|
126
126
|
schema2 = schema2.min(
|
|
127
127
|
config.min,
|
|
128
128
|
({ path, min }) => (error_messages?.array?.min ?? "").split("{path}").join(path).split("{min}").join(min.toString()).split("{plural_suffix}").join(min > 1 ? "s" : "")
|
|
129
129
|
);
|
|
130
|
-
if (config.max
|
|
130
|
+
if (config.max)
|
|
131
131
|
schema2 = schema2.max(
|
|
132
132
|
config.max,
|
|
133
133
|
({ path, max }) => (error_messages?.array?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()).split("{plural_suffix}").join(max > 1 ? "s" : "")
|
|
@@ -144,33 +144,39 @@ var convertToYup = (schema, error_messages) => {
|
|
|
144
144
|
// src/utils/ConvertToJSONSchema.util.ts
|
|
145
145
|
var import_typebox = require("@sinclair/typebox");
|
|
146
146
|
var convertToJSONSchema = (schema) => {
|
|
147
|
+
const base = (schema2, key, config) => {
|
|
148
|
+
if (!config.required) schema2 = import_typebox.Type.Optional(schema2);
|
|
149
|
+
if (config.nullable || config.default === null) schema2 = import_typebox.Type.Union([schema2, import_typebox.Type.Null()]);
|
|
150
|
+
return schema2;
|
|
151
|
+
};
|
|
147
152
|
const build = (key, config) => {
|
|
153
|
+
let schema2;
|
|
148
154
|
if (config.type === "string") {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
return
|
|
155
|
+
schema2 = import_typebox.Type.String({ enum: config.enum, minLength: config.min, maxLength: config.max, pattern: new RegExp(config.pattern ?? Any).source, default: config.default });
|
|
156
|
+
schema2 = base(schema2, key, config);
|
|
157
|
+
return schema2;
|
|
152
158
|
} else if (config.type === "number") {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
return
|
|
159
|
+
schema2 = config.integer === true ? import_typebox.Type.Integer({ enum: config.enum, minimum: config.min, maximum: config.max, default: config.default }) : import_typebox.Type.Number({ enum: config.enum, minimum: config.min, maximum: config.max, default: config.default });
|
|
160
|
+
schema2 = base(schema2, key, config);
|
|
161
|
+
return schema2;
|
|
156
162
|
} else if (config.type === "boolean") {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
return
|
|
163
|
+
schema2 = import_typebox.Type.Boolean({ default: config.default });
|
|
164
|
+
schema2 = base(schema2, key, config);
|
|
165
|
+
return schema2;
|
|
160
166
|
} else if (config.type === "date") {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
return
|
|
167
|
+
schema2 = import_typebox.Type.String({ format: "date-time", minimum: config.min, maximum: config.max, default: config.default });
|
|
168
|
+
schema2 = base(schema2, key, config);
|
|
169
|
+
return schema2;
|
|
164
170
|
} else if (config.type === "object") {
|
|
165
171
|
const nested_properties = {};
|
|
166
172
|
for (const [nested_key, nested_config] of Object.entries(config.properties)) nested_properties[nested_key] = build(nested_key, nested_config);
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
return
|
|
173
|
+
schema2 = import_typebox.Type.Object(nested_properties);
|
|
174
|
+
schema2 = base(schema2, key, config);
|
|
175
|
+
return schema2;
|
|
170
176
|
} else if (config.type === "array") {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
return
|
|
177
|
+
schema2 = import_typebox.Type.Array(build(key, config.items), { minItems: config.min, maxItems: config.max, default: config.default });
|
|
178
|
+
schema2 = base(schema2, key, config);
|
|
179
|
+
return schema2;
|
|
174
180
|
} else throw new Error(`Unsupported schema type for ${key}`);
|
|
175
181
|
};
|
|
176
182
|
const properties = {};
|
|
@@ -182,13 +188,13 @@ var convertToJSONSchema = (schema) => {
|
|
|
182
188
|
var YuppiOptionsDefault = {
|
|
183
189
|
error_messages: {
|
|
184
190
|
base: {
|
|
185
|
-
pattern: "Field {path} must match the required pattern {pattern}",
|
|
186
191
|
nullable: "Field {path} cannot be null",
|
|
187
192
|
required: "Field {path} is required"
|
|
188
193
|
},
|
|
189
194
|
string: {
|
|
190
195
|
type: "Field {path} must be a string",
|
|
191
196
|
enum: "Field {path} must be one of the allowed values",
|
|
197
|
+
pattern: "Field {path} must match the required pattern {pattern}",
|
|
192
198
|
min: "Field {path} must be at least {min} character{plural_suffix}",
|
|
193
199
|
max: "Field {path} must be at most {max} character{plural_suffix}"
|
|
194
200
|
},
|
|
@@ -231,8 +237,7 @@ var Yuppi = class {
|
|
|
231
237
|
}
|
|
232
238
|
validate(schema, properties) {
|
|
233
239
|
const yup_schema = convertToYup(schema, this.options.error_messages);
|
|
234
|
-
|
|
235
|
-
return validation;
|
|
240
|
+
return yup_schema.validateSync(properties, this.options.validate_options);
|
|
236
241
|
}
|
|
237
242
|
convertToYup(schema) {
|
|
238
243
|
return convertToYup(schema, this.options.error_messages);
|
package/dist/main.mjs
CHANGED
|
@@ -29,12 +29,7 @@ var convertToYup = (schema, error_messages) => {
|
|
|
29
29
|
}
|
|
30
30
|
);
|
|
31
31
|
if (!config.nullable && config.default !== null) schema2 = schema2.nonNullable(({ path }) => (error_messages?.base?.nullable ?? "").split("{path}").join(path));
|
|
32
|
-
if (config.default
|
|
33
|
-
if (config.pattern !== void 0 && schema2.matches !== void 0)
|
|
34
|
-
schema2 = schema2.matches(
|
|
35
|
-
new RegExp(config.pattern ?? Any),
|
|
36
|
-
({ path }) => (error_messages?.base?.pattern ?? "").split("{path}").join(path).split("{pattern}").join(new RegExp(config.pattern ?? Any).source)
|
|
37
|
-
);
|
|
32
|
+
if (config.default) schema2 = schema2.default(config.default);
|
|
38
33
|
return schema2;
|
|
39
34
|
};
|
|
40
35
|
const build = (key, config) => {
|
|
@@ -43,33 +38,38 @@ var convertToYup = (schema, error_messages) => {
|
|
|
43
38
|
schema2 = Yup.string().typeError(({ path }) => (error_messages?.string?.type ?? "").split("{path}").join(path));
|
|
44
39
|
schema2 = base(schema2, key, config);
|
|
45
40
|
schema2 = schema2.transform((property) => typeof property === "string" ? property.trim() : property);
|
|
46
|
-
if (config.enum
|
|
41
|
+
if (config.enum)
|
|
47
42
|
schema2 = schema2.oneOf(
|
|
48
43
|
config.enum.map((item) => item.trim()),
|
|
49
44
|
({ path }) => (error_messages?.string?.enum ?? "").split("{path}").join(path)
|
|
50
45
|
);
|
|
51
|
-
if (config.
|
|
46
|
+
if (config.pattern)
|
|
47
|
+
schema2 = schema2.matches(
|
|
48
|
+
new RegExp(config.pattern ?? Any),
|
|
49
|
+
({ path }) => (error_messages?.string?.pattern ?? "").split("{path}").join(path).split("{pattern}").join(new RegExp(config.pattern ?? Any).source)
|
|
50
|
+
);
|
|
51
|
+
if (config.min)
|
|
52
52
|
schema2 = schema2.min(
|
|
53
53
|
config.min,
|
|
54
54
|
({ path, min }) => (error_messages?.string?.min ?? "").split("{path}").join(path).split("{min}").join(min.toString()).split("{plural_suffix}").join(min > 1 ? "s" : "")
|
|
55
55
|
);
|
|
56
|
-
if (config.max
|
|
56
|
+
if (config.max)
|
|
57
57
|
schema2 = schema2.max(
|
|
58
58
|
config.max,
|
|
59
59
|
({ path, max }) => (error_messages?.string?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()).split("{plural_suffix}").join(max > 1 ? "s" : "")
|
|
60
60
|
);
|
|
61
|
-
if (config.lowercase
|
|
62
|
-
if (config.uppercase
|
|
61
|
+
if (config.lowercase) schema2 = schema2.transform((property) => typeof property === "string" ? property.toLowerCase() : property);
|
|
62
|
+
if (config.uppercase) schema2 = schema2.transform((property) => typeof property === "string" ? property.toUpperCase() : property);
|
|
63
63
|
return schema2;
|
|
64
64
|
} else if (config.type === "number") {
|
|
65
65
|
schema2 = Yup.number().typeError(({ path }) => (error_messages?.number?.type ?? "").split("{path}").join(path));
|
|
66
66
|
schema2 = base(schema2, key, config);
|
|
67
|
-
if (config.enum
|
|
68
|
-
if (config.min
|
|
69
|
-
if (config.max
|
|
70
|
-
if (config.integer
|
|
71
|
-
if (config.positive
|
|
72
|
-
if (config.negative
|
|
67
|
+
if (config.enum) schema2 = schema2.oneOf(config.enum, ({ path }) => (error_messages?.number?.enum ?? "").split("{path}").join(path));
|
|
68
|
+
if (config.min) schema2 = schema2.min(config.min, ({ path, min }) => (error_messages?.number?.min ?? "").split("{path}").join(path).split("{min}").join(min.toString()));
|
|
69
|
+
if (config.max) schema2 = schema2.max(config.max, ({ path, max }) => (error_messages?.number?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()));
|
|
70
|
+
if (config.integer) schema2 = schema2.integer(({ path }) => (error_messages?.number?.integer ?? "").split("{path}").join(path));
|
|
71
|
+
if (config.positive) schema2 = schema2.positive(({ path }) => (error_messages?.number?.positive ?? "").split("{path}").join(path));
|
|
72
|
+
if (config.negative) schema2 = schema2.negative(({ path }) => (error_messages?.number?.negative ?? "").split("{path}").join(path));
|
|
73
73
|
return schema2;
|
|
74
74
|
} else if (config.type === "boolean") {
|
|
75
75
|
schema2 = Yup.boolean().typeError(({ path }) => (error_messages?.boolean?.type ?? "").split("{path}").join(path));
|
|
@@ -78,8 +78,8 @@ var convertToYup = (schema, error_messages) => {
|
|
|
78
78
|
} else if (config.type === "date") {
|
|
79
79
|
schema2 = Yup.date().typeError(({ path }) => (error_messages?.date?.type ?? "").split("{path}").join(path));
|
|
80
80
|
schema2 = base(schema2, key, config);
|
|
81
|
-
if (config.min
|
|
82
|
-
if (config.max
|
|
81
|
+
if (config.min) schema2 = schema2.min(config.min, ({ path, min }) => (error_messages?.date?.min ?? "").split("{path}").join(path).split("{min}").join(new Date(min).toISOString()));
|
|
82
|
+
if (config.max) schema2 = schema2.max(config.max, ({ path, max }) => (error_messages?.date?.max ?? "").split("{path}").join(path).split("{max}").join(new Date(max).toISOString()));
|
|
83
83
|
return schema2;
|
|
84
84
|
} else if (config.type === "object") {
|
|
85
85
|
schema2 = Yup.object().typeError(({ path }) => (error_messages?.object?.type ?? "").split("{path}").join(path));
|
|
@@ -91,12 +91,12 @@ var convertToYup = (schema, error_messages) => {
|
|
|
91
91
|
} else if (config.type === "array") {
|
|
92
92
|
schema2 = Yup.array().typeError(({ path }) => (error_messages?.array?.type ?? "").split("{path}").join(path));
|
|
93
93
|
schema2 = base(schema2, key, config);
|
|
94
|
-
if (config.min
|
|
94
|
+
if (config.min)
|
|
95
95
|
schema2 = schema2.min(
|
|
96
96
|
config.min,
|
|
97
97
|
({ path, min }) => (error_messages?.array?.min ?? "").split("{path}").join(path).split("{min}").join(min.toString()).split("{plural_suffix}").join(min > 1 ? "s" : "")
|
|
98
98
|
);
|
|
99
|
-
if (config.max
|
|
99
|
+
if (config.max)
|
|
100
100
|
schema2 = schema2.max(
|
|
101
101
|
config.max,
|
|
102
102
|
({ path, max }) => (error_messages?.array?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()).split("{plural_suffix}").join(max > 1 ? "s" : "")
|
|
@@ -113,33 +113,39 @@ var convertToYup = (schema, error_messages) => {
|
|
|
113
113
|
// src/utils/ConvertToJSONSchema.util.ts
|
|
114
114
|
import { Type } from "@sinclair/typebox";
|
|
115
115
|
var convertToJSONSchema = (schema) => {
|
|
116
|
+
const base = (schema2, key, config) => {
|
|
117
|
+
if (!config.required) schema2 = Type.Optional(schema2);
|
|
118
|
+
if (config.nullable || config.default === null) schema2 = Type.Union([schema2, Type.Null()]);
|
|
119
|
+
return schema2;
|
|
120
|
+
};
|
|
116
121
|
const build = (key, config) => {
|
|
122
|
+
let schema2;
|
|
117
123
|
if (config.type === "string") {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
return
|
|
124
|
+
schema2 = Type.String({ enum: config.enum, minLength: config.min, maxLength: config.max, pattern: new RegExp(config.pattern ?? Any).source, default: config.default });
|
|
125
|
+
schema2 = base(schema2, key, config);
|
|
126
|
+
return schema2;
|
|
121
127
|
} else if (config.type === "number") {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
return
|
|
128
|
+
schema2 = config.integer === true ? Type.Integer({ enum: config.enum, minimum: config.min, maximum: config.max, default: config.default }) : Type.Number({ enum: config.enum, minimum: config.min, maximum: config.max, default: config.default });
|
|
129
|
+
schema2 = base(schema2, key, config);
|
|
130
|
+
return schema2;
|
|
125
131
|
} else if (config.type === "boolean") {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
return
|
|
132
|
+
schema2 = Type.Boolean({ default: config.default });
|
|
133
|
+
schema2 = base(schema2, key, config);
|
|
134
|
+
return schema2;
|
|
129
135
|
} else if (config.type === "date") {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
return
|
|
136
|
+
schema2 = Type.String({ format: "date-time", minimum: config.min, maximum: config.max, default: config.default });
|
|
137
|
+
schema2 = base(schema2, key, config);
|
|
138
|
+
return schema2;
|
|
133
139
|
} else if (config.type === "object") {
|
|
134
140
|
const nested_properties = {};
|
|
135
141
|
for (const [nested_key, nested_config] of Object.entries(config.properties)) nested_properties[nested_key] = build(nested_key, nested_config);
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
return
|
|
142
|
+
schema2 = Type.Object(nested_properties);
|
|
143
|
+
schema2 = base(schema2, key, config);
|
|
144
|
+
return schema2;
|
|
139
145
|
} else if (config.type === "array") {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
return
|
|
146
|
+
schema2 = Type.Array(build(key, config.items), { minItems: config.min, maxItems: config.max, default: config.default });
|
|
147
|
+
schema2 = base(schema2, key, config);
|
|
148
|
+
return schema2;
|
|
143
149
|
} else throw new Error(`Unsupported schema type for ${key}`);
|
|
144
150
|
};
|
|
145
151
|
const properties = {};
|
|
@@ -151,13 +157,13 @@ var convertToJSONSchema = (schema) => {
|
|
|
151
157
|
var YuppiOptionsDefault = {
|
|
152
158
|
error_messages: {
|
|
153
159
|
base: {
|
|
154
|
-
pattern: "Field {path} must match the required pattern {pattern}",
|
|
155
160
|
nullable: "Field {path} cannot be null",
|
|
156
161
|
required: "Field {path} is required"
|
|
157
162
|
},
|
|
158
163
|
string: {
|
|
159
164
|
type: "Field {path} must be a string",
|
|
160
165
|
enum: "Field {path} must be one of the allowed values",
|
|
166
|
+
pattern: "Field {path} must match the required pattern {pattern}",
|
|
161
167
|
min: "Field {path} must be at least {min} character{plural_suffix}",
|
|
162
168
|
max: "Field {path} must be at most {max} character{plural_suffix}"
|
|
163
169
|
},
|
|
@@ -200,8 +206,7 @@ var Yuppi = class {
|
|
|
200
206
|
}
|
|
201
207
|
validate(schema, properties) {
|
|
202
208
|
const yup_schema = convertToYup(schema, this.options.error_messages);
|
|
203
|
-
|
|
204
|
-
return validation;
|
|
209
|
+
return yup_schema.validateSync(properties, this.options.validate_options);
|
|
205
210
|
}
|
|
206
211
|
convertToYup(schema) {
|
|
207
212
|
return convertToYup(schema, this.options.error_messages);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "yuppi",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.8",
|
|
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",
|
|
@@ -18,10 +18,10 @@
|
|
|
18
18
|
"@sinclair/typebox": "^0.34.41",
|
|
19
19
|
"@types/lodash": "^4.17.20",
|
|
20
20
|
"lodash": "^4.17.21",
|
|
21
|
-
"yup": "^1.7.
|
|
21
|
+
"yup": "^1.7.1"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"neatlint": "^1.1.
|
|
24
|
+
"neatlint": "^1.1.11",
|
|
25
25
|
"prettier": "^3.6.2",
|
|
26
26
|
"tsup": "^8.5.0"
|
|
27
27
|
},
|