reliant-type 1.0.0 → 2.1.3

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 (58) hide show
  1. package/dist/cjs/core/schema/extensions/mods/typescript-generator.js +45 -16
  2. package/dist/cjs/core/schema/extensions/mods/typescript-generator.js.map +1 -1
  3. package/dist/cjs/core/schema/mode/interfaces/Interface.js +0 -12
  4. package/dist/cjs/core/schema/mode/interfaces/Interface.js.map +1 -1
  5. package/dist/cjs/core/schema/mode/interfaces/InterfaceSchema.js +34 -3
  6. package/dist/cjs/core/schema/mode/interfaces/InterfaceSchema.js.map +1 -1
  7. package/dist/cjs/core/schema/mode/interfaces/errors/SchemaValidationError.js +16 -0
  8. package/dist/cjs/core/schema/mode/interfaces/errors/SchemaValidationError.js.map +1 -0
  9. package/dist/cjs/core/schema/mode/interfaces/precompilation/FieldPrecompilers.js +119 -0
  10. package/dist/cjs/core/schema/mode/interfaces/precompilation/FieldPrecompilers.js.map +1 -1
  11. package/dist/cjs/core/schema/mode/interfaces/precompilation/SchemaPrecompiler.js +49 -0
  12. package/dist/cjs/core/schema/mode/interfaces/precompilation/SchemaPrecompiler.js.map +1 -1
  13. package/dist/cjs/core/schema/mode/interfaces/typescript/TypeInference.js.map +1 -1
  14. package/dist/cjs/core/schema/mode/interfaces/validators/ConstraintParser.js +6 -0
  15. package/dist/cjs/core/schema/mode/interfaces/validators/ConstraintParser.js.map +1 -1
  16. package/dist/cjs/core/schema/mode/interfaces/validators/TypeGuards.js +7 -0
  17. package/dist/cjs/core/schema/mode/interfaces/validators/TypeGuards.js.map +1 -1
  18. package/dist/cjs/core/utils/Mod.js +33 -4
  19. package/dist/cjs/core/utils/Mod.js.map +1 -1
  20. package/dist/cjs/index.js +74 -1
  21. package/dist/cjs/index.js.map +1 -1
  22. package/dist/esm/core/schema/extensions/mods/typescript-generator.js +45 -16
  23. package/dist/esm/core/schema/extensions/mods/typescript-generator.js.map +1 -1
  24. package/dist/esm/core/schema/mode/interfaces/Interface.js +1 -12
  25. package/dist/esm/core/schema/mode/interfaces/Interface.js.map +1 -1
  26. package/dist/esm/core/schema/mode/interfaces/InterfaceSchema.js +32 -1
  27. package/dist/esm/core/schema/mode/interfaces/InterfaceSchema.js.map +1 -1
  28. package/dist/esm/core/schema/mode/interfaces/errors/SchemaValidationError.js +14 -0
  29. package/dist/esm/core/schema/mode/interfaces/errors/SchemaValidationError.js.map +1 -0
  30. package/dist/esm/core/schema/mode/interfaces/precompilation/FieldPrecompilers.js +119 -0
  31. package/dist/esm/core/schema/mode/interfaces/precompilation/FieldPrecompilers.js.map +1 -1
  32. package/dist/esm/core/schema/mode/interfaces/precompilation/SchemaPrecompiler.js +49 -0
  33. package/dist/esm/core/schema/mode/interfaces/precompilation/SchemaPrecompiler.js.map +1 -1
  34. package/dist/esm/core/schema/mode/interfaces/typescript/TypeInference.js.map +1 -1
  35. package/dist/esm/core/schema/mode/interfaces/validators/ConstraintParser.js +6 -0
  36. package/dist/esm/core/schema/mode/interfaces/validators/ConstraintParser.js.map +1 -1
  37. package/dist/esm/core/schema/mode/interfaces/validators/TypeGuards.js +7 -0
  38. package/dist/esm/core/schema/mode/interfaces/validators/TypeGuards.js.map +1 -1
  39. package/dist/esm/core/utils/Mod.js +33 -4
  40. package/dist/esm/core/utils/Mod.js.map +1 -1
  41. package/dist/esm/index.js +78 -1
  42. package/dist/esm/index.js.map +1 -1
  43. package/dist/schema.d.ts +44 -3
  44. package/docs/FUNCTION-TYPES.md +120 -0
  45. package/package.json +4 -4
  46. package/src/core/schema/extensions/components/AutoDocumentation/Docs.ts +1 -1
  47. package/src/core/schema/extensions/mods/typescript-generator.ts +342 -295
  48. package/src/core/schema/mode/interfaces/Interface.ts +1 -13
  49. package/src/core/schema/mode/interfaces/InterfaceSchema.ts +41 -1
  50. package/src/core/schema/mode/interfaces/errors/SchemaValidationError.ts +13 -0
  51. package/src/core/schema/mode/interfaces/precompilation/FieldPrecompilers.ts +146 -0
  52. package/src/core/schema/mode/interfaces/precompilation/SchemaPrecompiler.ts +79 -0
  53. package/src/core/schema/mode/interfaces/typescript/TypeInference.ts +67 -16
  54. package/src/core/schema/mode/interfaces/validators/ConstraintParser.ts +8 -0
  55. package/src/core/schema/mode/interfaces/validators/TypeGuards.ts +19 -5
  56. package/src/core/types/SchemaValidator.type.ts +4 -0
  57. package/src/core/utils/Mod.ts +35 -4
  58. package/src/index.ts +9 -0
@@ -0,0 +1,120 @@
1
+ # Function Types
2
+
3
+ ReliantType introduces powerful runtime and compile-time validation for function signatures using the `fn` type. This feature allows you to define strict contracts for functions within your schema, ensuring that both arguments and return values adhere to specified types during execution.
4
+
5
+ ## Overview
6
+
7
+ The `fn` type enables you to describe function signatures using a syntax similar to TypeScript arrow functions. These definitions are used to:
8
+
9
+ 1. **Validate at Runtime**: Wraps the function to strictly enforce argument types, argument count, and return types every time the function is called.
10
+ 2. **Infer TypeScript Types**: Automatically generates accurate TypeScript definitions for your IDE, including argument names, types, and return values.
11
+
12
+ ## Syntax
13
+
14
+ The general syntax follows the pattern:
15
+
16
+ ```typescript
17
+ "fn((argument_list) => return_type)";
18
+ ```
19
+
20
+ ### Components
21
+
22
+ - **`fn(...)`**: The wrapper indicating a function type.
23
+ - **`argument_list`**: A comma-separated list of arguments in the format `name: type`.
24
+ - **`return_type`**: The expected return type of the function.
25
+
26
+ ## Features
27
+
28
+ ### 1. Basic Function Signatures
29
+
30
+ Define functions with specific argument types and return types.
31
+
32
+ ```typescript
33
+ const schema = Interface({
34
+ // Function taking a string and returning a boolean
35
+ isValid: "fn((input: string) => boolean)",
36
+
37
+ // Function taking two numbers and returning a number
38
+ add: "fn((a: number, b: number) => number)",
39
+ });
40
+ ```
41
+
42
+ ### 2. Optional Arguments
43
+
44
+ Mark arguments as optional using the `?` suffix on the argument name or type.
45
+
46
+ ```typescript
47
+ const schema = Interface({
48
+ // 'context' is optional
49
+ log: "fn((message: string, context?: object) => void)",
50
+ });
51
+ ```
52
+
53
+ ### 3. Rest Parameters (Spread Operator)
54
+
55
+ Support for variadic functions using the spread operator (`...`). This is fully supported in both runtime validation and TypeScript inference.
56
+
57
+ ```typescript
58
+ const schema = Interface({
59
+ // Accepts any number of numeric arguments
60
+ sum: "fn((...numbers: number[]) => number)",
61
+ });
62
+ ```
63
+
64
+ ### 4. The `TYPE` Placeholder
65
+
66
+ Use `TYPE` as a placeholder for `any`. This is useful when you want to enforce the presence of an argument or a return value without strictly validating its content, or when the type is too complex to describe in a string.
67
+
68
+ ```typescript
69
+ const schema = Interface({
70
+ // Accepts a generic input and returns a generic output
71
+ transform: "fn((input: TYPE) => TYPE)",
72
+ });
73
+ ```
74
+
75
+ ### 5. Void Return Type
76
+
77
+ Use `void` to indicate that the function should not return a value (or that the return value should be ignored).
78
+
79
+ ```typescript
80
+ const schema = Interface({
81
+ callback: "fn(() => void)",
82
+ });
83
+ ```
84
+
85
+ ## Runtime Validation Behavior
86
+
87
+ ReliantType enforces a **Strict Contract** policy for functions. When a function defined in a schema is accessed, it is wrapped in a validation proxy.
88
+
89
+ - **Argument Count**: Throws an error if required arguments are missing.
90
+ - **Argument Types**: Throws an error if an argument does not match its defined type.
91
+ - **Return Type**: Throws an error if the function returns a value that does not match the defined return type.
92
+
93
+ ### Example of Validation Failure
94
+
95
+ ```typescript
96
+ const config = {
97
+ // Incorrect implementation: returns a string instead of a number
98
+ calculator: (a, b) => "result",
99
+ };
100
+
101
+ const schema = Interface({
102
+ calculator: "fn((a: number, b: number) => number)",
103
+ });
104
+
105
+ const result = schema.parse(config);
106
+
107
+ // This call will THROW an Error:
108
+ // "[ReliantType] Return value validation failed... Expected number, got string"
109
+ result.calculator(10, 20);
110
+ ```
111
+
112
+ ## TypeScript Integration
113
+
114
+ The `fn` syntax is fully compatible with ReliantType's type inference system.
115
+
116
+ - `fn((a: string) => number)` infers to `(a: string) => number`
117
+ - `fn((...args: string[]) => void)` infers to `(...args: string[]) => void`
118
+ - `fn((opt?: string) => boolean)` infers to `(opt?: string | undefined) => boolean`
119
+
120
+ This ensures that your IDE provides correct autocompletion and type checking for implementation code.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "reliant-type",
3
- "version": "1.0.0",
4
- "description": "ReliantType is a modern TypeScript validation library designed around familiar interface syntax and powerful conditional validation. Experience schema validation that feels natural to TypeScript developers while unlocking advanced runtime validation capabilities.",
3
+ "version": "2.1.3",
4
+ "description": "A modern TypeScript validation library designed around familiar interface syntax and powerful conditional validation. Experience schema validation that feels natural to TypeScript developers while unlocking advanced runtime validation capabilities.",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
7
7
  "types": "dist/schema.d.ts",
@@ -178,7 +178,7 @@
178
178
  "robust",
179
179
  "scalable"
180
180
  ],
181
- "author": "Nehonix Team <rlt.support@nehonix.space> (https://nehonix.space)",
181
+ "author": "Nehonix Team <support@nehonix.space> (https://nehonix.space)",
182
182
  "license": "MIT License",
183
183
  "repository": {
184
184
  "type": "git",
@@ -203,7 +203,7 @@
203
203
  "jest": "^29.7.0",
204
204
  "prettier": "^3.0.3",
205
205
  "rimraf": "^6.0.1",
206
- "rollup": "^3.29.5",
206
+ "rollup": "^3.29.2",
207
207
  "rollup-plugin-dts": "^6.0.2",
208
208
  "ts-jest": "^29.1.1",
209
209
  "typescript": "^5.8.3"
@@ -70,7 +70,7 @@ export const Docs = {
70
70
  * // name: string;
71
71
  * // age?: number;
72
72
  * // role: "user" | "admin" | "moderator";
73
- * // }
73
+ * // }
74
74
  * ```
75
75
  */
76
76
  typescript(schema: SchemaInterface, options: TSOptions = {}): string {