yuppi 1.2.6 → 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 +139 -17
- package/dist/main.d.ts +139 -17
- package/dist/main.js +47 -42
- package/dist/main.mjs +47 -42
- package/package.json +3 -3
- package/dist/Patterns.barrel-D-k3IHDq.d.mts +0 -20
- package/dist/Patterns.barrel-JdN3lL2Q.d.ts +0 -20
- package/dist/Yuppi.class.d.mts +0 -24
- package/dist/Yuppi.class.d.ts +0 -24
- package/dist/Yuppi.class.js +0 -244
- package/dist/Yuppi.class.mjs +0 -209
- package/dist/barrels/Patterns.barrel.d.mts +0 -7
- package/dist/barrels/Patterns.barrel.d.ts +0 -7
- package/dist/barrels/Patterns.barrel.js +0 -62
- package/dist/barrels/Patterns.barrel.mjs +0 -29
- package/dist/barrels/Types.barrel.d.mts +0 -8
- package/dist/barrels/Types.barrel.d.ts +0 -8
- package/dist/barrels/Types.barrel.js +0 -18
- package/dist/barrels/Types.barrel.mjs +0 -0
- package/dist/defaults/YuppiOptions.default.d.mts +0 -7
- package/dist/defaults/YuppiOptions.default.d.ts +0 -7
- package/dist/defaults/YuppiOptions.default.js +0 -73
- package/dist/defaults/YuppiOptions.default.mjs +0 -48
- package/dist/patterns/Any.pattern.d.mts +0 -3
- package/dist/patterns/Any.pattern.d.ts +0 -3
- package/dist/patterns/Any.pattern.js +0 -30
- package/dist/patterns/Any.pattern.mjs +0 -5
- package/dist/patterns/Domain.pattern.d.mts +0 -3
- package/dist/patterns/Domain.pattern.d.ts +0 -3
- package/dist/patterns/Domain.pattern.js +0 -30
- package/dist/patterns/Domain.pattern.mjs +0 -5
- package/dist/patterns/Email.pattern.d.mts +0 -3
- package/dist/patterns/Email.pattern.d.ts +0 -3
- package/dist/patterns/Email.pattern.js +0 -30
- package/dist/patterns/Email.pattern.mjs +0 -5
- package/dist/patterns/HTTP.pattern.d.mts +0 -3
- package/dist/patterns/HTTP.pattern.d.ts +0 -3
- package/dist/patterns/HTTP.pattern.js +0 -30
- package/dist/patterns/HTTP.pattern.mjs +0 -5
- package/dist/patterns/PhoneNumber.pattern.d.mts +0 -3
- package/dist/patterns/PhoneNumber.pattern.d.ts +0 -3
- package/dist/patterns/PhoneNumber.pattern.js +0 -30
- package/dist/patterns/PhoneNumber.pattern.mjs +0 -5
- package/dist/patterns/URI.pattern.d.mts +0 -3
- package/dist/patterns/URI.pattern.d.ts +0 -3
- package/dist/patterns/URI.pattern.js +0 -30
- package/dist/patterns/URI.pattern.mjs +0 -5
- package/dist/patterns/Username.pattern.d.mts +0 -3
- package/dist/patterns/Username.pattern.d.ts +0 -3
- package/dist/patterns/Username.pattern.js +0 -30
- package/dist/patterns/Username.pattern.mjs +0 -5
- package/dist/types/AnyObject.type.d.mts +0 -5
- package/dist/types/AnyObject.type.d.ts +0 -5
- package/dist/types/AnyObject.type.js +0 -18
- package/dist/types/AnyObject.type.mjs +0 -0
- package/dist/types/JSONSchema.type.d.mts +0 -5
- package/dist/types/JSONSchema.type.d.ts +0 -5
- package/dist/types/JSONSchema.type.js +0 -18
- package/dist/types/JSONSchema.type.mjs +0 -0
- package/dist/types/Schema.type.d.mts +0 -45
- package/dist/types/Schema.type.d.ts +0 -45
- package/dist/types/Schema.type.js +0 -18
- package/dist/types/Schema.type.mjs +0 -0
- package/dist/types/ValidateOptions.type.d.mts +0 -5
- package/dist/types/ValidateOptions.type.d.ts +0 -5
- package/dist/types/ValidateOptions.type.js +0 -18
- package/dist/types/ValidateOptions.type.mjs +0 -0
- package/dist/types/ValidationError.type.d.mts +0 -5
- package/dist/types/ValidationError.type.d.ts +0 -5
- package/dist/types/ValidationError.type.js +0 -18
- package/dist/types/ValidationError.type.mjs +0 -0
- package/dist/types/YuppiOptions.type.d.mts +0 -46
- package/dist/types/YuppiOptions.type.d.ts +0 -46
- package/dist/types/YuppiOptions.type.js +0 -18
- package/dist/types/YuppiOptions.type.mjs +0 -0
- package/dist/utils/ConvertToJSONSchema.util.d.mts +0 -7
- package/dist/utils/ConvertToJSONSchema.util.d.ts +0 -7
- package/dist/utils/ConvertToJSONSchema.util.js +0 -69
- package/dist/utils/ConvertToJSONSchema.util.mjs +0 -44
- package/dist/utils/ConvertToYup.util.d.mts +0 -12
- package/dist/utils/ConvertToYup.util.d.ts +0 -12
- package/dist/utils/ConvertToYup.util.js +0 -140
- package/dist/utils/ConvertToYup.util.mjs +0 -105
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
|
},
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { Any } from './patterns/Any.pattern.mjs';
|
|
2
|
-
import { Domain } from './patterns/Domain.pattern.mjs';
|
|
3
|
-
import { Email } from './patterns/Email.pattern.mjs';
|
|
4
|
-
import { HTTP } from './patterns/HTTP.pattern.mjs';
|
|
5
|
-
import { PhoneNumber } from './patterns/PhoneNumber.pattern.mjs';
|
|
6
|
-
import { URI } from './patterns/URI.pattern.mjs';
|
|
7
|
-
import { Username } from './patterns/Username.pattern.mjs';
|
|
8
|
-
|
|
9
|
-
declare const Patterns_barrel_Any: typeof Any;
|
|
10
|
-
declare const Patterns_barrel_Domain: typeof Domain;
|
|
11
|
-
declare const Patterns_barrel_Email: typeof Email;
|
|
12
|
-
declare const Patterns_barrel_HTTP: typeof HTTP;
|
|
13
|
-
declare const Patterns_barrel_PhoneNumber: typeof PhoneNumber;
|
|
14
|
-
declare const Patterns_barrel_URI: typeof URI;
|
|
15
|
-
declare const Patterns_barrel_Username: typeof Username;
|
|
16
|
-
declare namespace Patterns_barrel {
|
|
17
|
-
export { Patterns_barrel_Any as Any, Patterns_barrel_Domain as Domain, Patterns_barrel_Email as Email, Patterns_barrel_HTTP as HTTP, Patterns_barrel_PhoneNumber as PhoneNumber, Patterns_barrel_URI as URI, Patterns_barrel_Username as Username };
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export { Patterns_barrel as P };
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { Any } from './patterns/Any.pattern.js';
|
|
2
|
-
import { Domain } from './patterns/Domain.pattern.js';
|
|
3
|
-
import { Email } from './patterns/Email.pattern.js';
|
|
4
|
-
import { HTTP } from './patterns/HTTP.pattern.js';
|
|
5
|
-
import { PhoneNumber } from './patterns/PhoneNumber.pattern.js';
|
|
6
|
-
import { URI } from './patterns/URI.pattern.js';
|
|
7
|
-
import { Username } from './patterns/Username.pattern.js';
|
|
8
|
-
|
|
9
|
-
declare const Patterns_barrel_Any: typeof Any;
|
|
10
|
-
declare const Patterns_barrel_Domain: typeof Domain;
|
|
11
|
-
declare const Patterns_barrel_Email: typeof Email;
|
|
12
|
-
declare const Patterns_barrel_HTTP: typeof HTTP;
|
|
13
|
-
declare const Patterns_barrel_PhoneNumber: typeof PhoneNumber;
|
|
14
|
-
declare const Patterns_barrel_URI: typeof URI;
|
|
15
|
-
declare const Patterns_barrel_Username: typeof Username;
|
|
16
|
-
declare namespace Patterns_barrel {
|
|
17
|
-
export { Patterns_barrel_Any as Any, Patterns_barrel_Domain as Domain, Patterns_barrel_Email as Email, Patterns_barrel_HTTP as HTTP, Patterns_barrel_PhoneNumber as PhoneNumber, Patterns_barrel_URI as URI, Patterns_barrel_Username as Username };
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export { Patterns_barrel as P };
|
package/dist/Yuppi.class.d.mts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import * as yup from 'yup';
|
|
2
|
-
import { AnyObject } from './types/AnyObject.type.mjs';
|
|
3
|
-
import { JSONSchema } from './types/JSONSchema.type.mjs';
|
|
4
|
-
import { Schema } from './types/Schema.type.mjs';
|
|
5
|
-
import { YuppiOptions } from './types/YuppiOptions.type.mjs';
|
|
6
|
-
import '@sinclair/typebox';
|
|
7
|
-
import './types/ValidateOptions.type.mjs';
|
|
8
|
-
|
|
9
|
-
declare class Yuppi {
|
|
10
|
-
private readonly options;
|
|
11
|
-
constructor(options?: YuppiOptions);
|
|
12
|
-
validate(schema: Schema, properties: AnyObject): {
|
|
13
|
-
[x: string]: any;
|
|
14
|
-
[x: number]: any;
|
|
15
|
-
};
|
|
16
|
-
convertToYup(schema: Schema): yup.ObjectSchema<{
|
|
17
|
-
[x: string]: any;
|
|
18
|
-
}, yup.AnyObject, {
|
|
19
|
-
[x: string]: any;
|
|
20
|
-
}, "">;
|
|
21
|
-
convertToJSONSchema(schema: Schema): JSONSchema;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export { Yuppi };
|
package/dist/Yuppi.class.d.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import * as yup from 'yup';
|
|
2
|
-
import { AnyObject } from './types/AnyObject.type.js';
|
|
3
|
-
import { JSONSchema } from './types/JSONSchema.type.js';
|
|
4
|
-
import { Schema } from './types/Schema.type.js';
|
|
5
|
-
import { YuppiOptions } from './types/YuppiOptions.type.js';
|
|
6
|
-
import '@sinclair/typebox';
|
|
7
|
-
import './types/ValidateOptions.type.js';
|
|
8
|
-
|
|
9
|
-
declare class Yuppi {
|
|
10
|
-
private readonly options;
|
|
11
|
-
constructor(options?: YuppiOptions);
|
|
12
|
-
validate(schema: Schema, properties: AnyObject): {
|
|
13
|
-
[x: string]: any;
|
|
14
|
-
[x: number]: any;
|
|
15
|
-
};
|
|
16
|
-
convertToYup(schema: Schema): yup.ObjectSchema<{
|
|
17
|
-
[x: string]: any;
|
|
18
|
-
}, yup.AnyObject, {
|
|
19
|
-
[x: string]: any;
|
|
20
|
-
}, "">;
|
|
21
|
-
convertToJSONSchema(schema: Schema): JSONSchema;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export { Yuppi };
|
package/dist/Yuppi.class.js
DELETED
|
@@ -1,244 +0,0 @@
|
|
|
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/Yuppi.class.ts
|
|
31
|
-
var Yuppi_class_exports = {};
|
|
32
|
-
__export(Yuppi_class_exports, {
|
|
33
|
-
Yuppi: () => Yuppi
|
|
34
|
-
});
|
|
35
|
-
module.exports = __toCommonJS(Yuppi_class_exports);
|
|
36
|
-
var import_lodash = __toESM(require("lodash"));
|
|
37
|
-
|
|
38
|
-
// src/utils/ConvertToYup.util.ts
|
|
39
|
-
var Yup = __toESM(require("yup"));
|
|
40
|
-
|
|
41
|
-
// src/patterns/Any.pattern.ts
|
|
42
|
-
var Any = "[\\s\\S]*";
|
|
43
|
-
|
|
44
|
-
// src/utils/ConvertToYup.util.ts
|
|
45
|
-
var convertToYup = (schema, error_messages) => {
|
|
46
|
-
const base = (schema2, key, config) => {
|
|
47
|
-
schema2 = schema2.nullable();
|
|
48
|
-
if (config.required)
|
|
49
|
-
schema2 = schema2.test(
|
|
50
|
-
"required",
|
|
51
|
-
({ path }) => (error_messages?.base?.required ?? "").split("{path}").join(path),
|
|
52
|
-
(property) => {
|
|
53
|
-
if (property === void 0) return false;
|
|
54
|
-
if (typeof property === "string" && property.trim() === "") return false;
|
|
55
|
-
if (Array.isArray(property) && property.length === 0) return false;
|
|
56
|
-
return true;
|
|
57
|
-
}
|
|
58
|
-
);
|
|
59
|
-
if (!config.nullable && config.default !== null) schema2 = schema2.nonNullable(({ path }) => (error_messages?.base?.nullable ?? "").split("{path}").join(path));
|
|
60
|
-
if (config.default !== void 0) schema2 = schema2.default(config.default);
|
|
61
|
-
if (config.pattern !== void 0 && schema2.matches !== void 0)
|
|
62
|
-
schema2 = schema2.matches(
|
|
63
|
-
new RegExp(config.pattern ?? Any),
|
|
64
|
-
({ path }) => (error_messages?.base?.pattern ?? "").split("{path}").join(path).split("{pattern}").join(new RegExp(config.pattern ?? Any).source)
|
|
65
|
-
);
|
|
66
|
-
return schema2;
|
|
67
|
-
};
|
|
68
|
-
const build = (key, config) => {
|
|
69
|
-
let schema2;
|
|
70
|
-
if (config.type === "string") {
|
|
71
|
-
schema2 = Yup.string().typeError(({ path }) => (error_messages?.string?.type ?? "").split("{path}").join(path));
|
|
72
|
-
schema2 = base(schema2, key, config);
|
|
73
|
-
schema2 = schema2.transform((property) => typeof property === "string" ? property.trim() : property);
|
|
74
|
-
if (config.enum !== void 0)
|
|
75
|
-
schema2 = schema2.oneOf(
|
|
76
|
-
config.enum.map((item) => item.trim()),
|
|
77
|
-
({ path }) => (error_messages?.string?.enum ?? "").split("{path}").join(path)
|
|
78
|
-
);
|
|
79
|
-
if (config.min !== void 0)
|
|
80
|
-
schema2 = schema2.min(
|
|
81
|
-
config.min,
|
|
82
|
-
({ path, min }) => (error_messages?.string?.min ?? "").split("{path}").join(path).split("{min}").join(min.toString()).split("{plural_suffix}").join(min > 1 ? "s" : "")
|
|
83
|
-
);
|
|
84
|
-
if (config.max !== void 0)
|
|
85
|
-
schema2 = schema2.max(
|
|
86
|
-
config.max,
|
|
87
|
-
({ path, max }) => (error_messages?.string?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()).split("{plural_suffix}").join(max > 1 ? "s" : "")
|
|
88
|
-
);
|
|
89
|
-
if (config.lowercase === true) schema2 = schema2.transform((property) => typeof property === "string" ? property.toLowerCase() : property);
|
|
90
|
-
if (config.uppercase === true) schema2 = schema2.transform((property) => typeof property === "string" ? property.toUpperCase() : property);
|
|
91
|
-
return schema2;
|
|
92
|
-
} else if (config.type === "number") {
|
|
93
|
-
schema2 = Yup.number().typeError(({ path }) => (error_messages?.number?.type ?? "").split("{path}").join(path));
|
|
94
|
-
schema2 = base(schema2, key, config);
|
|
95
|
-
if (config.enum !== void 0) schema2 = schema2.oneOf(config.enum, ({ path }) => (error_messages?.number?.enum ?? "").split("{path}").join(path));
|
|
96
|
-
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()));
|
|
97
|
-
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()));
|
|
98
|
-
if (config.integer === true) schema2 = schema2.integer(({ path }) => (error_messages?.number?.integer ?? "").split("{path}").join(path));
|
|
99
|
-
if (config.positive === true) schema2 = schema2.positive(({ path }) => (error_messages?.number?.positive ?? "").split("{path}").join(path));
|
|
100
|
-
if (config.negative === true) schema2 = schema2.negative(({ path }) => (error_messages?.number?.negative ?? "").split("{path}").join(path));
|
|
101
|
-
return schema2;
|
|
102
|
-
} else if (config.type === "boolean") {
|
|
103
|
-
schema2 = Yup.boolean().typeError(({ path }) => (error_messages?.boolean?.type ?? "").split("{path}").join(path));
|
|
104
|
-
schema2 = base(schema2, key, config);
|
|
105
|
-
return schema2;
|
|
106
|
-
} else if (config.type === "date") {
|
|
107
|
-
schema2 = Yup.date().typeError(({ path }) => (error_messages?.date?.type ?? "").split("{path}").join(path));
|
|
108
|
-
schema2 = base(schema2, key, config);
|
|
109
|
-
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()));
|
|
110
|
-
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()));
|
|
111
|
-
return schema2;
|
|
112
|
-
} else if (config.type === "object") {
|
|
113
|
-
schema2 = Yup.object().typeError(({ path }) => (error_messages?.object?.type ?? "").split("{path}").join(path));
|
|
114
|
-
schema2 = base(schema2, key, config);
|
|
115
|
-
const nested_properties = {};
|
|
116
|
-
for (const [nested_key, nested_config] of Object.entries(config.properties)) nested_properties[nested_key] = build(nested_key, nested_config);
|
|
117
|
-
schema2 = schema2.shape(nested_properties);
|
|
118
|
-
return schema2;
|
|
119
|
-
} else if (config.type === "array") {
|
|
120
|
-
schema2 = Yup.array().typeError(({ path }) => (error_messages?.array?.type ?? "").split("{path}").join(path));
|
|
121
|
-
schema2 = base(schema2, key, config);
|
|
122
|
-
if (config.min !== void 0)
|
|
123
|
-
schema2 = schema2.min(
|
|
124
|
-
config.min,
|
|
125
|
-
({ path, min }) => (error_messages?.array?.min ?? "").split("{path}").join(path).split("{min}").join(min.toString()).split("{plural_suffix}").join(min > 1 ? "s" : "")
|
|
126
|
-
);
|
|
127
|
-
if (config.max !== void 0)
|
|
128
|
-
schema2 = schema2.max(
|
|
129
|
-
config.max,
|
|
130
|
-
({ path, max }) => (error_messages?.array?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()).split("{plural_suffix}").join(max > 1 ? "s" : "")
|
|
131
|
-
);
|
|
132
|
-
schema2 = schema2.of(build(key, config.items));
|
|
133
|
-
return schema2;
|
|
134
|
-
} else throw new Error(`Unsupported schema type for ${key}`);
|
|
135
|
-
};
|
|
136
|
-
const properties = {};
|
|
137
|
-
for (const [key, config] of Object.entries(schema)) properties[key] = build(key, config);
|
|
138
|
-
return Yup.object().shape(properties);
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
// src/utils/ConvertToJSONSchema.util.ts
|
|
142
|
-
var import_typebox = require("@sinclair/typebox");
|
|
143
|
-
var convertToJSONSchema = (schema) => {
|
|
144
|
-
const build = (key, config) => {
|
|
145
|
-
if (config.type === "string") {
|
|
146
|
-
let schema2 = import_typebox.Type.String({ enum: config.enum, minLength: config.min, maxLength: config.max, pattern: new RegExp(config.pattern ?? Any).source, default: config.default });
|
|
147
|
-
if (config.nullable) schema2 = import_typebox.Type.Union([schema2, import_typebox.Type.Null()]);
|
|
148
|
-
return config.required ? schema2 : import_typebox.Type.Optional(schema2);
|
|
149
|
-
} else if (config.type === "number") {
|
|
150
|
-
let 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 });
|
|
151
|
-
if (config.nullable) schema2 = import_typebox.Type.Union([schema2, import_typebox.Type.Null()]);
|
|
152
|
-
return config.required ? schema2 : import_typebox.Type.Optional(schema2);
|
|
153
|
-
} else if (config.type === "boolean") {
|
|
154
|
-
let schema2 = import_typebox.Type.Boolean({ default: config.default });
|
|
155
|
-
if (config.nullable) schema2 = import_typebox.Type.Union([schema2, import_typebox.Type.Null()]);
|
|
156
|
-
return config.required ? schema2 : import_typebox.Type.Optional(schema2);
|
|
157
|
-
} else if (config.type === "date") {
|
|
158
|
-
let schema2 = import_typebox.Type.String({ format: "date-time", minimum: config.min, maximum: config.max, pattern: new RegExp(config.pattern ?? Any).source, default: config.default });
|
|
159
|
-
if (config.nullable) schema2 = import_typebox.Type.Union([schema2, import_typebox.Type.Null()]);
|
|
160
|
-
return config.required ? schema2 : import_typebox.Type.Optional(schema2);
|
|
161
|
-
} else if (config.type === "object") {
|
|
162
|
-
const nested_properties = {};
|
|
163
|
-
for (const [nested_key, nested_config] of Object.entries(config.properties)) nested_properties[nested_key] = build(nested_key, nested_config);
|
|
164
|
-
let schema2 = import_typebox.Type.Object(nested_properties);
|
|
165
|
-
if (config.nullable) schema2 = import_typebox.Type.Union([schema2, import_typebox.Type.Null()]);
|
|
166
|
-
return config.required ? schema2 : import_typebox.Type.Optional(schema2);
|
|
167
|
-
} else if (config.type === "array") {
|
|
168
|
-
let schema2 = import_typebox.Type.Array(build(key, config.items), { minItems: config.min, maxItems: config.max, default: config.default });
|
|
169
|
-
if (config.nullable) schema2 = import_typebox.Type.Union([schema2, import_typebox.Type.Null()]);
|
|
170
|
-
return config.required ? schema2 : import_typebox.Type.Optional(schema2);
|
|
171
|
-
} else throw new Error(`Unsupported schema type for ${key}`);
|
|
172
|
-
};
|
|
173
|
-
const properties = {};
|
|
174
|
-
for (const [key, config] of Object.entries(schema)) properties[key] = build(key, config);
|
|
175
|
-
return import_typebox.Type.Object(properties);
|
|
176
|
-
};
|
|
177
|
-
|
|
178
|
-
// src/defaults/YuppiOptions.default.ts
|
|
179
|
-
var YuppiOptionsDefault = {
|
|
180
|
-
error_messages: {
|
|
181
|
-
base: {
|
|
182
|
-
pattern: "Field {path} must match the required pattern {pattern}",
|
|
183
|
-
nullable: "Field {path} cannot be null",
|
|
184
|
-
required: "Field {path} is required"
|
|
185
|
-
},
|
|
186
|
-
string: {
|
|
187
|
-
type: "Field {path} must be a string",
|
|
188
|
-
enum: "Field {path} must be one of the allowed values",
|
|
189
|
-
min: "Field {path} must be at least {min} character{plural_suffix}",
|
|
190
|
-
max: "Field {path} must be at most {max} character{plural_suffix}"
|
|
191
|
-
},
|
|
192
|
-
number: {
|
|
193
|
-
type: "Field {path} must be a number",
|
|
194
|
-
enum: "Field {path} must be one of the allowed values",
|
|
195
|
-
min: "Field {path} must be greater than or equal to {min}",
|
|
196
|
-
max: "Field {path} must be less than or equal to {max}",
|
|
197
|
-
integer: "Field {path} must be an integer",
|
|
198
|
-
positive: "Field {path} must be a positive",
|
|
199
|
-
negative: "Field {path} must be a negative"
|
|
200
|
-
},
|
|
201
|
-
boolean: {
|
|
202
|
-
type: "Field {path} must be a boolean"
|
|
203
|
-
},
|
|
204
|
-
date: {
|
|
205
|
-
type: "Field {path} must be a date",
|
|
206
|
-
min: "Field {path} must be after {min}",
|
|
207
|
-
max: "Field {path} must be before {max}"
|
|
208
|
-
},
|
|
209
|
-
object: {
|
|
210
|
-
type: "Field {path} must be an object"
|
|
211
|
-
},
|
|
212
|
-
array: {
|
|
213
|
-
type: "Field {path} must be an array",
|
|
214
|
-
min: "Field {path} must be at least {min} item${plural_suffix}",
|
|
215
|
-
max: "Field {path} must be at most {max} item${plural_suffix}"
|
|
216
|
-
}
|
|
217
|
-
},
|
|
218
|
-
validate_options: {
|
|
219
|
-
abortEarly: false,
|
|
220
|
-
stripUnknown: true
|
|
221
|
-
}
|
|
222
|
-
};
|
|
223
|
-
|
|
224
|
-
// src/Yuppi.class.ts
|
|
225
|
-
var Yuppi = class {
|
|
226
|
-
constructor(options = YuppiOptionsDefault) {
|
|
227
|
-
this.options = import_lodash.default.merge({}, YuppiOptionsDefault, options);
|
|
228
|
-
}
|
|
229
|
-
validate(schema, properties) {
|
|
230
|
-
const yup_schema = convertToYup(schema, this.options.error_messages);
|
|
231
|
-
const validation = yup_schema.validateSync(properties, this.options.validate_options);
|
|
232
|
-
return validation;
|
|
233
|
-
}
|
|
234
|
-
convertToYup(schema) {
|
|
235
|
-
return convertToYup(schema, this.options.error_messages);
|
|
236
|
-
}
|
|
237
|
-
convertToJSONSchema(schema) {
|
|
238
|
-
return convertToJSONSchema(schema);
|
|
239
|
-
}
|
|
240
|
-
};
|
|
241
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
242
|
-
0 && (module.exports = {
|
|
243
|
-
Yuppi
|
|
244
|
-
});
|