yuppi 1.2.3 → 1.2.5

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.
Files changed (38) hide show
  1. package/README.md +15 -25
  2. package/dist/Yuppi.class.d.mts +10 -3
  3. package/dist/Yuppi.class.d.ts +10 -3
  4. package/dist/Yuppi.class.js +10 -2
  5. package/dist/Yuppi.class.mjs +10 -2
  6. package/dist/defaults/YuppiOptions.default.js +2 -0
  7. package/dist/defaults/YuppiOptions.default.mjs +2 -0
  8. package/dist/main.d.mts +7 -8
  9. package/dist/main.d.ts +7 -8
  10. package/dist/main.js +10 -2
  11. package/dist/main.mjs +10 -2
  12. package/dist/patterns/Any.pattern.d.mts +1 -1
  13. package/dist/patterns/Any.pattern.d.ts +1 -1
  14. package/dist/patterns/Domain.pattern.d.mts +1 -1
  15. package/dist/patterns/Domain.pattern.d.ts +1 -1
  16. package/dist/patterns/Email.pattern.d.mts +1 -1
  17. package/dist/patterns/Email.pattern.d.ts +1 -1
  18. package/dist/patterns/HTTP.pattern.d.mts +1 -1
  19. package/dist/patterns/HTTP.pattern.d.ts +1 -1
  20. package/dist/patterns/PhoneNumber.pattern.d.mts +1 -1
  21. package/dist/patterns/PhoneNumber.pattern.d.ts +1 -1
  22. package/dist/patterns/URI.pattern.d.mts +1 -1
  23. package/dist/patterns/URI.pattern.d.ts +1 -1
  24. package/dist/patterns/Username.pattern.d.mts +1 -1
  25. package/dist/patterns/Username.pattern.d.ts +1 -1
  26. package/dist/types/Schema.type.d.mts +3 -3
  27. package/dist/types/Schema.type.d.ts +3 -3
  28. package/dist/types/YuppiOptions.type.d.mts +2 -0
  29. package/dist/types/YuppiOptions.type.d.ts +2 -0
  30. package/dist/utils/ConvertToJSONSchema.util.js +2 -2
  31. package/dist/utils/ConvertToJSONSchema.util.mjs +2 -2
  32. package/dist/utils/ConvertToYup.util.d.mts +6 -3
  33. package/dist/utils/ConvertToYup.util.d.ts +6 -3
  34. package/dist/utils/ConvertToYup.util.js +6 -0
  35. package/dist/utils/ConvertToYup.util.mjs +6 -0
  36. package/package.json +6 -8
  37. package/dist/Types.barrel--XGUUjIp.d.mts +0 -18
  38. package/dist/Types.barrel-wzQq8e9v.d.ts +0 -18
package/README.md CHANGED
@@ -112,31 +112,21 @@ Yuppi
112
112
  │ ├── URI
113
113
  │ └── Username
114
114
 
115
- └── type Types
116
-
117
- ├── AnyObject
118
- ├── JSONSchema
119
- ├── Schema
120
- ├── ValidateOptions
121
- ├── ValidationError
122
- └── YuppiOptions
115
+ ├── type AnyObject
116
+ ├── type JSONSchema
117
+ ├── type Schema
118
+ ├── type ValidateOptions
119
+ ├── type ValidationError
120
+ └── type YuppiOptions
123
121
  ```
124
122
 
125
123
  ### Import
126
124
 
127
125
  Briefly as follows.
128
126
 
129
- > TypeScript
130
- >
131
- > ```typescript
132
- > import { Yuppi, Patterns, type Types as YuppiTypes } from "yuppi";
133
- > ```
134
- >
135
- > JavaScript
136
- >
137
- > ```javascript
138
- > import { Yuppi, Patterns } from "yuppi";
139
- > ```
127
+ ```typescript
128
+ import { Yuppi, Patterns } from "yuppi";
129
+ ```
140
130
 
141
131
  ### Constructors
142
132
 
@@ -170,7 +160,7 @@ Validate the properties with your Yuppi schema.
170
160
  > Example:
171
161
  >
172
162
  > ```typescript
173
- > const schema: YuppiTypes.Schema = {
163
+ > const schema: Schema = {
174
164
  > display_name: {
175
165
  > type: "string",
176
166
  > min: 1,
@@ -197,7 +187,7 @@ Validate the properties with your Yuppi schema.
197
187
  > }
198
188
  > };
199
189
  >
200
- > const properties: YuppiTypes.AnyObject = {
190
+ > const properties: AnyObject = {
201
191
  > display_name: "Fırat",
202
192
  > username: "fir4tozden",
203
193
  > email: "fir4tozden@gmail.com"
@@ -212,7 +202,7 @@ Validate the properties with your Yuppi schema.
212
202
  > email: "fir4tozden@gmail.com"
213
203
  > }
214
204
  > */
215
- > } catch((error: YuppiTypes.ValidationError)) {
205
+ > } catch((error: ValidationError)) {
216
206
  > console.log(error.message); // "Field email must match the required pattern ^[a-zA-Z0-9._-]+@([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}$"
217
207
  > }
218
208
  > ```
@@ -232,7 +222,7 @@ Convert your Yuppi schema into Yup schema.
232
222
  > Example:
233
223
  >
234
224
  > ```typescript
235
- > const schema: YuppiTypes.Schema = {
225
+ > const schema: Schema = {
236
226
  > display_name: {
237
227
  > type: "string",
238
228
  > min: 1,
@@ -272,12 +262,12 @@ Convert your Yuppi schema into [JSON Schema](https://json-schema.org).
272
262
  > | --------- | ------- | -------------------------- |
273
263
  > | schema | | [Schema]<br/>Yuppi schema. |
274
264
  >
275
- > returns [AnyObject]
265
+ > returns [JSONSchema]
276
266
  >
277
267
  > Example:
278
268
  >
279
269
  > ```typescript
280
- > const schema: YuppiTypes.Schema = {
270
+ > const schema: Schema = {
281
271
  > display_name: {
282
272
  > type: "string",
283
273
  > min: 1,
@@ -1,16 +1,23 @@
1
+ import * as yup from 'yup';
1
2
  import { AnyObject } from './types/AnyObject.type.mjs';
2
3
  import { JSONSchema } from './types/JSONSchema.type.mjs';
3
4
  import { Schema } from './types/Schema.type.mjs';
4
5
  import { YuppiOptions } from './types/YuppiOptions.type.mjs';
5
- import 'yup';
6
6
  import '@sinclair/typebox';
7
7
  import './types/ValidateOptions.type.mjs';
8
8
 
9
9
  declare class Yuppi {
10
10
  private readonly options;
11
11
  constructor(options?: YuppiOptions);
12
- validate(schema: Schema, properties: AnyObject): AnyObject;
13
- convertToYup(schema: Schema): AnyObject;
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
+ }, "">;
14
21
  convertToJSONSchema(schema: Schema): JSONSchema;
15
22
  }
16
23
 
@@ -1,16 +1,23 @@
1
+ import * as yup from 'yup';
1
2
  import { AnyObject } from './types/AnyObject.type.js';
2
3
  import { JSONSchema } from './types/JSONSchema.type.js';
3
4
  import { Schema } from './types/Schema.type.js';
4
5
  import { YuppiOptions } from './types/YuppiOptions.type.js';
5
- import 'yup';
6
6
  import '@sinclair/typebox';
7
7
  import './types/ValidateOptions.type.js';
8
8
 
9
9
  declare class Yuppi {
10
10
  private readonly options;
11
11
  constructor(options?: YuppiOptions);
12
- validate(schema: Schema, properties: AnyObject): AnyObject;
13
- convertToYup(schema: Schema): AnyObject;
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
+ }, "">;
14
21
  convertToJSONSchema(schema: Schema): JSONSchema;
15
22
  }
16
23
 
@@ -71,6 +71,11 @@ var convertToYup = (schema, error_messages) => {
71
71
  schema2 = Yup.string().typeError(({ path }) => (error_messages?.string?.type ?? "").split("{path}").join(path));
72
72
  schema2 = base(schema2, key, config);
73
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
+ );
74
79
  if (config.min !== void 0)
75
80
  schema2 = schema2.min(
76
81
  config.min,
@@ -87,6 +92,7 @@ var convertToYup = (schema, error_messages) => {
87
92
  } else if (config.type === "number") {
88
93
  schema2 = Yup.number().typeError(({ path }) => (error_messages?.number?.type ?? "").split("{path}").join(path));
89
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));
90
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()));
91
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()));
92
98
  if (config.integer === true) schema2 = schema2.integer(({ path }) => (error_messages?.number?.integer ?? "").split("{path}").join(path));
@@ -137,11 +143,11 @@ var import_typebox = require("@sinclair/typebox");
137
143
  var convertToJSONSchema = (schema) => {
138
144
  const build = (key, config) => {
139
145
  if (config.type === "string") {
140
- let schema2 = import_typebox.Type.String({ minLength: config.min, maxLength: config.max, pattern: new RegExp(config.pattern ?? Any).source, default: config.default });
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 });
141
147
  if (config.nullable) schema2 = import_typebox.Type.Union([schema2, import_typebox.Type.Null()]);
142
148
  return config.required ? schema2 : import_typebox.Type.Optional(schema2);
143
149
  } else if (config.type === "number") {
144
- 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 });
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 });
145
151
  if (config.nullable) schema2 = import_typebox.Type.Union([schema2, import_typebox.Type.Null()]);
146
152
  return config.required ? schema2 : import_typebox.Type.Optional(schema2);
147
153
  } else if (config.type === "boolean") {
@@ -179,11 +185,13 @@ var YuppiOptionsDefault = {
179
185
  },
180
186
  string: {
181
187
  type: "Field {path} must be a string",
188
+ enum: "Field {path} must be one of the allowed values",
182
189
  min: "Field {path} must be at least {min} character{plural_suffix}",
183
190
  max: "Field {path} must be at most {max} character{plural_suffix}"
184
191
  },
185
192
  number: {
186
193
  type: "Field {path} must be a number",
194
+ enum: "Field {path} must be one of the allowed values",
187
195
  min: "Field {path} must be greater than or equal to {min}",
188
196
  max: "Field {path} must be less than or equal to {max}",
189
197
  integer: "Field {path} must be an integer",
@@ -37,6 +37,11 @@ var convertToYup = (schema, error_messages) => {
37
37
  schema2 = Yup.string().typeError(({ path }) => (error_messages?.string?.type ?? "").split("{path}").join(path));
38
38
  schema2 = base(schema2, key, config);
39
39
  schema2 = schema2.transform((property) => typeof property === "string" ? property.trim() : property);
40
+ if (config.enum !== void 0)
41
+ schema2 = schema2.oneOf(
42
+ config.enum.map((item) => item.trim()),
43
+ ({ path }) => (error_messages?.string?.enum ?? "").split("{path}").join(path)
44
+ );
40
45
  if (config.min !== void 0)
41
46
  schema2 = schema2.min(
42
47
  config.min,
@@ -53,6 +58,7 @@ var convertToYup = (schema, error_messages) => {
53
58
  } else if (config.type === "number") {
54
59
  schema2 = Yup.number().typeError(({ path }) => (error_messages?.number?.type ?? "").split("{path}").join(path));
55
60
  schema2 = base(schema2, key, config);
61
+ if (config.enum !== void 0) schema2 = schema2.oneOf(config.enum, ({ path }) => (error_messages?.number?.enum ?? "").split("{path}").join(path));
56
62
  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()));
57
63
  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()));
58
64
  if (config.integer === true) schema2 = schema2.integer(({ path }) => (error_messages?.number?.integer ?? "").split("{path}").join(path));
@@ -103,11 +109,11 @@ import { Type } from "@sinclair/typebox";
103
109
  var convertToJSONSchema = (schema) => {
104
110
  const build = (key, config) => {
105
111
  if (config.type === "string") {
106
- let schema2 = Type.String({ minLength: config.min, maxLength: config.max, pattern: new RegExp(config.pattern ?? Any).source, default: config.default });
112
+ let schema2 = Type.String({ enum: config.enum, minLength: config.min, maxLength: config.max, pattern: new RegExp(config.pattern ?? Any).source, default: config.default });
107
113
  if (config.nullable) schema2 = Type.Union([schema2, Type.Null()]);
108
114
  return config.required ? schema2 : Type.Optional(schema2);
109
115
  } else if (config.type === "number") {
110
- 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 });
116
+ let 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 });
111
117
  if (config.nullable) schema2 = Type.Union([schema2, Type.Null()]);
112
118
  return config.required ? schema2 : Type.Optional(schema2);
113
119
  } else if (config.type === "boolean") {
@@ -145,11 +151,13 @@ var YuppiOptionsDefault = {
145
151
  },
146
152
  string: {
147
153
  type: "Field {path} must be a string",
154
+ enum: "Field {path} must be one of the allowed values",
148
155
  min: "Field {path} must be at least {min} character{plural_suffix}",
149
156
  max: "Field {path} must be at most {max} character{plural_suffix}"
150
157
  },
151
158
  number: {
152
159
  type: "Field {path} must be a number",
160
+ enum: "Field {path} must be one of the allowed values",
153
161
  min: "Field {path} must be greater than or equal to {min}",
154
162
  max: "Field {path} must be less than or equal to {max}",
155
163
  integer: "Field {path} must be an integer",
@@ -32,11 +32,13 @@ var YuppiOptionsDefault = {
32
32
  },
33
33
  string: {
34
34
  type: "Field {path} must be a string",
35
+ enum: "Field {path} must be one of the allowed values",
35
36
  min: "Field {path} must be at least {min} character{plural_suffix}",
36
37
  max: "Field {path} must be at most {max} character{plural_suffix}"
37
38
  },
38
39
  number: {
39
40
  type: "Field {path} must be a number",
41
+ enum: "Field {path} must be one of the allowed values",
40
42
  min: "Field {path} must be greater than or equal to {min}",
41
43
  max: "Field {path} must be less than or equal to {max}",
42
44
  integer: "Field {path} must be an integer",
@@ -8,11 +8,13 @@ var YuppiOptionsDefault = {
8
8
  },
9
9
  string: {
10
10
  type: "Field {path} must be a string",
11
+ enum: "Field {path} must be one of the allowed values",
11
12
  min: "Field {path} must be at least {min} character{plural_suffix}",
12
13
  max: "Field {path} must be at most {max} character{plural_suffix}"
13
14
  },
14
15
  number: {
15
16
  type: "Field {path} must be a number",
17
+ enum: "Field {path} must be one of the allowed values",
16
18
  min: "Field {path} must be greater than or equal to {min}",
17
19
  max: "Field {path} must be less than or equal to {max}",
18
20
  integer: "Field {path} must be an integer",
package/dist/main.d.mts CHANGED
@@ -1,13 +1,12 @@
1
1
  export { Yuppi } from './Yuppi.class.mjs';
2
2
  export { P as Patterns } from './Patterns.barrel-D-k3IHDq.mjs';
3
- export { T as Types } from './Types.barrel--XGUUjIp.mjs';
4
- import './types/AnyObject.type.mjs';
3
+ export { AnyObject } from './types/AnyObject.type.mjs';
4
+ export { JSONSchema } from './types/JSONSchema.type.mjs';
5
+ export { Schema } from './types/Schema.type.mjs';
6
+ export { ValidateOptions } from './types/ValidateOptions.type.mjs';
7
+ export { ValidationError } from './types/ValidationError.type.mjs';
8
+ export { YuppiOptions } from './types/YuppiOptions.type.mjs';
5
9
  import 'yup';
6
- import './types/JSONSchema.type.mjs';
7
- import '@sinclair/typebox';
8
- import './types/Schema.type.mjs';
9
- import './types/YuppiOptions.type.mjs';
10
- import './types/ValidateOptions.type.mjs';
11
10
  import './patterns/Any.pattern.mjs';
12
11
  import './patterns/Domain.pattern.mjs';
13
12
  import './patterns/Email.pattern.mjs';
@@ -15,4 +14,4 @@ import './patterns/HTTP.pattern.mjs';
15
14
  import './patterns/PhoneNumber.pattern.mjs';
16
15
  import './patterns/URI.pattern.mjs';
17
16
  import './patterns/Username.pattern.mjs';
18
- import './types/ValidationError.type.mjs';
17
+ import '@sinclair/typebox';
package/dist/main.d.ts CHANGED
@@ -1,13 +1,12 @@
1
1
  export { Yuppi } from './Yuppi.class.js';
2
2
  export { P as Patterns } from './Patterns.barrel-JdN3lL2Q.js';
3
- export { T as Types } from './Types.barrel-wzQq8e9v.js';
4
- import './types/AnyObject.type.js';
3
+ export { AnyObject } from './types/AnyObject.type.js';
4
+ export { JSONSchema } from './types/JSONSchema.type.js';
5
+ export { Schema } from './types/Schema.type.js';
6
+ export { ValidateOptions } from './types/ValidateOptions.type.js';
7
+ export { ValidationError } from './types/ValidationError.type.js';
8
+ export { YuppiOptions } from './types/YuppiOptions.type.js';
5
9
  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
10
  import './patterns/Any.pattern.js';
12
11
  import './patterns/Domain.pattern.js';
13
12
  import './patterns/Email.pattern.js';
@@ -15,4 +14,4 @@ import './patterns/HTTP.pattern.js';
15
14
  import './patterns/PhoneNumber.pattern.js';
16
15
  import './patterns/URI.pattern.js';
17
16
  import './patterns/Username.pattern.js';
18
- import './types/ValidationError.type.js';
17
+ import '@sinclair/typebox';
package/dist/main.js CHANGED
@@ -74,6 +74,11 @@ var convertToYup = (schema, error_messages) => {
74
74
  schema2 = Yup.string().typeError(({ path }) => (error_messages?.string?.type ?? "").split("{path}").join(path));
75
75
  schema2 = base(schema2, key, config);
76
76
  schema2 = schema2.transform((property) => typeof property === "string" ? property.trim() : property);
77
+ if (config.enum !== void 0)
78
+ schema2 = schema2.oneOf(
79
+ config.enum.map((item) => item.trim()),
80
+ ({ path }) => (error_messages?.string?.enum ?? "").split("{path}").join(path)
81
+ );
77
82
  if (config.min !== void 0)
78
83
  schema2 = schema2.min(
79
84
  config.min,
@@ -90,6 +95,7 @@ var convertToYup = (schema, error_messages) => {
90
95
  } else if (config.type === "number") {
91
96
  schema2 = Yup.number().typeError(({ path }) => (error_messages?.number?.type ?? "").split("{path}").join(path));
92
97
  schema2 = base(schema2, key, config);
98
+ if (config.enum !== void 0) schema2 = schema2.oneOf(config.enum, ({ path }) => (error_messages?.number?.enum ?? "").split("{path}").join(path));
93
99
  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()));
94
100
  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()));
95
101
  if (config.integer === true) schema2 = schema2.integer(({ path }) => (error_messages?.number?.integer ?? "").split("{path}").join(path));
@@ -140,11 +146,11 @@ var import_typebox = require("@sinclair/typebox");
140
146
  var convertToJSONSchema = (schema) => {
141
147
  const build = (key, config) => {
142
148
  if (config.type === "string") {
143
- let schema2 = import_typebox.Type.String({ minLength: config.min, maxLength: config.max, pattern: new RegExp(config.pattern ?? Any).source, default: config.default });
149
+ 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 });
144
150
  if (config.nullable) schema2 = import_typebox.Type.Union([schema2, import_typebox.Type.Null()]);
145
151
  return config.required ? schema2 : import_typebox.Type.Optional(schema2);
146
152
  } else if (config.type === "number") {
147
- 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 });
153
+ 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 });
148
154
  if (config.nullable) schema2 = import_typebox.Type.Union([schema2, import_typebox.Type.Null()]);
149
155
  return config.required ? schema2 : import_typebox.Type.Optional(schema2);
150
156
  } else if (config.type === "boolean") {
@@ -182,11 +188,13 @@ var YuppiOptionsDefault = {
182
188
  },
183
189
  string: {
184
190
  type: "Field {path} must be a string",
191
+ enum: "Field {path} must be one of the allowed values",
185
192
  min: "Field {path} must be at least {min} character{plural_suffix}",
186
193
  max: "Field {path} must be at most {max} character{plural_suffix}"
187
194
  },
188
195
  number: {
189
196
  type: "Field {path} must be a number",
197
+ enum: "Field {path} must be one of the allowed values",
190
198
  min: "Field {path} must be greater than or equal to {min}",
191
199
  max: "Field {path} must be less than or equal to {max}",
192
200
  integer: "Field {path} must be an integer",
package/dist/main.mjs CHANGED
@@ -43,6 +43,11 @@ var convertToYup = (schema, error_messages) => {
43
43
  schema2 = Yup.string().typeError(({ path }) => (error_messages?.string?.type ?? "").split("{path}").join(path));
44
44
  schema2 = base(schema2, key, config);
45
45
  schema2 = schema2.transform((property) => typeof property === "string" ? property.trim() : property);
46
+ if (config.enum !== void 0)
47
+ schema2 = schema2.oneOf(
48
+ config.enum.map((item) => item.trim()),
49
+ ({ path }) => (error_messages?.string?.enum ?? "").split("{path}").join(path)
50
+ );
46
51
  if (config.min !== void 0)
47
52
  schema2 = schema2.min(
48
53
  config.min,
@@ -59,6 +64,7 @@ var convertToYup = (schema, error_messages) => {
59
64
  } else if (config.type === "number") {
60
65
  schema2 = Yup.number().typeError(({ path }) => (error_messages?.number?.type ?? "").split("{path}").join(path));
61
66
  schema2 = base(schema2, key, config);
67
+ if (config.enum !== void 0) schema2 = schema2.oneOf(config.enum, ({ path }) => (error_messages?.number?.enum ?? "").split("{path}").join(path));
62
68
  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()));
63
69
  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()));
64
70
  if (config.integer === true) schema2 = schema2.integer(({ path }) => (error_messages?.number?.integer ?? "").split("{path}").join(path));
@@ -109,11 +115,11 @@ import { Type } from "@sinclair/typebox";
109
115
  var convertToJSONSchema = (schema) => {
110
116
  const build = (key, config) => {
111
117
  if (config.type === "string") {
112
- let schema2 = Type.String({ minLength: config.min, maxLength: config.max, pattern: new RegExp(config.pattern ?? Any).source, default: config.default });
118
+ let schema2 = Type.String({ enum: config.enum, minLength: config.min, maxLength: config.max, pattern: new RegExp(config.pattern ?? Any).source, default: config.default });
113
119
  if (config.nullable) schema2 = Type.Union([schema2, Type.Null()]);
114
120
  return config.required ? schema2 : Type.Optional(schema2);
115
121
  } else if (config.type === "number") {
116
- 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 });
122
+ let 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 });
117
123
  if (config.nullable) schema2 = Type.Union([schema2, Type.Null()]);
118
124
  return config.required ? schema2 : Type.Optional(schema2);
119
125
  } else if (config.type === "boolean") {
@@ -151,11 +157,13 @@ var YuppiOptionsDefault = {
151
157
  },
152
158
  string: {
153
159
  type: "Field {path} must be a string",
160
+ enum: "Field {path} must be one of the allowed values",
154
161
  min: "Field {path} must be at least {min} character{plural_suffix}",
155
162
  max: "Field {path} must be at most {max} character{plural_suffix}"
156
163
  },
157
164
  number: {
158
165
  type: "Field {path} must be a number",
166
+ enum: "Field {path} must be one of the allowed values",
159
167
  min: "Field {path} must be greater than or equal to {min}",
160
168
  max: "Field {path} must be less than or equal to {max}",
161
169
  integer: "Field {path} must be an integer",
@@ -1,3 +1,3 @@
1
- declare const Any: string;
1
+ declare const Any = "[\\s\\S]*";
2
2
 
3
3
  export { Any };
@@ -1,3 +1,3 @@
1
- declare const Any: string;
1
+ declare const Any = "[\\s\\S]*";
2
2
 
3
3
  export { Any };
@@ -1,3 +1,3 @@
1
- declare const Domain: string;
1
+ declare const Domain = "^([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}$";
2
2
 
3
3
  export { Domain };
@@ -1,3 +1,3 @@
1
- declare const Domain: string;
1
+ declare const Domain = "^([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}$";
2
2
 
3
3
  export { Domain };
@@ -1,3 +1,3 @@
1
- declare const Email: string;
1
+ declare const Email = "^[a-zA-Z0-9._-]+@([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}$";
2
2
 
3
3
  export { Email };
@@ -1,3 +1,3 @@
1
- declare const Email: string;
1
+ declare const Email = "^[a-zA-Z0-9._-]+@([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}$";
2
2
 
3
3
  export { Email };
@@ -1,3 +1,3 @@
1
- declare const HTTP: string;
1
+ 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._~%!$&'()*+,;=:@/?-]*)?$";
2
2
 
3
3
  export { HTTP };
@@ -1,3 +1,3 @@
1
- declare const HTTP: string;
1
+ 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._~%!$&'()*+,;=:@/?-]*)?$";
2
2
 
3
3
  export { HTTP };
@@ -1,3 +1,3 @@
1
- declare const PhoneNumber: string;
1
+ declare const PhoneNumber = "^\\d{4}-\\d{7,12}$";
2
2
 
3
3
  export { PhoneNumber };
@@ -1,3 +1,3 @@
1
- declare const PhoneNumber: string;
1
+ declare const PhoneNumber = "^\\d{4}-\\d{7,12}$";
2
2
 
3
3
  export { PhoneNumber };
@@ -1,3 +1,3 @@
1
- declare const URI: string;
1
+ 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._~%!$&'()*+,;=:@/?-]*)?$";
2
2
 
3
3
  export { URI };
@@ -1,3 +1,3 @@
1
- declare const URI: string;
1
+ 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._~%!$&'()*+,;=:@/?-]*)?$";
2
2
 
3
3
  export { URI };
@@ -1,3 +1,3 @@
1
- declare const Username: string;
1
+ declare const Username = "^(?=.*[a-zA-Z])[a-zA-Z0-9][a-zA-Z0-9_]*$";
2
2
 
3
3
  export { Username };
@@ -1,3 +1,3 @@
1
- declare const Username: string;
1
+ declare const Username = "^(?=.*[a-zA-Z])[a-zA-Z0-9][a-zA-Z0-9_]*$";
2
2
 
3
3
  export { Username };
@@ -6,6 +6,7 @@ type Base = {
6
6
  };
7
7
  type String = Base & {
8
8
  type: "string";
9
+ enum?: string[];
9
10
  min?: number;
10
11
  max?: number;
11
12
  lowercase?: boolean;
@@ -13,6 +14,7 @@ type String = Base & {
13
14
  };
14
15
  type Number = Base & {
15
16
  type: "number";
17
+ enum?: number[];
16
18
  min?: number;
17
19
  max?: number;
18
20
  integer?: boolean;
@@ -38,8 +40,6 @@ type Array = Base & {
38
40
  items: Types;
39
41
  };
40
42
  type Types = String | Number | Boolean | Date | Object$1 | Array;
41
- type Schema = {
42
- [key: string]: Types;
43
- };
43
+ type Schema = Record<string, Types>;
44
44
 
45
45
  export type { Array, Base, Boolean, Date, Number, Object$1 as Object, Schema, String, Types };
@@ -6,6 +6,7 @@ type Base = {
6
6
  };
7
7
  type String = Base & {
8
8
  type: "string";
9
+ enum?: string[];
9
10
  min?: number;
10
11
  max?: number;
11
12
  lowercase?: boolean;
@@ -13,6 +14,7 @@ type String = Base & {
13
14
  };
14
15
  type Number = Base & {
15
16
  type: "number";
17
+ enum?: number[];
16
18
  min?: number;
17
19
  max?: number;
18
20
  integer?: boolean;
@@ -38,8 +40,6 @@ type Array = Base & {
38
40
  items: Types;
39
41
  };
40
42
  type Types = String | Number | Boolean | Date | Object$1 | Array;
41
- type Schema = {
42
- [key: string]: Types;
43
- };
43
+ type Schema = Record<string, Types>;
44
44
 
45
45
  export type { Array, Base, Boolean, Date, Number, Object$1 as Object, Schema, String, Types };
@@ -10,11 +10,13 @@ type YuppiOptions = {
10
10
  };
11
11
  string?: {
12
12
  type?: string;
13
+ enum?: string;
13
14
  min?: string;
14
15
  max?: string;
15
16
  };
16
17
  number?: {
17
18
  type?: string;
19
+ enum?: string;
18
20
  min?: string;
19
21
  max?: string;
20
22
  integer?: string;
@@ -10,11 +10,13 @@ type YuppiOptions = {
10
10
  };
11
11
  string?: {
12
12
  type?: string;
13
+ enum?: string;
13
14
  min?: string;
14
15
  max?: string;
15
16
  };
16
17
  number?: {
17
18
  type?: string;
19
+ enum?: string;
18
20
  min?: string;
19
21
  max?: string;
20
22
  integer?: string;
@@ -32,11 +32,11 @@ var Any = "[\\s\\S]*";
32
32
  var convertToJSONSchema = (schema) => {
33
33
  const build = (key, config) => {
34
34
  if (config.type === "string") {
35
- let schema2 = import_typebox.Type.String({ minLength: config.min, maxLength: config.max, pattern: new RegExp(config.pattern ?? Any).source, default: config.default });
35
+ 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 });
36
36
  if (config.nullable) schema2 = import_typebox.Type.Union([schema2, import_typebox.Type.Null()]);
37
37
  return config.required ? schema2 : import_typebox.Type.Optional(schema2);
38
38
  } else if (config.type === "number") {
39
- 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 });
39
+ 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 });
40
40
  if (config.nullable) schema2 = import_typebox.Type.Union([schema2, import_typebox.Type.Null()]);
41
41
  return config.required ? schema2 : import_typebox.Type.Optional(schema2);
42
42
  } else if (config.type === "boolean") {
@@ -8,11 +8,11 @@ var Any = "[\\s\\S]*";
8
8
  var convertToJSONSchema = (schema) => {
9
9
  const build = (key, config) => {
10
10
  if (config.type === "string") {
11
- let schema2 = Type.String({ minLength: config.min, maxLength: config.max, pattern: new RegExp(config.pattern ?? Any).source, default: config.default });
11
+ let schema2 = Type.String({ enum: config.enum, minLength: config.min, maxLength: config.max, pattern: new RegExp(config.pattern ?? Any).source, default: config.default });
12
12
  if (config.nullable) schema2 = Type.Union([schema2, Type.Null()]);
13
13
  return config.required ? schema2 : Type.Optional(schema2);
14
14
  } else if (config.type === "number") {
15
- 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 });
15
+ let 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 });
16
16
  if (config.nullable) schema2 = Type.Union([schema2, Type.Null()]);
17
17
  return config.required ? schema2 : Type.Optional(schema2);
18
18
  } else if (config.type === "boolean") {
@@ -1,9 +1,12 @@
1
- import { AnyObject } from '../types/AnyObject.type.mjs';
1
+ import * as yup from 'yup';
2
2
  import { Schema } from '../types/Schema.type.mjs';
3
3
  import { YuppiOptions } from '../types/YuppiOptions.type.mjs';
4
- import 'yup';
5
4
  import '../types/ValidateOptions.type.mjs';
6
5
 
7
- declare const convertToYup: (schema: Schema, error_messages: YuppiOptions["error_messages"]) => AnyObject;
6
+ declare const convertToYup: (schema: Schema, error_messages: YuppiOptions["error_messages"]) => yup.ObjectSchema<{
7
+ [x: string]: any;
8
+ }, yup.AnyObject, {
9
+ [x: string]: any;
10
+ }, "">;
8
11
 
9
12
  export { convertToYup };
@@ -1,9 +1,12 @@
1
- import { AnyObject } from '../types/AnyObject.type.js';
1
+ import * as yup from 'yup';
2
2
  import { Schema } from '../types/Schema.type.js';
3
3
  import { YuppiOptions } from '../types/YuppiOptions.type.js';
4
- import 'yup';
5
4
  import '../types/ValidateOptions.type.js';
6
5
 
7
- declare const convertToYup: (schema: Schema, error_messages: YuppiOptions["error_messages"]) => AnyObject;
6
+ declare const convertToYup: (schema: Schema, error_messages: YuppiOptions["error_messages"]) => yup.ObjectSchema<{
7
+ [x: string]: any;
8
+ }, yup.AnyObject, {
9
+ [x: string]: any;
10
+ }, "">;
8
11
 
9
12
  export { convertToYup };
@@ -68,6 +68,11 @@ var convertToYup = (schema, error_messages) => {
68
68
  schema2 = Yup.string().typeError(({ path }) => (error_messages?.string?.type ?? "").split("{path}").join(path));
69
69
  schema2 = base(schema2, key, config);
70
70
  schema2 = schema2.transform((property) => typeof property === "string" ? property.trim() : property);
71
+ if (config.enum !== void 0)
72
+ schema2 = schema2.oneOf(
73
+ config.enum.map((item) => item.trim()),
74
+ ({ path }) => (error_messages?.string?.enum ?? "").split("{path}").join(path)
75
+ );
71
76
  if (config.min !== void 0)
72
77
  schema2 = schema2.min(
73
78
  config.min,
@@ -84,6 +89,7 @@ var convertToYup = (schema, error_messages) => {
84
89
  } else if (config.type === "number") {
85
90
  schema2 = Yup.number().typeError(({ path }) => (error_messages?.number?.type ?? "").split("{path}").join(path));
86
91
  schema2 = base(schema2, key, config);
92
+ if (config.enum !== void 0) schema2 = schema2.oneOf(config.enum, ({ path }) => (error_messages?.number?.enum ?? "").split("{path}").join(path));
87
93
  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()));
88
94
  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()));
89
95
  if (config.integer === true) schema2 = schema2.integer(({ path }) => (error_messages?.number?.integer ?? "").split("{path}").join(path));
@@ -34,6 +34,11 @@ var convertToYup = (schema, error_messages) => {
34
34
  schema2 = Yup.string().typeError(({ path }) => (error_messages?.string?.type ?? "").split("{path}").join(path));
35
35
  schema2 = base(schema2, key, config);
36
36
  schema2 = schema2.transform((property) => typeof property === "string" ? property.trim() : property);
37
+ if (config.enum !== void 0)
38
+ schema2 = schema2.oneOf(
39
+ config.enum.map((item) => item.trim()),
40
+ ({ path }) => (error_messages?.string?.enum ?? "").split("{path}").join(path)
41
+ );
37
42
  if (config.min !== void 0)
38
43
  schema2 = schema2.min(
39
44
  config.min,
@@ -50,6 +55,7 @@ var convertToYup = (schema, error_messages) => {
50
55
  } else if (config.type === "number") {
51
56
  schema2 = Yup.number().typeError(({ path }) => (error_messages?.number?.type ?? "").split("{path}").join(path));
52
57
  schema2 = base(schema2, key, config);
58
+ if (config.enum !== void 0) schema2 = schema2.oneOf(config.enum, ({ path }) => (error_messages?.number?.enum ?? "").split("{path}").join(path));
53
59
  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()));
54
60
  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()));
55
61
  if (config.integer === true) schema2 = schema2.integer(({ path }) => (error_messages?.number?.integer ?? "").split("{path}").join(path));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yuppi",
3
- "version": "1.2.3",
3
+ "version": "1.2.5",
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",
@@ -10,22 +10,20 @@
10
10
  "types": "./dist/main.d.ts",
11
11
  "scripts": {
12
12
  "tests": "bun run lint && bun test",
13
- "prettier": "prettier --write ./",
14
13
  "build": "tsup",
14
+ "prettier": "prettier --write ./",
15
15
  "lint": "eslint ./"
16
16
  },
17
17
  "dependencies": {
18
- "@sinclair/typebox": "^0.34.38",
18
+ "@sinclair/typebox": "^0.34.41",
19
19
  "@types/lodash": "^4.17.20",
20
20
  "lodash": "^4.17.21",
21
- "yup": "^1.6.1"
21
+ "yup": "^1.7.0"
22
22
  },
23
23
  "devDependencies": {
24
- "jiti": "^2.5.1",
24
+ "neatlint": "^1.1.4",
25
25
  "prettier": "^3.6.2",
26
- "tsup": "^8.5.0",
27
- "typescript": "^5.8.3",
28
- "typescript-eslint": "^8.38.0"
26
+ "tsup": "^8.5.0"
29
27
  },
30
28
  "repository": {
31
29
  "type": "git",
@@ -1,18 +0,0 @@
1
- import { AnyObject } from './types/AnyObject.type.mjs';
2
- import { JSONSchema } from './types/JSONSchema.type.mjs';
3
- import { Schema } from './types/Schema.type.mjs';
4
- import { ValidateOptions } from './types/ValidateOptions.type.mjs';
5
- import { ValidationError } from './types/ValidationError.type.mjs';
6
- import { YuppiOptions } from './types/YuppiOptions.type.mjs';
7
-
8
- declare const Types_barrel_AnyObject: typeof AnyObject;
9
- declare const Types_barrel_JSONSchema: typeof JSONSchema;
10
- declare const Types_barrel_Schema: typeof Schema;
11
- declare const Types_barrel_ValidateOptions: typeof ValidateOptions;
12
- declare const Types_barrel_ValidationError: typeof ValidationError;
13
- declare const Types_barrel_YuppiOptions: typeof YuppiOptions;
14
- declare namespace Types_barrel {
15
- export { Types_barrel_AnyObject as AnyObject, Types_barrel_JSONSchema as JSONSchema, Types_barrel_Schema as Schema, Types_barrel_ValidateOptions as ValidateOptions, Types_barrel_ValidationError as ValidationError, Types_barrel_YuppiOptions as YuppiOptions };
16
- }
17
-
18
- export { Types_barrel as T };
@@ -1,18 +0,0 @@
1
- import { AnyObject } from './types/AnyObject.type.js';
2
- import { JSONSchema } from './types/JSONSchema.type.js';
3
- import { Schema } from './types/Schema.type.js';
4
- import { ValidateOptions } from './types/ValidateOptions.type.js';
5
- import { ValidationError } from './types/ValidationError.type.js';
6
- import { YuppiOptions } from './types/YuppiOptions.type.js';
7
-
8
- declare const Types_barrel_AnyObject: typeof AnyObject;
9
- declare const Types_barrel_JSONSchema: typeof JSONSchema;
10
- declare const Types_barrel_Schema: typeof Schema;
11
- declare const Types_barrel_ValidateOptions: typeof ValidateOptions;
12
- declare const Types_barrel_ValidationError: typeof ValidationError;
13
- declare const Types_barrel_YuppiOptions: typeof YuppiOptions;
14
- declare namespace Types_barrel {
15
- export { Types_barrel_AnyObject as AnyObject, Types_barrel_JSONSchema as JSONSchema, Types_barrel_Schema as Schema, Types_barrel_ValidateOptions as ValidateOptions, Types_barrel_ValidationError as ValidationError, Types_barrel_YuppiOptions as YuppiOptions };
16
- }
17
-
18
- export { Types_barrel as T };