yuppi 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +9 -0
- package/README.md +368 -0
- package/dist/Patterns.barrel-KU6hZBXs.d.mts +14 -0
- package/dist/Patterns.barrel-UKU2yPZ6.d.ts +14 -0
- package/dist/Types.barrel--XGUUjIp.d.mts +18 -0
- package/dist/Types.barrel-wzQq8e9v.d.ts +18 -0
- package/dist/Yuppi.class.d.mts +17 -0
- package/dist/Yuppi.class.d.ts +17 -0
- package/dist/Yuppi.class.js +212 -0
- package/dist/Yuppi.class.mjs +177 -0
- package/dist/barrels/Patterns.barrel.d.mts +5 -0
- package/dist/barrels/Patterns.barrel.d.ts +5 -0
- package/dist/barrels/Patterns.barrel.js +47 -0
- package/dist/barrels/Patterns.barrel.mjs +17 -0
- package/dist/barrels/Types.barrel.d.mts +8 -0
- package/dist/barrels/Types.barrel.d.ts +8 -0
- package/dist/barrels/Types.barrel.js +18 -0
- package/dist/barrels/Types.barrel.mjs +0 -0
- package/dist/defaults/YuppiOptions.default.d.mts +7 -0
- package/dist/defaults/YuppiOptions.default.d.ts +7 -0
- package/dist/defaults/YuppiOptions.default.js +71 -0
- package/dist/defaults/YuppiOptions.default.mjs +46 -0
- package/dist/main.d.mts +15 -0
- package/dist/main.d.ts +15 -0
- package/dist/main.js +237 -0
- package/dist/main.mjs +205 -0
- package/dist/patterns/Email.pattern.d.mts +5 -0
- package/dist/patterns/Email.pattern.d.ts +5 -0
- package/dist/patterns/Email.pattern.js +30 -0
- package/dist/patterns/Email.pattern.mjs +5 -0
- package/dist/patterns/PhoneNumber.pattern.d.mts +5 -0
- package/dist/patterns/PhoneNumber.pattern.d.ts +5 -0
- package/dist/patterns/PhoneNumber.pattern.js +30 -0
- package/dist/patterns/PhoneNumber.pattern.mjs +5 -0
- package/dist/patterns/URL.pattern.d.mts +5 -0
- package/dist/patterns/URL.pattern.d.ts +5 -0
- package/dist/patterns/URL.pattern.js +30 -0
- package/dist/patterns/URL.pattern.mjs +5 -0
- package/dist/patterns/Username.pattern.d.mts +5 -0
- package/dist/patterns/Username.pattern.d.ts +5 -0
- package/dist/patterns/Username.pattern.js +30 -0
- package/dist/patterns/Username.pattern.mjs +5 -0
- package/dist/types/AnyObject.type.d.mts +5 -0
- package/dist/types/AnyObject.type.d.ts +5 -0
- package/dist/types/AnyObject.type.js +18 -0
- package/dist/types/AnyObject.type.mjs +0 -0
- package/dist/types/JSONSchema.type.d.mts +5 -0
- package/dist/types/JSONSchema.type.d.ts +5 -0
- package/dist/types/JSONSchema.type.js +18 -0
- package/dist/types/JSONSchema.type.mjs +0 -0
- package/dist/types/Schema.type.d.mts +45 -0
- package/dist/types/Schema.type.d.ts +45 -0
- package/dist/types/Schema.type.js +18 -0
- package/dist/types/Schema.type.mjs +0 -0
- package/dist/types/ValidateOptions.type.d.mts +5 -0
- package/dist/types/ValidateOptions.type.d.ts +5 -0
- package/dist/types/ValidateOptions.type.js +18 -0
- package/dist/types/ValidateOptions.type.mjs +0 -0
- package/dist/types/ValidationError.type.d.mts +5 -0
- package/dist/types/ValidationError.type.d.ts +5 -0
- package/dist/types/ValidationError.type.js +18 -0
- package/dist/types/ValidationError.type.mjs +0 -0
- package/dist/types/YuppiOptions.type.d.mts +44 -0
- package/dist/types/YuppiOptions.type.d.ts +44 -0
- package/dist/types/YuppiOptions.type.js +18 -0
- package/dist/types/YuppiOptions.type.mjs +0 -0
- package/dist/utils/ConvertToJSONSchema.util.d.mts +6 -0
- package/dist/utils/ConvertToJSONSchema.util.d.ts +6 -0
- package/dist/utils/ConvertToJSONSchema.util.js +64 -0
- package/dist/utils/ConvertToJSONSchema.util.mjs +39 -0
- package/dist/utils/ConvertToYup.util.d.mts +9 -0
- package/dist/utils/ConvertToYup.util.d.ts +9 -0
- package/dist/utils/ConvertToYup.util.js +110 -0
- package/dist/utils/ConvertToYup.util.mjs +75 -0
- package/package.json +42 -0
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export { Yuppi } from './Yuppi.class.js';
|
|
2
|
+
export { P as Patterns } from './Patterns.barrel-UKU2yPZ6.js';
|
|
3
|
+
export { T as Types } from './Types.barrel-wzQq8e9v.js';
|
|
4
|
+
import './types/AnyObject.type.js';
|
|
5
|
+
import 'yup';
|
|
6
|
+
import './types/JSONSchema.type.js';
|
|
7
|
+
import '@sinclair/typebox';
|
|
8
|
+
import './types/Schema.type.js';
|
|
9
|
+
import './types/YuppiOptions.type.js';
|
|
10
|
+
import './types/ValidateOptions.type.js';
|
|
11
|
+
import './patterns/Email.pattern.js';
|
|
12
|
+
import './patterns/PhoneNumber.pattern.js';
|
|
13
|
+
import './patterns/URL.pattern.js';
|
|
14
|
+
import './patterns/Username.pattern.js';
|
|
15
|
+
import './types/ValidationError.type.js';
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/main.ts
|
|
31
|
+
var main_exports = {};
|
|
32
|
+
__export(main_exports, {
|
|
33
|
+
Patterns: () => Patterns_barrel_exports,
|
|
34
|
+
Yuppi: () => Yuppi
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(main_exports);
|
|
37
|
+
|
|
38
|
+
// src/Yuppi.class.ts
|
|
39
|
+
var import_lodash = __toESM(require("lodash"));
|
|
40
|
+
|
|
41
|
+
// src/utils/ConvertToYup.util.ts
|
|
42
|
+
var Yup = __toESM(require("yup"));
|
|
43
|
+
var convertToYup = (schema, error_messages) => {
|
|
44
|
+
const build = (key, config) => {
|
|
45
|
+
let schema2;
|
|
46
|
+
if (config.type === "string") {
|
|
47
|
+
schema2 = Yup.string().typeError(({ path }) => (error_messages?.string?.type ?? "").split("{path}").join(path));
|
|
48
|
+
schema2 = schema2.trim();
|
|
49
|
+
if (config.min !== void 0)
|
|
50
|
+
schema2 = schema2.min(
|
|
51
|
+
config.min,
|
|
52
|
+
({ path, min }) => (error_messages?.string?.min ?? "").split("{path}").join(path).split("{min}").join(min.toString()).split("{plural_suffix}").join(min > 1 ? "s" : "")
|
|
53
|
+
);
|
|
54
|
+
if (config.max !== void 0)
|
|
55
|
+
schema2 = schema2.max(
|
|
56
|
+
config.max,
|
|
57
|
+
({ path, max }) => (error_messages?.string?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()).split("{plural_suffix}").join(max > 1 ? "s" : "")
|
|
58
|
+
);
|
|
59
|
+
if (config.lowercase === true) schema2 = schema2.lowercase();
|
|
60
|
+
if (config.uppercase === true) schema2 = schema2.uppercase();
|
|
61
|
+
} else if (config.type === "number") {
|
|
62
|
+
schema2 = Yup.number().typeError(({ path }) => (error_messages?.number?.type ?? "").split("{path}").join(path));
|
|
63
|
+
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
|
+
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
|
+
if (config.integer === true) schema2 = schema2.integer(({ path }) => (error_messages?.number?.integer ?? "").split("{path}").join(path));
|
|
66
|
+
if (config.positive === true) schema2 = schema2.positive(({ path }) => (error_messages?.number?.positive ?? "").split("{path}").join(path));
|
|
67
|
+
if (config.negative === true) schema2 = schema2.negative(({ path }) => (error_messages?.number?.negative ?? "").split("{path}").join(path));
|
|
68
|
+
} else if (config.type === "boolean") {
|
|
69
|
+
schema2 = Yup.boolean().typeError(({ path }) => (error_messages?.boolean?.type ?? "").split("{path}").join(path));
|
|
70
|
+
} else if (config.type === "date") {
|
|
71
|
+
schema2 = Yup.date().typeError(({ path }) => (error_messages?.date?.type ?? "").split("{path}").join(path));
|
|
72
|
+
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
|
+
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()));
|
|
74
|
+
} else if (config.type === "object") {
|
|
75
|
+
schema2 = Yup.object().typeError(({ path }) => (error_messages?.object?.type ?? "").split("{path}").join(path));
|
|
76
|
+
const nested_properties = {};
|
|
77
|
+
for (const [nested_key, nested_config] of Object.entries(config.properties)) nested_properties[nested_key] = build(nested_key, nested_config);
|
|
78
|
+
schema2 = schema2.shape(nested_properties);
|
|
79
|
+
} else if (config.type === "array") {
|
|
80
|
+
schema2 = Yup.array().typeError(({ path }) => (error_messages?.array?.type ?? "").split("{path}").join(path));
|
|
81
|
+
if (config.min !== void 0)
|
|
82
|
+
schema2 = schema2.min(
|
|
83
|
+
config.min,
|
|
84
|
+
({ path, min }) => (error_messages?.array?.min ?? "").split("{path}").join(path).split("{min}").join(min.toString()).split("{plural_suffix}").join(min > 1 ? "s" : "")
|
|
85
|
+
);
|
|
86
|
+
if (config.max !== void 0)
|
|
87
|
+
schema2 = schema2.max(
|
|
88
|
+
config.max,
|
|
89
|
+
({ path, max }) => (error_messages?.array?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()).split("{plural_suffix}").join(max > 1 ? "s" : "")
|
|
90
|
+
);
|
|
91
|
+
schema2 = schema2.of(build(key, config.items));
|
|
92
|
+
} 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
|
+
};
|
|
109
|
+
const shape = {};
|
|
110
|
+
for (const [key, config] of Object.entries(schema)) shape[key] = build(key, config);
|
|
111
|
+
return Yup.object().shape(shape);
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
// src/utils/ConvertToJSONSchema.util.ts
|
|
115
|
+
var import_typebox = require("@sinclair/typebox");
|
|
116
|
+
var convertToJSONSchema = (schema) => {
|
|
117
|
+
const build = (key, config) => {
|
|
118
|
+
if (config.type === "string") {
|
|
119
|
+
let schema2 = import_typebox.Type.String({ minLength: config.min, maxLength: config.max, pattern: new RegExp(config.pattern ?? "[\\s\\S]*").source, default: config.default });
|
|
120
|
+
if (config.nullable) schema2 = import_typebox.Type.Union([schema2, import_typebox.Type.Null()]);
|
|
121
|
+
return config.required ? schema2 : import_typebox.Type.Optional(schema2);
|
|
122
|
+
} else if (config.type === "number") {
|
|
123
|
+
let schema2 = config.integer === true ? import_typebox.Type.Integer({ minimum: config.min, maximum: config.max, default: config.default }) : import_typebox.Type.Number({ minimum: config.min, maximum: config.max, default: config.default });
|
|
124
|
+
if (config.nullable) schema2 = import_typebox.Type.Union([schema2, import_typebox.Type.Null()]);
|
|
125
|
+
return config.required ? schema2 : import_typebox.Type.Optional(schema2);
|
|
126
|
+
} else if (config.type === "boolean") {
|
|
127
|
+
let schema2 = import_typebox.Type.Boolean({ default: config.default });
|
|
128
|
+
if (config.nullable) schema2 = import_typebox.Type.Union([schema2, import_typebox.Type.Null()]);
|
|
129
|
+
return config.required ? schema2 : import_typebox.Type.Optional(schema2);
|
|
130
|
+
} else if (config.type === "date") {
|
|
131
|
+
let schema2 = import_typebox.Type.String({ format: "date-time", minimum: config.min, maximum: config.max, pattern: new RegExp(config.pattern ?? "[\\s\\S]*").source, default: config.default });
|
|
132
|
+
if (config.nullable) schema2 = import_typebox.Type.Union([schema2, import_typebox.Type.Null()]);
|
|
133
|
+
return config.required ? schema2 : import_typebox.Type.Optional(schema2);
|
|
134
|
+
} else if (config.type === "object") {
|
|
135
|
+
const nested_properties = {};
|
|
136
|
+
for (const [nested_key, nested_config] of Object.entries(config.properties)) nested_properties[nested_key] = build(nested_key, nested_config);
|
|
137
|
+
let schema2 = import_typebox.Type.Object(nested_properties);
|
|
138
|
+
if (config.nullable) schema2 = import_typebox.Type.Union([schema2, import_typebox.Type.Null()]);
|
|
139
|
+
return config.required ? schema2 : import_typebox.Type.Optional(schema2);
|
|
140
|
+
} else if (config.type === "array") {
|
|
141
|
+
let schema2 = import_typebox.Type.Array(build(key, config.items), { minItems: config.min, maxItems: config.max, default: config.default });
|
|
142
|
+
if (config.nullable) schema2 = import_typebox.Type.Union([schema2, import_typebox.Type.Null()]);
|
|
143
|
+
return config.required ? schema2 : import_typebox.Type.Optional(schema2);
|
|
144
|
+
} else throw new Error(`Unsupported schema type for ${key}`);
|
|
145
|
+
};
|
|
146
|
+
const properties = {};
|
|
147
|
+
for (const [key, config] of Object.entries(schema)) properties[key] = build(key, config);
|
|
148
|
+
return import_typebox.Type.Object(properties);
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
// src/defaults/YuppiOptions.default.ts
|
|
152
|
+
var YuppiOptionsDefault = {
|
|
153
|
+
error_messages: {
|
|
154
|
+
base: {
|
|
155
|
+
pattern: "Field {path} must match the required pattern {pattern}",
|
|
156
|
+
nullable: "Field {path} cannot be null",
|
|
157
|
+
required: "Field {path} is required"
|
|
158
|
+
},
|
|
159
|
+
string: {
|
|
160
|
+
type: "Field {path} must be a string",
|
|
161
|
+
min: "Field {path} must be at least {min} character{plural_suffix}",
|
|
162
|
+
max: "Field {path} must be at most {max} character{plural_suffix}"
|
|
163
|
+
},
|
|
164
|
+
number: {
|
|
165
|
+
type: "Field {path} must be a number",
|
|
166
|
+
min: "Field {path} must be greater than or equal to {min}",
|
|
167
|
+
max: "Field {path} must be less than or equal to {max}",
|
|
168
|
+
integer: "Field {path} must be an integer",
|
|
169
|
+
positive: "Field {path} must be a positive number",
|
|
170
|
+
negative: "Field {path} must be a negative number"
|
|
171
|
+
},
|
|
172
|
+
boolean: {
|
|
173
|
+
type: "Field {path} must be a boolean"
|
|
174
|
+
},
|
|
175
|
+
date: {
|
|
176
|
+
type: "Field {path} must be a date",
|
|
177
|
+
min: "Field {path} must be after {min}",
|
|
178
|
+
max: "Field {path} must be before {max}"
|
|
179
|
+
},
|
|
180
|
+
object: {
|
|
181
|
+
type: "Field {path} must be an object"
|
|
182
|
+
},
|
|
183
|
+
array: {
|
|
184
|
+
type: "Field {path} must be an array",
|
|
185
|
+
min: "Field {path} must be at least {min} item${plural_suffix}",
|
|
186
|
+
max: "Field {path} must be at most {max} item${plural_suffix}"
|
|
187
|
+
}
|
|
188
|
+
},
|
|
189
|
+
validate_options: {
|
|
190
|
+
abortEarly: false,
|
|
191
|
+
stripUnknown: true
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
// src/Yuppi.class.ts
|
|
196
|
+
var Yuppi = class {
|
|
197
|
+
constructor(options = YuppiOptionsDefault) {
|
|
198
|
+
this.options = import_lodash.default.merge({}, YuppiOptionsDefault, options);
|
|
199
|
+
}
|
|
200
|
+
async validate(schema, properties) {
|
|
201
|
+
const yup_schema = convertToYup(schema, this.options.error_messages);
|
|
202
|
+
const validation = await yup_schema.validate(properties, this.options.validate_options);
|
|
203
|
+
return validation;
|
|
204
|
+
}
|
|
205
|
+
convertToYup(schema) {
|
|
206
|
+
return convertToYup(schema, this.options.error_messages);
|
|
207
|
+
}
|
|
208
|
+
convertToJSONSchema(schema) {
|
|
209
|
+
return convertToJSONSchema(schema);
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
// src/barrels/Patterns.barrel.ts
|
|
214
|
+
var Patterns_barrel_exports = {};
|
|
215
|
+
__export(Patterns_barrel_exports, {
|
|
216
|
+
Email: () => Email,
|
|
217
|
+
PhoneNumber: () => PhoneNumber,
|
|
218
|
+
URL: () => URL,
|
|
219
|
+
Username: () => Username
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
// src/patterns/Email.pattern.ts
|
|
223
|
+
var Email = "^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$";
|
|
224
|
+
|
|
225
|
+
// src/patterns/PhoneNumber.pattern.ts
|
|
226
|
+
var PhoneNumber = "^\\d{4}\\d{7,12}$";
|
|
227
|
+
|
|
228
|
+
// src/patterns/URL.pattern.ts
|
|
229
|
+
var URL = "^(https?:\\/\\/)?([\\w-]+\\.)+[\\w-]{2,}(\\/[\\w-./?%&=]*)?$";
|
|
230
|
+
|
|
231
|
+
// src/patterns/Username.pattern.ts
|
|
232
|
+
var Username = "^(?=.*[a-zA-Z])[a-zA-Z0-9][a-zA-Z0-9_]*$";
|
|
233
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
234
|
+
0 && (module.exports = {
|
|
235
|
+
Patterns,
|
|
236
|
+
Yuppi
|
|
237
|
+
});
|
package/dist/main.mjs
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __export = (target, all) => {
|
|
3
|
+
for (var name in all)
|
|
4
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
// src/Yuppi.class.ts
|
|
8
|
+
import _ from "lodash";
|
|
9
|
+
|
|
10
|
+
// src/utils/ConvertToYup.util.ts
|
|
11
|
+
import * as Yup from "yup";
|
|
12
|
+
var convertToYup = (schema, error_messages) => {
|
|
13
|
+
const build = (key, config) => {
|
|
14
|
+
let schema2;
|
|
15
|
+
if (config.type === "string") {
|
|
16
|
+
schema2 = Yup.string().typeError(({ path }) => (error_messages?.string?.type ?? "").split("{path}").join(path));
|
|
17
|
+
schema2 = schema2.trim();
|
|
18
|
+
if (config.min !== void 0)
|
|
19
|
+
schema2 = schema2.min(
|
|
20
|
+
config.min,
|
|
21
|
+
({ path, min }) => (error_messages?.string?.min ?? "").split("{path}").join(path).split("{min}").join(min.toString()).split("{plural_suffix}").join(min > 1 ? "s" : "")
|
|
22
|
+
);
|
|
23
|
+
if (config.max !== void 0)
|
|
24
|
+
schema2 = schema2.max(
|
|
25
|
+
config.max,
|
|
26
|
+
({ path, max }) => (error_messages?.string?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()).split("{plural_suffix}").join(max > 1 ? "s" : "")
|
|
27
|
+
);
|
|
28
|
+
if (config.lowercase === true) schema2 = schema2.lowercase();
|
|
29
|
+
if (config.uppercase === true) schema2 = schema2.uppercase();
|
|
30
|
+
} else if (config.type === "number") {
|
|
31
|
+
schema2 = Yup.number().typeError(({ path }) => (error_messages?.number?.type ?? "").split("{path}").join(path));
|
|
32
|
+
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
|
+
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
|
+
if (config.integer === true) schema2 = schema2.integer(({ path }) => (error_messages?.number?.integer ?? "").split("{path}").join(path));
|
|
35
|
+
if (config.positive === true) schema2 = schema2.positive(({ path }) => (error_messages?.number?.positive ?? "").split("{path}").join(path));
|
|
36
|
+
if (config.negative === true) schema2 = schema2.negative(({ path }) => (error_messages?.number?.negative ?? "").split("{path}").join(path));
|
|
37
|
+
} else if (config.type === "boolean") {
|
|
38
|
+
schema2 = Yup.boolean().typeError(({ path }) => (error_messages?.boolean?.type ?? "").split("{path}").join(path));
|
|
39
|
+
} else if (config.type === "date") {
|
|
40
|
+
schema2 = Yup.date().typeError(({ path }) => (error_messages?.date?.type ?? "").split("{path}").join(path));
|
|
41
|
+
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
|
+
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()));
|
|
43
|
+
} else if (config.type === "object") {
|
|
44
|
+
schema2 = Yup.object().typeError(({ path }) => (error_messages?.object?.type ?? "").split("{path}").join(path));
|
|
45
|
+
const nested_properties = {};
|
|
46
|
+
for (const [nested_key, nested_config] of Object.entries(config.properties)) nested_properties[nested_key] = build(nested_key, nested_config);
|
|
47
|
+
schema2 = schema2.shape(nested_properties);
|
|
48
|
+
} else if (config.type === "array") {
|
|
49
|
+
schema2 = Yup.array().typeError(({ path }) => (error_messages?.array?.type ?? "").split("{path}").join(path));
|
|
50
|
+
if (config.min !== void 0)
|
|
51
|
+
schema2 = schema2.min(
|
|
52
|
+
config.min,
|
|
53
|
+
({ path, min }) => (error_messages?.array?.min ?? "").split("{path}").join(path).split("{min}").join(min.toString()).split("{plural_suffix}").join(min > 1 ? "s" : "")
|
|
54
|
+
);
|
|
55
|
+
if (config.max !== void 0)
|
|
56
|
+
schema2 = schema2.max(
|
|
57
|
+
config.max,
|
|
58
|
+
({ path, max }) => (error_messages?.array?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()).split("{plural_suffix}").join(max > 1 ? "s" : "")
|
|
59
|
+
);
|
|
60
|
+
schema2 = schema2.of(build(key, config.items));
|
|
61
|
+
} 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
|
+
};
|
|
78
|
+
const shape = {};
|
|
79
|
+
for (const [key, config] of Object.entries(schema)) shape[key] = build(key, config);
|
|
80
|
+
return Yup.object().shape(shape);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// src/utils/ConvertToJSONSchema.util.ts
|
|
84
|
+
import { Type } from "@sinclair/typebox";
|
|
85
|
+
var convertToJSONSchema = (schema) => {
|
|
86
|
+
const build = (key, config) => {
|
|
87
|
+
if (config.type === "string") {
|
|
88
|
+
let schema2 = Type.String({ minLength: config.min, maxLength: config.max, pattern: new RegExp(config.pattern ?? "[\\s\\S]*").source, default: config.default });
|
|
89
|
+
if (config.nullable) schema2 = Type.Union([schema2, Type.Null()]);
|
|
90
|
+
return config.required ? schema2 : Type.Optional(schema2);
|
|
91
|
+
} else if (config.type === "number") {
|
|
92
|
+
let schema2 = config.integer === true ? Type.Integer({ minimum: config.min, maximum: config.max, default: config.default }) : Type.Number({ minimum: config.min, maximum: config.max, default: config.default });
|
|
93
|
+
if (config.nullable) schema2 = Type.Union([schema2, Type.Null()]);
|
|
94
|
+
return config.required ? schema2 : Type.Optional(schema2);
|
|
95
|
+
} else if (config.type === "boolean") {
|
|
96
|
+
let schema2 = Type.Boolean({ default: config.default });
|
|
97
|
+
if (config.nullable) schema2 = Type.Union([schema2, Type.Null()]);
|
|
98
|
+
return config.required ? schema2 : Type.Optional(schema2);
|
|
99
|
+
} else if (config.type === "date") {
|
|
100
|
+
let schema2 = Type.String({ format: "date-time", minimum: config.min, maximum: config.max, pattern: new RegExp(config.pattern ?? "[\\s\\S]*").source, default: config.default });
|
|
101
|
+
if (config.nullable) schema2 = Type.Union([schema2, Type.Null()]);
|
|
102
|
+
return config.required ? schema2 : Type.Optional(schema2);
|
|
103
|
+
} else if (config.type === "object") {
|
|
104
|
+
const nested_properties = {};
|
|
105
|
+
for (const [nested_key, nested_config] of Object.entries(config.properties)) nested_properties[nested_key] = build(nested_key, nested_config);
|
|
106
|
+
let schema2 = Type.Object(nested_properties);
|
|
107
|
+
if (config.nullable) schema2 = Type.Union([schema2, Type.Null()]);
|
|
108
|
+
return config.required ? schema2 : Type.Optional(schema2);
|
|
109
|
+
} else if (config.type === "array") {
|
|
110
|
+
let schema2 = Type.Array(build(key, config.items), { minItems: config.min, maxItems: config.max, default: config.default });
|
|
111
|
+
if (config.nullable) schema2 = Type.Union([schema2, Type.Null()]);
|
|
112
|
+
return config.required ? schema2 : Type.Optional(schema2);
|
|
113
|
+
} else throw new Error(`Unsupported schema type for ${key}`);
|
|
114
|
+
};
|
|
115
|
+
const properties = {};
|
|
116
|
+
for (const [key, config] of Object.entries(schema)) properties[key] = build(key, config);
|
|
117
|
+
return Type.Object(properties);
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
// src/defaults/YuppiOptions.default.ts
|
|
121
|
+
var YuppiOptionsDefault = {
|
|
122
|
+
error_messages: {
|
|
123
|
+
base: {
|
|
124
|
+
pattern: "Field {path} must match the required pattern {pattern}",
|
|
125
|
+
nullable: "Field {path} cannot be null",
|
|
126
|
+
required: "Field {path} is required"
|
|
127
|
+
},
|
|
128
|
+
string: {
|
|
129
|
+
type: "Field {path} must be a string",
|
|
130
|
+
min: "Field {path} must be at least {min} character{plural_suffix}",
|
|
131
|
+
max: "Field {path} must be at most {max} character{plural_suffix}"
|
|
132
|
+
},
|
|
133
|
+
number: {
|
|
134
|
+
type: "Field {path} must be a number",
|
|
135
|
+
min: "Field {path} must be greater than or equal to {min}",
|
|
136
|
+
max: "Field {path} must be less than or equal to {max}",
|
|
137
|
+
integer: "Field {path} must be an integer",
|
|
138
|
+
positive: "Field {path} must be a positive number",
|
|
139
|
+
negative: "Field {path} must be a negative number"
|
|
140
|
+
},
|
|
141
|
+
boolean: {
|
|
142
|
+
type: "Field {path} must be a boolean"
|
|
143
|
+
},
|
|
144
|
+
date: {
|
|
145
|
+
type: "Field {path} must be a date",
|
|
146
|
+
min: "Field {path} must be after {min}",
|
|
147
|
+
max: "Field {path} must be before {max}"
|
|
148
|
+
},
|
|
149
|
+
object: {
|
|
150
|
+
type: "Field {path} must be an object"
|
|
151
|
+
},
|
|
152
|
+
array: {
|
|
153
|
+
type: "Field {path} must be an array",
|
|
154
|
+
min: "Field {path} must be at least {min} item${plural_suffix}",
|
|
155
|
+
max: "Field {path} must be at most {max} item${plural_suffix}"
|
|
156
|
+
}
|
|
157
|
+
},
|
|
158
|
+
validate_options: {
|
|
159
|
+
abortEarly: false,
|
|
160
|
+
stripUnknown: true
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
// src/Yuppi.class.ts
|
|
165
|
+
var Yuppi = class {
|
|
166
|
+
constructor(options = YuppiOptionsDefault) {
|
|
167
|
+
this.options = _.merge({}, YuppiOptionsDefault, options);
|
|
168
|
+
}
|
|
169
|
+
async validate(schema, properties) {
|
|
170
|
+
const yup_schema = convertToYup(schema, this.options.error_messages);
|
|
171
|
+
const validation = await yup_schema.validate(properties, this.options.validate_options);
|
|
172
|
+
return validation;
|
|
173
|
+
}
|
|
174
|
+
convertToYup(schema) {
|
|
175
|
+
return convertToYup(schema, this.options.error_messages);
|
|
176
|
+
}
|
|
177
|
+
convertToJSONSchema(schema) {
|
|
178
|
+
return convertToJSONSchema(schema);
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
// src/barrels/Patterns.barrel.ts
|
|
183
|
+
var Patterns_barrel_exports = {};
|
|
184
|
+
__export(Patterns_barrel_exports, {
|
|
185
|
+
Email: () => Email,
|
|
186
|
+
PhoneNumber: () => PhoneNumber,
|
|
187
|
+
URL: () => URL,
|
|
188
|
+
Username: () => Username
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
// src/patterns/Email.pattern.ts
|
|
192
|
+
var Email = "^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$";
|
|
193
|
+
|
|
194
|
+
// src/patterns/PhoneNumber.pattern.ts
|
|
195
|
+
var PhoneNumber = "^\\d{4}\\d{7,12}$";
|
|
196
|
+
|
|
197
|
+
// src/patterns/URL.pattern.ts
|
|
198
|
+
var URL = "^(https?:\\/\\/)?([\\w-]+\\.)+[\\w-]{2,}(\\/[\\w-./?%&=]*)?$";
|
|
199
|
+
|
|
200
|
+
// src/patterns/Username.pattern.ts
|
|
201
|
+
var Username = "^(?=.*[a-zA-Z])[a-zA-Z0-9][a-zA-Z0-9_]*$";
|
|
202
|
+
export {
|
|
203
|
+
Patterns_barrel_exports as Patterns,
|
|
204
|
+
Yuppi
|
|
205
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/patterns/Email.pattern.ts
|
|
21
|
+
var Email_pattern_exports = {};
|
|
22
|
+
__export(Email_pattern_exports, {
|
|
23
|
+
Email: () => Email
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(Email_pattern_exports);
|
|
26
|
+
var Email = "^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$";
|
|
27
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
28
|
+
0 && (module.exports = {
|
|
29
|
+
Email
|
|
30
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/patterns/PhoneNumber.pattern.ts
|
|
21
|
+
var PhoneNumber_pattern_exports = {};
|
|
22
|
+
__export(PhoneNumber_pattern_exports, {
|
|
23
|
+
PhoneNumber: () => PhoneNumber
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(PhoneNumber_pattern_exports);
|
|
26
|
+
var PhoneNumber = "^\\d{4}\\d{7,12}$";
|
|
27
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
28
|
+
0 && (module.exports = {
|
|
29
|
+
PhoneNumber
|
|
30
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/patterns/URL.pattern.ts
|
|
21
|
+
var URL_pattern_exports = {};
|
|
22
|
+
__export(URL_pattern_exports, {
|
|
23
|
+
URL: () => URL
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(URL_pattern_exports);
|
|
26
|
+
var URL = "^(https?:\\/\\/)?([\\w-]+\\.)+[\\w-]{2,}(\\/[\\w-./?%&=]*)?$";
|
|
27
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
28
|
+
0 && (module.exports = {
|
|
29
|
+
URL
|
|
30
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/patterns/Username.pattern.ts
|
|
21
|
+
var Username_pattern_exports = {};
|
|
22
|
+
__export(Username_pattern_exports, {
|
|
23
|
+
Username: () => Username
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(Username_pattern_exports);
|
|
26
|
+
var Username = "^(?=.*[a-zA-Z])[a-zA-Z0-9][a-zA-Z0-9_]*$";
|
|
27
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
28
|
+
0 && (module.exports = {
|
|
29
|
+
Username
|
|
30
|
+
});
|