@fishka/express 0.9.14 → 0.9.15
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/README.md +87 -39
- package/dist/cjs/api.types.d.ts +8 -21
- package/dist/cjs/api.types.js +1 -21
- package/dist/cjs/api.types.js.map +1 -1
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/route-table.d.ts +14 -6
- package/dist/cjs/route-table.js +3 -0
- package/dist/cjs/route-table.js.map +1 -1
- package/dist/cjs/router.d.ts +18 -26
- package/dist/cjs/router.js +33 -53
- package/dist/cjs/router.js.map +1 -1
- package/dist/cjs/utils/type-validators.d.ts +58 -0
- package/dist/cjs/utils/type-validators.js +122 -0
- package/dist/cjs/utils/type-validators.js.map +1 -0
- package/dist/esm/api.types.d.ts +8 -21
- package/dist/esm/api.types.js +0 -18
- package/dist/esm/api.types.js.map +1 -1
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/route-table.d.ts +14 -6
- package/dist/esm/route-table.js +3 -0
- package/dist/esm/route-table.js.map +1 -1
- package/dist/esm/router.d.ts +18 -26
- package/dist/esm/router.js +35 -55
- package/dist/esm/router.js.map +1 -1
- package/dist/esm/utils/type-validators.d.ts +58 -0
- package/dist/esm/utils/type-validators.js +102 -0
- package/dist/esm/utils/type-validators.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* URL parameter validators for Express path/query params.
|
|
3
|
+
* All params are strings from Express, so validation starts with string.
|
|
4
|
+
*/
|
|
5
|
+
import { TypeValidator } from '../api.types';
|
|
6
|
+
/** Makes validator optional - returns undefined if value missing */
|
|
7
|
+
export declare function optional<T>(validator: TypeValidator<T>): TypeValidator<T | undefined>;
|
|
8
|
+
/** Operator that transforms string to T */
|
|
9
|
+
export type ParamOperator<T> = (value: string) => T;
|
|
10
|
+
/** Operator that transforms T to R */
|
|
11
|
+
export type Operator<T, R = T> = (value: T) => R;
|
|
12
|
+
/**
|
|
13
|
+
* Creates a URL parameter validator. Input is always string (from Express).
|
|
14
|
+
* Operators are applied in sequence to validate/transform the value.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* param() // string
|
|
18
|
+
* param(toInt) // number
|
|
19
|
+
* param(toInt, min(1)) // number >= 1
|
|
20
|
+
* param(minLength(3)) // string with min length
|
|
21
|
+
* param(trim, lowercase) // trimmed lowercase string
|
|
22
|
+
*/
|
|
23
|
+
export declare function param(): TypeValidator<string>;
|
|
24
|
+
export declare function param<A>(op1: ParamOperator<A>): TypeValidator<A>;
|
|
25
|
+
export declare function param<A, B>(op1: ParamOperator<A>, op2: Operator<A, B>): TypeValidator<B>;
|
|
26
|
+
export declare function param<A, B, C>(op1: ParamOperator<A>, op2: Operator<A, B>, op3: Operator<B, C>): TypeValidator<C>;
|
|
27
|
+
export declare function param<A, B, C, D>(op1: ParamOperator<A>, op2: Operator<A, B>, op3: Operator<B, C>, op4: Operator<C, D>): TypeValidator<D>;
|
|
28
|
+
export declare function param<A, B, C, D, E>(op1: ParamOperator<A>, op2: Operator<A, B>, op3: Operator<B, C>, op4: Operator<C, D>, op5: Operator<D, E>): TypeValidator<E>;
|
|
29
|
+
/** Parses string to integer */
|
|
30
|
+
export declare const toInt: (message?: string) => ParamOperator<number>;
|
|
31
|
+
/** Parses string to number */
|
|
32
|
+
export declare const toNumber: (message?: string) => ParamOperator<number>;
|
|
33
|
+
/** Parses string to boolean ('true'/'false') */
|
|
34
|
+
export declare const toBool: (message?: string) => ParamOperator<boolean>;
|
|
35
|
+
/** Validates value is one of allowed enum values */
|
|
36
|
+
export declare const oneOf: <T extends string>(...allowedValues: T[]) => ParamOperator<T>;
|
|
37
|
+
/** Requires minimum string length */
|
|
38
|
+
export declare const minLength: (n: number, message?: string) => ParamOperator<string>;
|
|
39
|
+
/** Requires maximum string length */
|
|
40
|
+
export declare const maxLength: (n: number, message?: string) => ParamOperator<string>;
|
|
41
|
+
/** Requires string to match regex */
|
|
42
|
+
export declare const matches: (regex: RegExp, message?: string) => ParamOperator<string>;
|
|
43
|
+
/** Trims whitespace from string */
|
|
44
|
+
export declare const trim: ParamOperator<string>;
|
|
45
|
+
/** Converts string to lowercase */
|
|
46
|
+
export declare const lowercase: ParamOperator<string>;
|
|
47
|
+
/** Converts string to uppercase */
|
|
48
|
+
export declare const uppercase: ParamOperator<string>;
|
|
49
|
+
/** Requires minimum value */
|
|
50
|
+
export declare const min: (n: number, message?: string) => Operator<number>;
|
|
51
|
+
/** Requires maximum value */
|
|
52
|
+
export declare const max: (n: number, message?: string) => Operator<number>;
|
|
53
|
+
/** Requires value to be in range [minVal, maxVal] */
|
|
54
|
+
export declare const range: (minVal: number, maxVal: number, message?: string) => Operator<number>;
|
|
55
|
+
/** Adds custom validation */
|
|
56
|
+
export declare const check: <T>(predicate: (value: T) => boolean, message: string) => Operator<T>;
|
|
57
|
+
/** Transforms the value */
|
|
58
|
+
export declare const map: <T, R>(fn: (value: T) => R) => Operator<T, R>;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* URL parameter validators for Express path/query params.
|
|
3
|
+
* All params are strings from Express, so validation starts with string.
|
|
4
|
+
*/
|
|
5
|
+
import { assertTruthy } from '@fishka/assertions';
|
|
6
|
+
/** Makes validator optional - returns undefined if value missing */
|
|
7
|
+
export function optional(validator) {
|
|
8
|
+
return (value) => {
|
|
9
|
+
if (value === undefined || value === null || value === '') {
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
return validator(value);
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export function param(...operators) {
|
|
16
|
+
return (value) => {
|
|
17
|
+
assertTruthy(typeof value === 'string', `Expected string, got ${typeof value}`);
|
|
18
|
+
let result = value;
|
|
19
|
+
for (const op of operators) {
|
|
20
|
+
result = op(result);
|
|
21
|
+
}
|
|
22
|
+
return result;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
// ============================================================================
|
|
26
|
+
// String → T operators (first in chain)
|
|
27
|
+
// ============================================================================
|
|
28
|
+
/** Parses string to integer */
|
|
29
|
+
export const toInt = (message) => (value) => {
|
|
30
|
+
const num = Number(value);
|
|
31
|
+
assertTruthy(Number.isInteger(num), message ?? `Expected integer, got '${value}'`);
|
|
32
|
+
return num;
|
|
33
|
+
};
|
|
34
|
+
/** Parses string to number */
|
|
35
|
+
export const toNumber = (message) => (value) => {
|
|
36
|
+
const num = Number(value);
|
|
37
|
+
assertTruthy(!isNaN(num), message ?? `Expected number, got '${value}'`);
|
|
38
|
+
return num;
|
|
39
|
+
};
|
|
40
|
+
/** Parses string to boolean ('true'/'false') */
|
|
41
|
+
export const toBool = (message) => (value) => {
|
|
42
|
+
assertTruthy(value === 'true' || value === 'false', message ?? `Expected 'true' or 'false', got '${value}'`);
|
|
43
|
+
return value === 'true';
|
|
44
|
+
};
|
|
45
|
+
/** Validates value is one of allowed enum values */
|
|
46
|
+
export const oneOf = (...allowedValues) => (value) => {
|
|
47
|
+
assertTruthy(allowedValues.includes(value), `Expected one of [${allowedValues.join(', ')}], got '${value}'`);
|
|
48
|
+
return value;
|
|
49
|
+
};
|
|
50
|
+
// ============================================================================
|
|
51
|
+
// String operators (string → string)
|
|
52
|
+
// ============================================================================
|
|
53
|
+
/** Requires minimum string length */
|
|
54
|
+
export const minLength = (n, message) => (value) => {
|
|
55
|
+
assertTruthy(value.length >= n, message ?? `Must be at least ${n} characters`);
|
|
56
|
+
return value;
|
|
57
|
+
};
|
|
58
|
+
/** Requires maximum string length */
|
|
59
|
+
export const maxLength = (n, message) => (value) => {
|
|
60
|
+
assertTruthy(value.length <= n, message ?? `Must be at most ${n} characters`);
|
|
61
|
+
return value;
|
|
62
|
+
};
|
|
63
|
+
/** Requires string to match regex */
|
|
64
|
+
export const matches = (regex, message) => (value) => {
|
|
65
|
+
assertTruthy(regex.test(value), message ?? `Must match pattern ${regex}`);
|
|
66
|
+
return value;
|
|
67
|
+
};
|
|
68
|
+
/** Trims whitespace from string */
|
|
69
|
+
export const trim = (value) => value.trim();
|
|
70
|
+
/** Converts string to lowercase */
|
|
71
|
+
export const lowercase = (value) => value.toLowerCase();
|
|
72
|
+
/** Converts string to uppercase */
|
|
73
|
+
export const uppercase = (value) => value.toUpperCase();
|
|
74
|
+
// ============================================================================
|
|
75
|
+
// Number operators (number → number)
|
|
76
|
+
// ============================================================================
|
|
77
|
+
/** Requires minimum value */
|
|
78
|
+
export const min = (n, message) => (value) => {
|
|
79
|
+
assertTruthy(value >= n, message ?? `Must be at least ${n}`);
|
|
80
|
+
return value;
|
|
81
|
+
};
|
|
82
|
+
/** Requires maximum value */
|
|
83
|
+
export const max = (n, message) => (value) => {
|
|
84
|
+
assertTruthy(value <= n, message ?? `Must be at most ${n}`);
|
|
85
|
+
return value;
|
|
86
|
+
};
|
|
87
|
+
/** Requires value to be in range [minVal, maxVal] */
|
|
88
|
+
export const range = (minVal, maxVal, message) => (value) => {
|
|
89
|
+
assertTruthy(value >= minVal && value <= maxVal, message ?? `Must be between ${minVal} and ${maxVal}`);
|
|
90
|
+
return value;
|
|
91
|
+
};
|
|
92
|
+
// ============================================================================
|
|
93
|
+
// Generic operators
|
|
94
|
+
// ============================================================================
|
|
95
|
+
/** Adds custom validation */
|
|
96
|
+
export const check = (predicate, message) => (value) => {
|
|
97
|
+
assertTruthy(predicate(value), message);
|
|
98
|
+
return value;
|
|
99
|
+
};
|
|
100
|
+
/** Transforms the value */
|
|
101
|
+
export const map = (fn) => (value) => fn(value);
|
|
102
|
+
//# sourceMappingURL=type-validators.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type-validators.js","sourceRoot":"","sources":["../../../src/utils/type-validators.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CA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