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.
Files changed (75) hide show
  1. package/LICENSE.md +9 -0
  2. package/README.md +368 -0
  3. package/dist/Patterns.barrel-KU6hZBXs.d.mts +14 -0
  4. package/dist/Patterns.barrel-UKU2yPZ6.d.ts +14 -0
  5. package/dist/Types.barrel--XGUUjIp.d.mts +18 -0
  6. package/dist/Types.barrel-wzQq8e9v.d.ts +18 -0
  7. package/dist/Yuppi.class.d.mts +17 -0
  8. package/dist/Yuppi.class.d.ts +17 -0
  9. package/dist/Yuppi.class.js +212 -0
  10. package/dist/Yuppi.class.mjs +177 -0
  11. package/dist/barrels/Patterns.barrel.d.mts +5 -0
  12. package/dist/barrels/Patterns.barrel.d.ts +5 -0
  13. package/dist/barrels/Patterns.barrel.js +47 -0
  14. package/dist/barrels/Patterns.barrel.mjs +17 -0
  15. package/dist/barrels/Types.barrel.d.mts +8 -0
  16. package/dist/barrels/Types.barrel.d.ts +8 -0
  17. package/dist/barrels/Types.barrel.js +18 -0
  18. package/dist/barrels/Types.barrel.mjs +0 -0
  19. package/dist/defaults/YuppiOptions.default.d.mts +7 -0
  20. package/dist/defaults/YuppiOptions.default.d.ts +7 -0
  21. package/dist/defaults/YuppiOptions.default.js +71 -0
  22. package/dist/defaults/YuppiOptions.default.mjs +46 -0
  23. package/dist/main.d.mts +15 -0
  24. package/dist/main.d.ts +15 -0
  25. package/dist/main.js +237 -0
  26. package/dist/main.mjs +205 -0
  27. package/dist/patterns/Email.pattern.d.mts +5 -0
  28. package/dist/patterns/Email.pattern.d.ts +5 -0
  29. package/dist/patterns/Email.pattern.js +30 -0
  30. package/dist/patterns/Email.pattern.mjs +5 -0
  31. package/dist/patterns/PhoneNumber.pattern.d.mts +5 -0
  32. package/dist/patterns/PhoneNumber.pattern.d.ts +5 -0
  33. package/dist/patterns/PhoneNumber.pattern.js +30 -0
  34. package/dist/patterns/PhoneNumber.pattern.mjs +5 -0
  35. package/dist/patterns/URL.pattern.d.mts +5 -0
  36. package/dist/patterns/URL.pattern.d.ts +5 -0
  37. package/dist/patterns/URL.pattern.js +30 -0
  38. package/dist/patterns/URL.pattern.mjs +5 -0
  39. package/dist/patterns/Username.pattern.d.mts +5 -0
  40. package/dist/patterns/Username.pattern.d.ts +5 -0
  41. package/dist/patterns/Username.pattern.js +30 -0
  42. package/dist/patterns/Username.pattern.mjs +5 -0
  43. package/dist/types/AnyObject.type.d.mts +5 -0
  44. package/dist/types/AnyObject.type.d.ts +5 -0
  45. package/dist/types/AnyObject.type.js +18 -0
  46. package/dist/types/AnyObject.type.mjs +0 -0
  47. package/dist/types/JSONSchema.type.d.mts +5 -0
  48. package/dist/types/JSONSchema.type.d.ts +5 -0
  49. package/dist/types/JSONSchema.type.js +18 -0
  50. package/dist/types/JSONSchema.type.mjs +0 -0
  51. package/dist/types/Schema.type.d.mts +45 -0
  52. package/dist/types/Schema.type.d.ts +45 -0
  53. package/dist/types/Schema.type.js +18 -0
  54. package/dist/types/Schema.type.mjs +0 -0
  55. package/dist/types/ValidateOptions.type.d.mts +5 -0
  56. package/dist/types/ValidateOptions.type.d.ts +5 -0
  57. package/dist/types/ValidateOptions.type.js +18 -0
  58. package/dist/types/ValidateOptions.type.mjs +0 -0
  59. package/dist/types/ValidationError.type.d.mts +5 -0
  60. package/dist/types/ValidationError.type.d.ts +5 -0
  61. package/dist/types/ValidationError.type.js +18 -0
  62. package/dist/types/ValidationError.type.mjs +0 -0
  63. package/dist/types/YuppiOptions.type.d.mts +44 -0
  64. package/dist/types/YuppiOptions.type.d.ts +44 -0
  65. package/dist/types/YuppiOptions.type.js +18 -0
  66. package/dist/types/YuppiOptions.type.mjs +0 -0
  67. package/dist/utils/ConvertToJSONSchema.util.d.mts +6 -0
  68. package/dist/utils/ConvertToJSONSchema.util.d.ts +6 -0
  69. package/dist/utils/ConvertToJSONSchema.util.js +64 -0
  70. package/dist/utils/ConvertToJSONSchema.util.mjs +39 -0
  71. package/dist/utils/ConvertToYup.util.d.mts +9 -0
  72. package/dist/utils/ConvertToYup.util.d.ts +9 -0
  73. package/dist/utils/ConvertToYup.util.js +110 -0
  74. package/dist/utils/ConvertToYup.util.mjs +75 -0
  75. 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
+ }