yuppi 1.3.1 → 1.3.3
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/main.js +11 -11
- package/dist/main.mjs +11 -11
- package/package.json +2 -2
package/dist/main.js
CHANGED
|
@@ -45,8 +45,8 @@ var import_path = __toESM(require("path"));
|
|
|
45
45
|
var import_typebox = require("@sinclair/typebox");
|
|
46
46
|
var convertToJSONSchema = (schema) => {
|
|
47
47
|
const base = (schema2, key, config) => {
|
|
48
|
-
if (!config.required) schema2 = import_typebox.Type.Optional(schema2);
|
|
49
48
|
if (config.nullable || config.default === null) schema2 = import_typebox.Type.Union([schema2, import_typebox.Type.Null()]);
|
|
49
|
+
if (!config.required) schema2 = import_typebox.Type.Optional(schema2);
|
|
50
50
|
return schema2;
|
|
51
51
|
};
|
|
52
52
|
const build = (key, config) => {
|
|
@@ -56,7 +56,7 @@ var convertToJSONSchema = (schema) => {
|
|
|
56
56
|
schema2 = base(schema2, key, config);
|
|
57
57
|
return schema2;
|
|
58
58
|
} else if (config.type === "number") {
|
|
59
|
-
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 });
|
|
59
|
+
schema2 = config.integer === true ? import_typebox.Type.Integer({ enum: config.enum, minimum: config.min, maximum: config.max, positive: config.positive, negative: config.negative, default: config.default }) : import_typebox.Type.Number({ enum: config.enum, minimum: config.min, maximum: config.max, positive: config.positive, negative: config.negative, default: config.default });
|
|
60
60
|
schema2 = base(schema2, key, config);
|
|
61
61
|
return schema2;
|
|
62
62
|
} else if (config.type === "boolean") {
|
|
@@ -70,7 +70,7 @@ var convertToJSONSchema = (schema) => {
|
|
|
70
70
|
} else if (config.type === "object") {
|
|
71
71
|
const nested_properties = {};
|
|
72
72
|
for (const [nested_key, nested_config] of Object.entries(config.properties)) nested_properties[nested_key] = build(nested_key, nested_config);
|
|
73
|
-
schema2 = import_typebox.Type.Object(nested_properties, { additionalProperties: false });
|
|
73
|
+
schema2 = import_typebox.Type.Object(nested_properties, { default: config.default, additionalProperties: false });
|
|
74
74
|
schema2 = base(schema2, key, config);
|
|
75
75
|
return schema2;
|
|
76
76
|
} else if (config.type === "array") {
|
|
@@ -89,6 +89,9 @@ var Yup = __toESM(require("yup"));
|
|
|
89
89
|
var convertToYup = (schema, error_messages) => {
|
|
90
90
|
const base = (schema2, key, config) => {
|
|
91
91
|
schema2 = schema2.nullable();
|
|
92
|
+
schema2 = schema2.optional();
|
|
93
|
+
if (config.default) schema2 = schema2.default(config.default);
|
|
94
|
+
if (!config.nullable && config.default !== null) schema2 = schema2.nonNullable(({ path: path2 }) => (error_messages?.base?.nullable ?? "").replaceAll("{path}", path2));
|
|
92
95
|
if (config.required)
|
|
93
96
|
schema2 = schema2.test(
|
|
94
97
|
"required",
|
|
@@ -96,19 +99,15 @@ var convertToYup = (schema, error_messages) => {
|
|
|
96
99
|
(property) => {
|
|
97
100
|
if (property === void 0) return false;
|
|
98
101
|
if (typeof property === "string" && property.trim() === "") return false;
|
|
99
|
-
if (Array.isArray(property) && property.length === 0) return false;
|
|
100
102
|
return true;
|
|
101
103
|
}
|
|
102
104
|
);
|
|
103
|
-
if (!config.nullable && config.default !== null) schema2 = schema2.nonNullable(({ path: path2 }) => (error_messages?.base?.nullable ?? "").replaceAll("{path}", path2));
|
|
104
|
-
if (config.default) schema2 = schema2.default(config.default);
|
|
105
105
|
return schema2;
|
|
106
106
|
};
|
|
107
107
|
const build = (key, config) => {
|
|
108
108
|
let schema2;
|
|
109
109
|
if (config.type === "string") {
|
|
110
110
|
schema2 = Yup.string().typeError(({ path: path2 }) => (error_messages?.string?.type ?? "").replaceAll("{path}", path2));
|
|
111
|
-
schema2 = base(schema2, key, config);
|
|
112
111
|
schema2 = schema2.transform((property) => typeof property === "string" ? property.trim() : property);
|
|
113
112
|
if (config.enum)
|
|
114
113
|
schema2 = schema2.oneOf(
|
|
@@ -128,16 +127,17 @@ var convertToYup = (schema, error_messages) => {
|
|
|
128
127
|
);
|
|
129
128
|
if (config.lowercase) schema2 = schema2.transform((property) => typeof property === "string" ? property.toLowerCase() : property);
|
|
130
129
|
if (config.uppercase) schema2 = schema2.transform((property) => typeof property === "string" ? property.toUpperCase() : property);
|
|
130
|
+
schema2 = base(schema2, key, config);
|
|
131
131
|
return schema2;
|
|
132
132
|
} else if (config.type === "number") {
|
|
133
133
|
schema2 = Yup.number().typeError(({ path: path2 }) => (error_messages?.number?.type ?? "").replaceAll("{path}", path2));
|
|
134
|
-
schema2 = base(schema2, key, config);
|
|
135
134
|
if (config.enum) schema2 = schema2.oneOf(config.enum, ({ path: path2 }) => (error_messages?.number?.enum ?? "").replaceAll("{path}", path2));
|
|
136
135
|
if (config.min) schema2 = schema2.min(config.min, ({ path: path2, min }) => (error_messages?.number?.min ?? "").replaceAll("{path}", path2).replaceAll("{min}", min.toString()));
|
|
137
136
|
if (config.max) schema2 = schema2.max(config.max, ({ path: path2, max }) => (error_messages?.number?.max ?? "").replaceAll("{path}", path2).replaceAll("{max}", max.toString()));
|
|
138
137
|
if (config.integer) schema2 = schema2.integer(({ path: path2 }) => (error_messages?.number?.integer ?? "").replaceAll("{path}", path2));
|
|
139
138
|
if (config.positive) schema2 = schema2.positive(({ path: path2 }) => (error_messages?.number?.positive ?? "").replaceAll("{path}", path2));
|
|
140
139
|
if (config.negative) schema2 = schema2.negative(({ path: path2 }) => (error_messages?.number?.negative ?? "").replaceAll("{path}", path2));
|
|
140
|
+
schema2 = base(schema2, key, config);
|
|
141
141
|
return schema2;
|
|
142
142
|
} else if (config.type === "boolean") {
|
|
143
143
|
schema2 = Yup.boolean().typeError(({ path: path2 }) => (error_messages?.boolean?.type ?? "").replaceAll("{path}", path2));
|
|
@@ -145,20 +145,19 @@ var convertToYup = (schema, error_messages) => {
|
|
|
145
145
|
return schema2;
|
|
146
146
|
} else if (config.type === "date") {
|
|
147
147
|
schema2 = Yup.date().typeError(({ path: path2 }) => (error_messages?.date?.type ?? "").replaceAll("{path}", path2));
|
|
148
|
-
schema2 = base(schema2, key, config);
|
|
149
148
|
if (config.min) schema2 = schema2.min(config.min, ({ path: path2, min }) => (error_messages?.date?.min ?? "").replaceAll("{path}", path2).replaceAll("{min}", new Date(min).toISOString()));
|
|
150
149
|
if (config.max) schema2 = schema2.max(config.max, ({ path: path2, max }) => (error_messages?.date?.max ?? "").replaceAll("{path}", path2).replaceAll("{max}", new Date(max).toISOString()));
|
|
150
|
+
schema2 = base(schema2, key, config);
|
|
151
151
|
return schema2;
|
|
152
152
|
} else if (config.type === "object") {
|
|
153
153
|
schema2 = Yup.object().typeError(({ path: path2 }) => (error_messages?.object?.type ?? "").replaceAll("{path}", path2));
|
|
154
|
-
schema2 = base(schema2, key, config);
|
|
155
154
|
const nested_properties = {};
|
|
156
155
|
for (const [nested_key, nested_config] of Object.entries(config.properties)) nested_properties[nested_key] = build(nested_key, nested_config);
|
|
157
156
|
schema2 = schema2.shape(nested_properties);
|
|
157
|
+
schema2 = base(schema2, key, config);
|
|
158
158
|
return schema2;
|
|
159
159
|
} else if (config.type === "array") {
|
|
160
160
|
schema2 = Yup.array().typeError(({ path: path2 }) => (error_messages?.array?.type ?? "").replaceAll("{path}", path2));
|
|
161
|
-
schema2 = base(schema2, key, config);
|
|
162
161
|
if (config.min)
|
|
163
162
|
schema2 = schema2.min(
|
|
164
163
|
config.min,
|
|
@@ -170,6 +169,7 @@ var convertToYup = (schema, error_messages) => {
|
|
|
170
169
|
({ path: path2, max }) => (error_messages?.array?.max ?? "").replaceAll("{path}", path2).replaceAll("{max}", max.toString()).replaceAll("{plural_suffix}", max > 1 ? "s" : "")
|
|
171
170
|
);
|
|
172
171
|
schema2 = schema2.of(build(key, config.items));
|
|
172
|
+
schema2 = base(schema2, key, config);
|
|
173
173
|
return schema2;
|
|
174
174
|
} else throw new Error(`Unsupported schema type for ${key}`);
|
|
175
175
|
};
|
package/dist/main.mjs
CHANGED
|
@@ -14,8 +14,8 @@ import path from "path";
|
|
|
14
14
|
import { Type } from "@sinclair/typebox";
|
|
15
15
|
var convertToJSONSchema = (schema) => {
|
|
16
16
|
const base = (schema2, key, config) => {
|
|
17
|
-
if (!config.required) schema2 = Type.Optional(schema2);
|
|
18
17
|
if (config.nullable || config.default === null) schema2 = Type.Union([schema2, Type.Null()]);
|
|
18
|
+
if (!config.required) schema2 = Type.Optional(schema2);
|
|
19
19
|
return schema2;
|
|
20
20
|
};
|
|
21
21
|
const build = (key, config) => {
|
|
@@ -25,7 +25,7 @@ var convertToJSONSchema = (schema) => {
|
|
|
25
25
|
schema2 = base(schema2, key, config);
|
|
26
26
|
return schema2;
|
|
27
27
|
} else if (config.type === "number") {
|
|
28
|
-
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 });
|
|
28
|
+
schema2 = config.integer === true ? Type.Integer({ enum: config.enum, minimum: config.min, maximum: config.max, positive: config.positive, negative: config.negative, default: config.default }) : Type.Number({ enum: config.enum, minimum: config.min, maximum: config.max, positive: config.positive, negative: config.negative, default: config.default });
|
|
29
29
|
schema2 = base(schema2, key, config);
|
|
30
30
|
return schema2;
|
|
31
31
|
} else if (config.type === "boolean") {
|
|
@@ -39,7 +39,7 @@ var convertToJSONSchema = (schema) => {
|
|
|
39
39
|
} else if (config.type === "object") {
|
|
40
40
|
const nested_properties = {};
|
|
41
41
|
for (const [nested_key, nested_config] of Object.entries(config.properties)) nested_properties[nested_key] = build(nested_key, nested_config);
|
|
42
|
-
schema2 = Type.Object(nested_properties, { additionalProperties: false });
|
|
42
|
+
schema2 = Type.Object(nested_properties, { default: config.default, additionalProperties: false });
|
|
43
43
|
schema2 = base(schema2, key, config);
|
|
44
44
|
return schema2;
|
|
45
45
|
} else if (config.type === "array") {
|
|
@@ -58,6 +58,9 @@ import * as Yup from "yup";
|
|
|
58
58
|
var convertToYup = (schema, error_messages) => {
|
|
59
59
|
const base = (schema2, key, config) => {
|
|
60
60
|
schema2 = schema2.nullable();
|
|
61
|
+
schema2 = schema2.optional();
|
|
62
|
+
if (config.default) schema2 = schema2.default(config.default);
|
|
63
|
+
if (!config.nullable && config.default !== null) schema2 = schema2.nonNullable(({ path: path2 }) => (error_messages?.base?.nullable ?? "").replaceAll("{path}", path2));
|
|
61
64
|
if (config.required)
|
|
62
65
|
schema2 = schema2.test(
|
|
63
66
|
"required",
|
|
@@ -65,19 +68,15 @@ var convertToYup = (schema, error_messages) => {
|
|
|
65
68
|
(property) => {
|
|
66
69
|
if (property === void 0) return false;
|
|
67
70
|
if (typeof property === "string" && property.trim() === "") return false;
|
|
68
|
-
if (Array.isArray(property) && property.length === 0) return false;
|
|
69
71
|
return true;
|
|
70
72
|
}
|
|
71
73
|
);
|
|
72
|
-
if (!config.nullable && config.default !== null) schema2 = schema2.nonNullable(({ path: path2 }) => (error_messages?.base?.nullable ?? "").replaceAll("{path}", path2));
|
|
73
|
-
if (config.default) schema2 = schema2.default(config.default);
|
|
74
74
|
return schema2;
|
|
75
75
|
};
|
|
76
76
|
const build = (key, config) => {
|
|
77
77
|
let schema2;
|
|
78
78
|
if (config.type === "string") {
|
|
79
79
|
schema2 = Yup.string().typeError(({ path: path2 }) => (error_messages?.string?.type ?? "").replaceAll("{path}", path2));
|
|
80
|
-
schema2 = base(schema2, key, config);
|
|
81
80
|
schema2 = schema2.transform((property) => typeof property === "string" ? property.trim() : property);
|
|
82
81
|
if (config.enum)
|
|
83
82
|
schema2 = schema2.oneOf(
|
|
@@ -97,16 +96,17 @@ var convertToYup = (schema, error_messages) => {
|
|
|
97
96
|
);
|
|
98
97
|
if (config.lowercase) schema2 = schema2.transform((property) => typeof property === "string" ? property.toLowerCase() : property);
|
|
99
98
|
if (config.uppercase) schema2 = schema2.transform((property) => typeof property === "string" ? property.toUpperCase() : property);
|
|
99
|
+
schema2 = base(schema2, key, config);
|
|
100
100
|
return schema2;
|
|
101
101
|
} else if (config.type === "number") {
|
|
102
102
|
schema2 = Yup.number().typeError(({ path: path2 }) => (error_messages?.number?.type ?? "").replaceAll("{path}", path2));
|
|
103
|
-
schema2 = base(schema2, key, config);
|
|
104
103
|
if (config.enum) schema2 = schema2.oneOf(config.enum, ({ path: path2 }) => (error_messages?.number?.enum ?? "").replaceAll("{path}", path2));
|
|
105
104
|
if (config.min) schema2 = schema2.min(config.min, ({ path: path2, min }) => (error_messages?.number?.min ?? "").replaceAll("{path}", path2).replaceAll("{min}", min.toString()));
|
|
106
105
|
if (config.max) schema2 = schema2.max(config.max, ({ path: path2, max }) => (error_messages?.number?.max ?? "").replaceAll("{path}", path2).replaceAll("{max}", max.toString()));
|
|
107
106
|
if (config.integer) schema2 = schema2.integer(({ path: path2 }) => (error_messages?.number?.integer ?? "").replaceAll("{path}", path2));
|
|
108
107
|
if (config.positive) schema2 = schema2.positive(({ path: path2 }) => (error_messages?.number?.positive ?? "").replaceAll("{path}", path2));
|
|
109
108
|
if (config.negative) schema2 = schema2.negative(({ path: path2 }) => (error_messages?.number?.negative ?? "").replaceAll("{path}", path2));
|
|
109
|
+
schema2 = base(schema2, key, config);
|
|
110
110
|
return schema2;
|
|
111
111
|
} else if (config.type === "boolean") {
|
|
112
112
|
schema2 = Yup.boolean().typeError(({ path: path2 }) => (error_messages?.boolean?.type ?? "").replaceAll("{path}", path2));
|
|
@@ -114,20 +114,19 @@ var convertToYup = (schema, error_messages) => {
|
|
|
114
114
|
return schema2;
|
|
115
115
|
} else if (config.type === "date") {
|
|
116
116
|
schema2 = Yup.date().typeError(({ path: path2 }) => (error_messages?.date?.type ?? "").replaceAll("{path}", path2));
|
|
117
|
-
schema2 = base(schema2, key, config);
|
|
118
117
|
if (config.min) schema2 = schema2.min(config.min, ({ path: path2, min }) => (error_messages?.date?.min ?? "").replaceAll("{path}", path2).replaceAll("{min}", new Date(min).toISOString()));
|
|
119
118
|
if (config.max) schema2 = schema2.max(config.max, ({ path: path2, max }) => (error_messages?.date?.max ?? "").replaceAll("{path}", path2).replaceAll("{max}", new Date(max).toISOString()));
|
|
119
|
+
schema2 = base(schema2, key, config);
|
|
120
120
|
return schema2;
|
|
121
121
|
} else if (config.type === "object") {
|
|
122
122
|
schema2 = Yup.object().typeError(({ path: path2 }) => (error_messages?.object?.type ?? "").replaceAll("{path}", path2));
|
|
123
|
-
schema2 = base(schema2, key, config);
|
|
124
123
|
const nested_properties = {};
|
|
125
124
|
for (const [nested_key, nested_config] of Object.entries(config.properties)) nested_properties[nested_key] = build(nested_key, nested_config);
|
|
126
125
|
schema2 = schema2.shape(nested_properties);
|
|
126
|
+
schema2 = base(schema2, key, config);
|
|
127
127
|
return schema2;
|
|
128
128
|
} else if (config.type === "array") {
|
|
129
129
|
schema2 = Yup.array().typeError(({ path: path2 }) => (error_messages?.array?.type ?? "").replaceAll("{path}", path2));
|
|
130
|
-
schema2 = base(schema2, key, config);
|
|
131
130
|
if (config.min)
|
|
132
131
|
schema2 = schema2.min(
|
|
133
132
|
config.min,
|
|
@@ -139,6 +138,7 @@ var convertToYup = (schema, error_messages) => {
|
|
|
139
138
|
({ path: path2, max }) => (error_messages?.array?.max ?? "").replaceAll("{path}", path2).replaceAll("{max}", max.toString()).replaceAll("{plural_suffix}", max > 1 ? "s" : "")
|
|
140
139
|
);
|
|
141
140
|
schema2 = schema2.of(build(key, config.items));
|
|
141
|
+
schema2 = base(schema2, key, config);
|
|
142
142
|
return schema2;
|
|
143
143
|
} else throw new Error(`Unsupported schema type for ${key}`);
|
|
144
144
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "yuppi",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.3",
|
|
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",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"@sinclair/typebox": "^0.34.41",
|
|
19
19
|
"@types/lodash": "^4.17.20",
|
|
20
|
-
"@types/node": "^24.
|
|
20
|
+
"@types/node": "^24.6.0",
|
|
21
21
|
"json-schema-to-typescript": "^15.0.4",
|
|
22
22
|
"lodash": "^4.17.21",
|
|
23
23
|
"yup": "^1.7.1"
|