yuppi 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +9 -0
- package/README.md +368 -0
- package/dist/Patterns.barrel-KU6hZBXs.d.mts +14 -0
- package/dist/Patterns.barrel-UKU2yPZ6.d.ts +14 -0
- package/dist/Types.barrel--XGUUjIp.d.mts +18 -0
- package/dist/Types.barrel-wzQq8e9v.d.ts +18 -0
- package/dist/Yuppi.class.d.mts +17 -0
- package/dist/Yuppi.class.d.ts +17 -0
- package/dist/Yuppi.class.js +212 -0
- package/dist/Yuppi.class.mjs +177 -0
- package/dist/barrels/Patterns.barrel.d.mts +5 -0
- package/dist/barrels/Patterns.barrel.d.ts +5 -0
- package/dist/barrels/Patterns.barrel.js +47 -0
- package/dist/barrels/Patterns.barrel.mjs +17 -0
- package/dist/barrels/Types.barrel.d.mts +8 -0
- package/dist/barrels/Types.barrel.d.ts +8 -0
- package/dist/barrels/Types.barrel.js +18 -0
- package/dist/barrels/Types.barrel.mjs +0 -0
- package/dist/defaults/YuppiOptions.default.d.mts +7 -0
- package/dist/defaults/YuppiOptions.default.d.ts +7 -0
- package/dist/defaults/YuppiOptions.default.js +71 -0
- package/dist/defaults/YuppiOptions.default.mjs +46 -0
- package/dist/main.d.mts +15 -0
- package/dist/main.d.ts +15 -0
- package/dist/main.js +237 -0
- package/dist/main.mjs +205 -0
- package/dist/patterns/Email.pattern.d.mts +5 -0
- package/dist/patterns/Email.pattern.d.ts +5 -0
- package/dist/patterns/Email.pattern.js +30 -0
- package/dist/patterns/Email.pattern.mjs +5 -0
- package/dist/patterns/PhoneNumber.pattern.d.mts +5 -0
- package/dist/patterns/PhoneNumber.pattern.d.ts +5 -0
- package/dist/patterns/PhoneNumber.pattern.js +30 -0
- package/dist/patterns/PhoneNumber.pattern.mjs +5 -0
- package/dist/patterns/URL.pattern.d.mts +5 -0
- package/dist/patterns/URL.pattern.d.ts +5 -0
- package/dist/patterns/URL.pattern.js +30 -0
- package/dist/patterns/URL.pattern.mjs +5 -0
- package/dist/patterns/Username.pattern.d.mts +5 -0
- package/dist/patterns/Username.pattern.d.ts +5 -0
- package/dist/patterns/Username.pattern.js +30 -0
- package/dist/patterns/Username.pattern.mjs +5 -0
- package/dist/types/AnyObject.type.d.mts +5 -0
- package/dist/types/AnyObject.type.d.ts +5 -0
- package/dist/types/AnyObject.type.js +18 -0
- package/dist/types/AnyObject.type.mjs +0 -0
- package/dist/types/JSONSchema.type.d.mts +5 -0
- package/dist/types/JSONSchema.type.d.ts +5 -0
- package/dist/types/JSONSchema.type.js +18 -0
- package/dist/types/JSONSchema.type.mjs +0 -0
- package/dist/types/Schema.type.d.mts +45 -0
- package/dist/types/Schema.type.d.ts +45 -0
- package/dist/types/Schema.type.js +18 -0
- package/dist/types/Schema.type.mjs +0 -0
- package/dist/types/ValidateOptions.type.d.mts +5 -0
- package/dist/types/ValidateOptions.type.d.ts +5 -0
- package/dist/types/ValidateOptions.type.js +18 -0
- package/dist/types/ValidateOptions.type.mjs +0 -0
- package/dist/types/ValidationError.type.d.mts +5 -0
- package/dist/types/ValidationError.type.d.ts +5 -0
- package/dist/types/ValidationError.type.js +18 -0
- package/dist/types/ValidationError.type.mjs +0 -0
- package/dist/types/YuppiOptions.type.d.mts +44 -0
- package/dist/types/YuppiOptions.type.d.ts +44 -0
- package/dist/types/YuppiOptions.type.js +18 -0
- package/dist/types/YuppiOptions.type.mjs +0 -0
- package/dist/utils/ConvertToJSONSchema.util.d.mts +6 -0
- package/dist/utils/ConvertToJSONSchema.util.d.ts +6 -0
- package/dist/utils/ConvertToJSONSchema.util.js +64 -0
- package/dist/utils/ConvertToJSONSchema.util.mjs +39 -0
- package/dist/utils/ConvertToYup.util.d.mts +9 -0
- package/dist/utils/ConvertToYup.util.d.ts +9 -0
- package/dist/utils/ConvertToYup.util.js +110 -0
- package/dist/utils/ConvertToYup.util.mjs +75 -0
- package/package.json +42 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// src/utils/ConvertToYup.util.ts
|
|
2
|
+
import * as Yup from "yup";
|
|
3
|
+
var convertToYup = (schema, error_messages) => {
|
|
4
|
+
const build = (key, config) => {
|
|
5
|
+
let schema2;
|
|
6
|
+
if (config.type === "string") {
|
|
7
|
+
schema2 = Yup.string().typeError(({ path }) => (error_messages?.string?.type ?? "").split("{path}").join(path));
|
|
8
|
+
schema2 = schema2.trim();
|
|
9
|
+
if (config.min !== void 0)
|
|
10
|
+
schema2 = schema2.min(
|
|
11
|
+
config.min,
|
|
12
|
+
({ path, min }) => (error_messages?.string?.min ?? "").split("{path}").join(path).split("{min}").join(min.toString()).split("{plural_suffix}").join(min > 1 ? "s" : "")
|
|
13
|
+
);
|
|
14
|
+
if (config.max !== void 0)
|
|
15
|
+
schema2 = schema2.max(
|
|
16
|
+
config.max,
|
|
17
|
+
({ path, max }) => (error_messages?.string?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()).split("{plural_suffix}").join(max > 1 ? "s" : "")
|
|
18
|
+
);
|
|
19
|
+
if (config.lowercase === true) schema2 = schema2.lowercase();
|
|
20
|
+
if (config.uppercase === true) schema2 = schema2.uppercase();
|
|
21
|
+
} else if (config.type === "number") {
|
|
22
|
+
schema2 = Yup.number().typeError(({ path }) => (error_messages?.number?.type ?? "").split("{path}").join(path));
|
|
23
|
+
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()));
|
|
24
|
+
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()));
|
|
25
|
+
if (config.integer === true) schema2 = schema2.integer(({ path }) => (error_messages?.number?.integer ?? "").split("{path}").join(path));
|
|
26
|
+
if (config.positive === true) schema2 = schema2.positive(({ path }) => (error_messages?.number?.positive ?? "").split("{path}").join(path));
|
|
27
|
+
if (config.negative === true) schema2 = schema2.negative(({ path }) => (error_messages?.number?.negative ?? "").split("{path}").join(path));
|
|
28
|
+
} else if (config.type === "boolean") {
|
|
29
|
+
schema2 = Yup.boolean().typeError(({ path }) => (error_messages?.boolean?.type ?? "").split("{path}").join(path));
|
|
30
|
+
} else if (config.type === "date") {
|
|
31
|
+
schema2 = Yup.date().typeError(({ path }) => (error_messages?.date?.type ?? "").split("{path}").join(path));
|
|
32
|
+
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()));
|
|
33
|
+
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()));
|
|
34
|
+
} else if (config.type === "object") {
|
|
35
|
+
schema2 = Yup.object().typeError(({ path }) => (error_messages?.object?.type ?? "").split("{path}").join(path));
|
|
36
|
+
const nested_properties = {};
|
|
37
|
+
for (const [nested_key, nested_config] of Object.entries(config.properties)) nested_properties[nested_key] = build(nested_key, nested_config);
|
|
38
|
+
schema2 = schema2.shape(nested_properties);
|
|
39
|
+
} else if (config.type === "array") {
|
|
40
|
+
schema2 = Yup.array().typeError(({ path }) => (error_messages?.array?.type ?? "").split("{path}").join(path));
|
|
41
|
+
if (config.min !== void 0)
|
|
42
|
+
schema2 = schema2.min(
|
|
43
|
+
config.min,
|
|
44
|
+
({ path, min }) => (error_messages?.array?.min ?? "").split("{path}").join(path).split("{min}").join(min.toString()).split("{plural_suffix}").join(min > 1 ? "s" : "")
|
|
45
|
+
);
|
|
46
|
+
if (config.max !== void 0)
|
|
47
|
+
schema2 = schema2.max(
|
|
48
|
+
config.max,
|
|
49
|
+
({ path, max }) => (error_messages?.array?.max ?? "").split("{path}").join(path).split("{max}").join(max.toString()).split("{plural_suffix}").join(max > 1 ? "s" : "")
|
|
50
|
+
);
|
|
51
|
+
schema2 = schema2.of(build(key, config.items));
|
|
52
|
+
} else throw new Error(`Unsupported schema type for ${key}`);
|
|
53
|
+
schema2 = schema2.nullable();
|
|
54
|
+
if (config.pattern !== void 0 && schema2.matches !== void 0)
|
|
55
|
+
schema2 = schema2.matches(
|
|
56
|
+
new RegExp(config.pattern ?? "[\\s\\S]*"),
|
|
57
|
+
({ path }) => (error_messages?.base?.pattern ?? "").split("{path}").join(path).split("{pattern}").join(new RegExp(config.pattern ?? "[\\s\\S]*").source)
|
|
58
|
+
);
|
|
59
|
+
if (config.default !== void 0) schema2 = schema2.default(config.default);
|
|
60
|
+
if (!config.nullable && config.default !== null)
|
|
61
|
+
schema2 = schema2.test(
|
|
62
|
+
"nullable",
|
|
63
|
+
({ path }) => (error_messages?.base?.nullable ?? "").split("{path}").join(path),
|
|
64
|
+
(value) => value !== null
|
|
65
|
+
);
|
|
66
|
+
if (config.required) schema2 = schema2.required(({ path }) => (error_messages?.base?.required ?? "").split("{path}").join(path));
|
|
67
|
+
return schema2;
|
|
68
|
+
};
|
|
69
|
+
const shape = {};
|
|
70
|
+
for (const [key, config] of Object.entries(schema)) shape[key] = build(key, config);
|
|
71
|
+
return Yup.object().shape(shape);
|
|
72
|
+
};
|
|
73
|
+
export {
|
|
74
|
+
convertToYup
|
|
75
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "yuppi",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Schemas that can be converted to Yup and JSON Schema.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"homepage": "https://github.com/keift/yuppi",
|
|
7
|
+
"bugs": "https://github.com/keift/yuppi/issues",
|
|
8
|
+
"main": "./dist/main.js",
|
|
9
|
+
"module": "./dist/main.mjs",
|
|
10
|
+
"types": "./dist/main.d.ts",
|
|
11
|
+
"scripts": {
|
|
12
|
+
"dev": "bun run --watch ./examples/yuppi.example.ts",
|
|
13
|
+
"tests": "bun run lint && npm run ./tests/ConvertToJSONSchema.test.ts && npm run ./tests/Validate.test.ts && npm run ./tests/ValidationError.test.ts",
|
|
14
|
+
"prettier": "prettier --write ./",
|
|
15
|
+
"build": "tsup",
|
|
16
|
+
"lint": "eslint ./"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@sinclair/typebox": "^0.34.35",
|
|
20
|
+
"@types/lodash": "^4.17.17",
|
|
21
|
+
"lodash": "^4.17.21",
|
|
22
|
+
"yup": "^1.6.1"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"jiti": "^2.4.2",
|
|
26
|
+
"prettier": "^3.5.3",
|
|
27
|
+
"tsup": "^8.5.0",
|
|
28
|
+
"typescript": "^5.8.3",
|
|
29
|
+
"typescript-eslint": "^8.34.1"
|
|
30
|
+
},
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "https://github.com/keift/yuppi"
|
|
34
|
+
},
|
|
35
|
+
"keywords": [
|
|
36
|
+
"validate",
|
|
37
|
+
"typecheck",
|
|
38
|
+
"yup",
|
|
39
|
+
"json-schema",
|
|
40
|
+
"openapi"
|
|
41
|
+
]
|
|
42
|
+
}
|