payload-guard-filter 1.5.0 → 1.6.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/README.md +15 -0
- package/dist/core/compiler.js +3 -1
- package/dist/core/filter.js +1 -0
- package/dist/core/types.d.ts +3 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -113,6 +113,21 @@ Creates a string builder with chained constraints:
|
|
|
113
113
|
- `.transform(fn)` — Custom transformation function
|
|
114
114
|
- `.validate(fn)` — Custom validation function (return `false` to fail)
|
|
115
115
|
|
|
116
|
+
### Field Aliasing & Mapping (v1.6+)
|
|
117
|
+
|
|
118
|
+
Rename fields from your database to match your frontend API:
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
const userShape = guard.shape({
|
|
122
|
+
id: guard.string().from('_id'), // Map DB _id to id
|
|
123
|
+
workTime: guard.number().from('totalWorkTimeMs').transform(v => v / 1000),
|
|
124
|
+
name: 'string',
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
// Input: { _id: '123', totalWorkTimeMs: 5000, name: 'Sannu' }
|
|
128
|
+
// Output: { id: '123', workTime: 5, name: 'Sannu' }
|
|
129
|
+
```
|
|
130
|
+
|
|
116
131
|
---
|
|
117
132
|
|
|
118
133
|
## 🛡️ "Never Crash" Policy
|
package/dist/core/compiler.js
CHANGED
|
@@ -149,7 +149,9 @@ function compile(shape, opts = {}) {
|
|
|
149
149
|
const result = {};
|
|
150
150
|
for (const key of Object.keys(fieldFilters)) {
|
|
151
151
|
try {
|
|
152
|
-
const
|
|
152
|
+
const config = shapeObj[key].__isBuilder ? shapeObj[key].config : shapeObj[key];
|
|
153
|
+
const sourceKey = (config && typeof config === 'object' && 'from' in config && config.from) ? config.from : key;
|
|
154
|
+
const sourceVal = source[sourceKey];
|
|
153
155
|
let outVal = fieldFilters[key](sourceVal);
|
|
154
156
|
// apply redact hook if provided
|
|
155
157
|
if (opts.redact) {
|
package/dist/core/filter.js
CHANGED
|
@@ -77,6 +77,7 @@ function createBuilder(type) {
|
|
|
77
77
|
default(val) { config.default = val; return builder; },
|
|
78
78
|
transform(fn) { config.transform = fn; return builder; },
|
|
79
79
|
validate(fn) { config.validate = fn; return builder; },
|
|
80
|
+
from(key) { config.from = key; return builder; },
|
|
80
81
|
};
|
|
81
82
|
Object.setPrototypeOf(builder, proto);
|
|
82
83
|
if (type === 'string') {
|
package/dist/core/types.d.ts
CHANGED
|
@@ -12,6 +12,8 @@ export interface FieldConfig<T extends PrimitiveType = PrimitiveType> {
|
|
|
12
12
|
regex?: RegExp;
|
|
13
13
|
email?: boolean;
|
|
14
14
|
transform?: (v: any) => any;
|
|
15
|
+
/** Source field key name (aliasing) */
|
|
16
|
+
from?: string;
|
|
15
17
|
/** Custom validation function: return false if invalid */
|
|
16
18
|
validate?: (v: any) => boolean;
|
|
17
19
|
}
|
|
@@ -24,6 +26,7 @@ export interface ShapeBuilder<T extends PrimitiveType> {
|
|
|
24
26
|
default(val: InferPrimitive<T>): ShapeBuilder<T>;
|
|
25
27
|
transform(fn: (v: InferPrimitive<T>) => any): ShapeBuilder<T>;
|
|
26
28
|
validate(fn: (v: InferPrimitive<T>) => boolean): ShapeBuilder<T>;
|
|
29
|
+
from(key: string): ShapeBuilder<T>;
|
|
27
30
|
}
|
|
28
31
|
export interface StringBuilder extends ShapeBuilder<'string'> {
|
|
29
32
|
min(len: number): StringBuilder;
|
package/package.json
CHANGED