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/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,17 +1,139 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
1
|
+
import * as yup from 'yup';
|
|
2
|
+
import { AnyObject as AnyObject$1, ValidateOptions as ValidateOptions$1, ValidationError as ValidationError$1 } from 'yup';
|
|
3
|
+
import { TObject, TAnySchema } from '@sinclair/typebox';
|
|
4
|
+
|
|
5
|
+
type AnyObject = AnyObject$1;
|
|
6
|
+
|
|
7
|
+
type JSONSchema = TObject<Record<string, TAnySchema>>;
|
|
8
|
+
|
|
9
|
+
type Base = {
|
|
10
|
+
default?: unknown;
|
|
11
|
+
nullable: boolean;
|
|
12
|
+
required: boolean;
|
|
13
|
+
};
|
|
14
|
+
type String = Base & {
|
|
15
|
+
type: 'string';
|
|
16
|
+
enum?: string[];
|
|
17
|
+
pattern?: string;
|
|
18
|
+
min?: number;
|
|
19
|
+
max?: number;
|
|
20
|
+
lowercase?: boolean;
|
|
21
|
+
uppercase?: boolean;
|
|
22
|
+
};
|
|
23
|
+
type Number = Base & {
|
|
24
|
+
type: 'number';
|
|
25
|
+
enum?: number[];
|
|
26
|
+
min?: number;
|
|
27
|
+
max?: number;
|
|
28
|
+
integer?: boolean;
|
|
29
|
+
positive?: boolean;
|
|
30
|
+
negative?: boolean;
|
|
31
|
+
};
|
|
32
|
+
type Boolean = Base & {
|
|
33
|
+
type: 'boolean';
|
|
34
|
+
};
|
|
35
|
+
type Date = Base & {
|
|
36
|
+
type: 'date';
|
|
37
|
+
min?: string;
|
|
38
|
+
max?: string;
|
|
39
|
+
};
|
|
40
|
+
type Object$1 = Base & {
|
|
41
|
+
type: 'object';
|
|
42
|
+
properties: Schema;
|
|
43
|
+
};
|
|
44
|
+
type Array = Base & {
|
|
45
|
+
type: 'array';
|
|
46
|
+
min?: number;
|
|
47
|
+
max?: number;
|
|
48
|
+
items: Types;
|
|
49
|
+
};
|
|
50
|
+
type Types = String | Number | Boolean | Date | Object$1 | Array;
|
|
51
|
+
type Schema = Record<string, Types>;
|
|
52
|
+
|
|
53
|
+
type ValidateOptions = ValidateOptions$1;
|
|
54
|
+
|
|
55
|
+
type YuppiOptions = {
|
|
56
|
+
error_messages?: {
|
|
57
|
+
base?: {
|
|
58
|
+
nullable?: string;
|
|
59
|
+
required?: string;
|
|
60
|
+
};
|
|
61
|
+
string?: {
|
|
62
|
+
type?: string;
|
|
63
|
+
enum?: string;
|
|
64
|
+
pattern?: string;
|
|
65
|
+
min?: string;
|
|
66
|
+
max?: string;
|
|
67
|
+
};
|
|
68
|
+
number?: {
|
|
69
|
+
type?: string;
|
|
70
|
+
enum?: string;
|
|
71
|
+
min?: string;
|
|
72
|
+
max?: string;
|
|
73
|
+
integer?: string;
|
|
74
|
+
positive?: string;
|
|
75
|
+
negative?: string;
|
|
76
|
+
};
|
|
77
|
+
boolean?: {
|
|
78
|
+
type?: string;
|
|
79
|
+
};
|
|
80
|
+
date?: {
|
|
81
|
+
type?: string;
|
|
82
|
+
min?: string;
|
|
83
|
+
max?: string;
|
|
84
|
+
};
|
|
85
|
+
object?: {
|
|
86
|
+
type?: string;
|
|
87
|
+
};
|
|
88
|
+
array?: {
|
|
89
|
+
type?: string;
|
|
90
|
+
min?: string;
|
|
91
|
+
max?: string;
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
validate_options?: ValidateOptions;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
declare class Yuppi {
|
|
98
|
+
private readonly options;
|
|
99
|
+
constructor(options?: YuppiOptions);
|
|
100
|
+
validate(schema: Schema, properties: AnyObject): {
|
|
101
|
+
[x: string]: any;
|
|
102
|
+
[x: number]: any;
|
|
103
|
+
};
|
|
104
|
+
convertToYup(schema: Schema): yup.ObjectSchema<{
|
|
105
|
+
[x: string]: any;
|
|
106
|
+
}, yup.AnyObject, {
|
|
107
|
+
[x: string]: any;
|
|
108
|
+
}, "">;
|
|
109
|
+
convertToJSONSchema(schema: Schema): JSONSchema;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
declare const Any = "[\\s\\S]*";
|
|
113
|
+
|
|
114
|
+
declare const Domain = "^([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}$";
|
|
115
|
+
|
|
116
|
+
declare const Email = "^[a-zA-Z0-9._-]+@([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}$";
|
|
117
|
+
|
|
118
|
+
declare const HTTP = "^(https?:\\/\\/)([a-zA-Z0-9._%+-]+(:[a-zA-Z0-9._%+-]+)?@)?([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}(:\\d+)?(\\/[a-zA-Z0-9._-~%!$&'()*+,;=:@/]*)?(\\?[a-zA-Z0-9._~%!$&'()*+,;=:@/?-]*)?$";
|
|
119
|
+
|
|
120
|
+
declare const PhoneNumber = "^\\d{4}-\\d{7,12}$";
|
|
121
|
+
|
|
122
|
+
declare const URI = "^([a-zA-Z][a-zA-Z0-9+\\-.]*:\\/\\/)([a-zA-Z0-9._%+-]+(:[a-zA-Z0-9._%+-]+)?@)?([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}(:\\d+)?(\\/[a-zA-Z0-9._-~%!$&'()*+,;=:@/]*)?(\\?[a-zA-Z0-9._~%!$&'()*+,;=:@/?-]*)?$";
|
|
123
|
+
|
|
124
|
+
declare const Username = "^(?=.*[a-zA-Z])[a-zA-Z0-9][a-zA-Z0-9_]*$";
|
|
125
|
+
|
|
126
|
+
declare const Patterns_barrel_Any: typeof Any;
|
|
127
|
+
declare const Patterns_barrel_Domain: typeof Domain;
|
|
128
|
+
declare const Patterns_barrel_Email: typeof Email;
|
|
129
|
+
declare const Patterns_barrel_HTTP: typeof HTTP;
|
|
130
|
+
declare const Patterns_barrel_PhoneNumber: typeof PhoneNumber;
|
|
131
|
+
declare const Patterns_barrel_URI: typeof URI;
|
|
132
|
+
declare const Patterns_barrel_Username: typeof Username;
|
|
133
|
+
declare namespace Patterns_barrel {
|
|
134
|
+
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 };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
type ValidationError = ValidationError$1;
|
|
138
|
+
|
|
139
|
+
export { type AnyObject, type JSONSchema, Patterns_barrel as Patterns, type Schema, type ValidateOptions, type ValidationError, Yuppi, type YuppiOptions };
|
package/dist/main.d.ts
CHANGED
|
@@ -1,17 +1,139 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
1
|
+
import * as yup from 'yup';
|
|
2
|
+
import { AnyObject as AnyObject$1, ValidateOptions as ValidateOptions$1, ValidationError as ValidationError$1 } from 'yup';
|
|
3
|
+
import { TObject, TAnySchema } from '@sinclair/typebox';
|
|
4
|
+
|
|
5
|
+
type AnyObject = AnyObject$1;
|
|
6
|
+
|
|
7
|
+
type JSONSchema = TObject<Record<string, TAnySchema>>;
|
|
8
|
+
|
|
9
|
+
type Base = {
|
|
10
|
+
default?: unknown;
|
|
11
|
+
nullable: boolean;
|
|
12
|
+
required: boolean;
|
|
13
|
+
};
|
|
14
|
+
type String = Base & {
|
|
15
|
+
type: 'string';
|
|
16
|
+
enum?: string[];
|
|
17
|
+
pattern?: string;
|
|
18
|
+
min?: number;
|
|
19
|
+
max?: number;
|
|
20
|
+
lowercase?: boolean;
|
|
21
|
+
uppercase?: boolean;
|
|
22
|
+
};
|
|
23
|
+
type Number = Base & {
|
|
24
|
+
type: 'number';
|
|
25
|
+
enum?: number[];
|
|
26
|
+
min?: number;
|
|
27
|
+
max?: number;
|
|
28
|
+
integer?: boolean;
|
|
29
|
+
positive?: boolean;
|
|
30
|
+
negative?: boolean;
|
|
31
|
+
};
|
|
32
|
+
type Boolean = Base & {
|
|
33
|
+
type: 'boolean';
|
|
34
|
+
};
|
|
35
|
+
type Date = Base & {
|
|
36
|
+
type: 'date';
|
|
37
|
+
min?: string;
|
|
38
|
+
max?: string;
|
|
39
|
+
};
|
|
40
|
+
type Object$1 = Base & {
|
|
41
|
+
type: 'object';
|
|
42
|
+
properties: Schema;
|
|
43
|
+
};
|
|
44
|
+
type Array = Base & {
|
|
45
|
+
type: 'array';
|
|
46
|
+
min?: number;
|
|
47
|
+
max?: number;
|
|
48
|
+
items: Types;
|
|
49
|
+
};
|
|
50
|
+
type Types = String | Number | Boolean | Date | Object$1 | Array;
|
|
51
|
+
type Schema = Record<string, Types>;
|
|
52
|
+
|
|
53
|
+
type ValidateOptions = ValidateOptions$1;
|
|
54
|
+
|
|
55
|
+
type YuppiOptions = {
|
|
56
|
+
error_messages?: {
|
|
57
|
+
base?: {
|
|
58
|
+
nullable?: string;
|
|
59
|
+
required?: string;
|
|
60
|
+
};
|
|
61
|
+
string?: {
|
|
62
|
+
type?: string;
|
|
63
|
+
enum?: string;
|
|
64
|
+
pattern?: string;
|
|
65
|
+
min?: string;
|
|
66
|
+
max?: string;
|
|
67
|
+
};
|
|
68
|
+
number?: {
|
|
69
|
+
type?: string;
|
|
70
|
+
enum?: string;
|
|
71
|
+
min?: string;
|
|
72
|
+
max?: string;
|
|
73
|
+
integer?: string;
|
|
74
|
+
positive?: string;
|
|
75
|
+
negative?: string;
|
|
76
|
+
};
|
|
77
|
+
boolean?: {
|
|
78
|
+
type?: string;
|
|
79
|
+
};
|
|
80
|
+
date?: {
|
|
81
|
+
type?: string;
|
|
82
|
+
min?: string;
|
|
83
|
+
max?: string;
|
|
84
|
+
};
|
|
85
|
+
object?: {
|
|
86
|
+
type?: string;
|
|
87
|
+
};
|
|
88
|
+
array?: {
|
|
89
|
+
type?: string;
|
|
90
|
+
min?: string;
|
|
91
|
+
max?: string;
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
validate_options?: ValidateOptions;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
declare class Yuppi {
|
|
98
|
+
private readonly options;
|
|
99
|
+
constructor(options?: YuppiOptions);
|
|
100
|
+
validate(schema: Schema, properties: AnyObject): {
|
|
101
|
+
[x: string]: any;
|
|
102
|
+
[x: number]: any;
|
|
103
|
+
};
|
|
104
|
+
convertToYup(schema: Schema): yup.ObjectSchema<{
|
|
105
|
+
[x: string]: any;
|
|
106
|
+
}, yup.AnyObject, {
|
|
107
|
+
[x: string]: any;
|
|
108
|
+
}, "">;
|
|
109
|
+
convertToJSONSchema(schema: Schema): JSONSchema;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
declare const Any = "[\\s\\S]*";
|
|
113
|
+
|
|
114
|
+
declare const Domain = "^([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}$";
|
|
115
|
+
|
|
116
|
+
declare const Email = "^[a-zA-Z0-9._-]+@([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}$";
|
|
117
|
+
|
|
118
|
+
declare const HTTP = "^(https?:\\/\\/)([a-zA-Z0-9._%+-]+(:[a-zA-Z0-9._%+-]+)?@)?([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}(:\\d+)?(\\/[a-zA-Z0-9._-~%!$&'()*+,;=:@/]*)?(\\?[a-zA-Z0-9._~%!$&'()*+,;=:@/?-]*)?$";
|
|
119
|
+
|
|
120
|
+
declare const PhoneNumber = "^\\d{4}-\\d{7,12}$";
|
|
121
|
+
|
|
122
|
+
declare const URI = "^([a-zA-Z][a-zA-Z0-9+\\-.]*:\\/\\/)([a-zA-Z0-9._%+-]+(:[a-zA-Z0-9._%+-]+)?@)?([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}(:\\d+)?(\\/[a-zA-Z0-9._-~%!$&'()*+,;=:@/]*)?(\\?[a-zA-Z0-9._~%!$&'()*+,;=:@/?-]*)?$";
|
|
123
|
+
|
|
124
|
+
declare const Username = "^(?=.*[a-zA-Z])[a-zA-Z0-9][a-zA-Z0-9_]*$";
|
|
125
|
+
|
|
126
|
+
declare const Patterns_barrel_Any: typeof Any;
|
|
127
|
+
declare const Patterns_barrel_Domain: typeof Domain;
|
|
128
|
+
declare const Patterns_barrel_Email: typeof Email;
|
|
129
|
+
declare const Patterns_barrel_HTTP: typeof HTTP;
|
|
130
|
+
declare const Patterns_barrel_PhoneNumber: typeof PhoneNumber;
|
|
131
|
+
declare const Patterns_barrel_URI: typeof URI;
|
|
132
|
+
declare const Patterns_barrel_Username: typeof Username;
|
|
133
|
+
declare namespace Patterns_barrel {
|
|
134
|
+
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 };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
type ValidationError = ValidationError$1;
|
|
138
|
+
|
|
139
|
+
export { type AnyObject, type JSONSchema, Patterns_barrel as Patterns, type Schema, type ValidateOptions, type ValidationError, Yuppi, type YuppiOptions };
|
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);
|