0
# Interface and Method Validation
1
2
Specialized validation for interface properties and method calls, including argument and return value validation.
3
4
## Capabilities
5
6
### Property Validation
7
8
Validate individual properties of interface types.
9
10
```typescript { .api }
11
/**
12
* If this checker is for an interface, returns a Checker for the type required
13
* for the given property of this interface
14
* @param prop - Property name
15
* @returns Checker for the property type
16
* @throws Error if property doesn't exist
17
*/
18
getProp(prop: string): Checker;
19
```
20
21
**Usage Examples:**
22
23
```typescript
24
import { createCheckers } from "ts-interface-checker";
25
import userTypes from "./user-ti";
26
27
const { User } = createCheckers(userTypes);
28
29
// Get checker for specific property
30
const nameChecker = User.getProp("name");
31
const ageChecker = User.getProp("age");
32
33
// Validate individual properties
34
nameChecker.check("Alice"); // OK
35
nameChecker.check(123); // Throws: "value is not a string"
36
37
ageChecker.check(30); // OK
38
ageChecker.check("30"); // Throws: "value is not a number"
39
40
// Use property checkers for partial validation
41
const userUpdate = { name: "Bob" };
42
nameChecker.check(userUpdate.name); // Validate just the name
43
```
44
45
### Method Argument Validation
46
47
Validate method call arguments for interface methods.
48
49
```typescript { .api }
50
/**
51
* If this checker is for an interface, returns a Checker for the argument-list
52
* required to call the given method of this interface
53
* @param methodName - Name of the method
54
* @returns Checker for the method's argument list
55
* @throws Error if method doesn't exist or is not a function
56
*/
57
methodArgs(methodName: string): Checker;
58
```
59
60
**Usage Examples:**
61
62
```typescript
63
// For interface with methods:
64
// interface Calculator {
65
// add(a: number, b: number): number;
66
// divide(dividend: number, divisor: number): number;
67
// }
68
69
const { Calculator } = createCheckers(calculatorTypes);
70
71
// Validate method arguments
72
const addArgsChecker = Calculator.methodArgs("add");
73
const divideArgsChecker = Calculator.methodArgs("divide");
74
75
// Check arguments as arrays
76
addArgsChecker.check([10, 20]); // OK
77
addArgsChecker.check([10]); // Throws: argument missing
78
addArgsChecker.check(["10", 20]); // Throws: first argument not a number
79
80
divideArgsChecker.check([100, 5]); // OK
81
divideArgsChecker.check([100, 0]); // OK (type check passes, divide by zero is runtime issue)
82
divideArgsChecker.check([100]); // Throws: divisor missing
83
84
// Validate actual function calls
85
function safeAdd(calc: any, a: number, b: number) {
86
Calculator.methodArgs("add").check([a, b]);
87
return calc.add(a, b);
88
}
89
```
90
91
### Method Return Value Validation
92
93
Validate method return values for interface methods.
94
95
```typescript { .api }
96
/**
97
* If this checker is for an interface, returns a Checker for the return value
98
* of the given method of this interface
99
* @param methodName - Name of the method
100
* @returns Checker for the method's return type
101
* @throws Error if method doesn't exist or is not a function
102
*/
103
methodResult(methodName: string): Checker;
104
```
105
106
**Usage Examples:**
107
108
```typescript
109
const { Calculator } = createCheckers(calculatorTypes);
110
111
// Validate method return values
112
const addResultChecker = Calculator.methodResult("add");
113
const divideResultChecker = Calculator.methodResult("divide");
114
115
// Check return values
116
addResultChecker.check(30); // OK
117
addResultChecker.check("30"); // Throws: result not a number
118
119
// Validate function implementations
120
function validateCalculatorImplementation(calc: any) {
121
const result1 = calc.add(10, 20);
122
addResultChecker.check(result1); // Ensure return type is correct
123
124
const result2 = calc.divide(100, 5);
125
divideResultChecker.check(result2); // Ensure return type is correct
126
}
127
```
128
129
### Function Type Validation
130
131
For function types (not interface methods), validate arguments and return values.
132
133
```typescript { .api }
134
/**
135
* If this checker is for a function, returns a Checker for its argument-list
136
* @returns Checker for the function's arguments
137
* @throws Error if not applied to a function type
138
*/
139
getArgs(): Checker;
140
141
/**
142
* If this checker is for a function, returns a Checker for its result
143
* @returns Checker for the function's return type
144
* @throws Error if not applied to a function type
145
*/
146
getResult(): Checker;
147
```
148
149
**Usage Examples:**
150
151
```typescript
152
// For function type:
153
// type MathFunction = (x: number, y: number) => number;
154
155
const { MathFunction } = createCheckers(mathTypes);
156
157
// Validate function arguments and results
158
const argsChecker = MathFunction.getArgs();
159
const resultChecker = MathFunction.getResult();
160
161
// Check function signature compliance
162
function validateMathFunction(fn: any) {
163
// Test with sample arguments
164
argsChecker.check([5, 10]); // Arguments are valid
165
166
const result = fn(5, 10);
167
resultChecker.check(result); // Result type is valid
168
}
169
170
// Usage with actual functions
171
const multiply = (x: number, y: number) => x * y;
172
const invalidFn = (x: string, y: string) => x + y;
173
174
validateMathFunction(multiply); // OK
175
// validateMathFunction(invalidFn); // Would fail arg validation
176
```
177
178
### Method Validation Patterns
179
180
Common patterns for validating methods and functions in practice.
181
182
**Usage Examples:**
183
184
```typescript
185
// Interface with optional parameters:
186
// interface DataProcessor {
187
// process(data: string[], options?: ProcessOptions): ProcessResult;
188
// }
189
190
const { DataProcessor } = createCheckers(dataProcessorTypes);
191
192
// Validate method with optional parameters
193
const processArgsChecker = DataProcessor.methodArgs("process");
194
195
processArgsChecker.check([["data1", "data2"]]); // OK - options omitted
196
processArgsChecker.check([["data1", "data2"], { sort: true }]); // OK - options provided
197
processArgsChecker.check([]); // Throws: data parameter missing
198
199
// Validate complex return types
200
const processResultChecker = DataProcessor.methodResult("process");
201
202
const result = {
203
processedData: ["data1", "data2"],
204
metadata: { count: 2, timestamp: Date.now() }
205
};
206
207
processResultChecker.check(result); // Validates entire result structure
208
```