You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
whitelist({name:'Darth'}, allowed, {omitUndefined:true}); //resolves with {name: 'Darth', lightsaber: {}}
41
44
```
42
45
43
46
## Pick allowed fields
44
47
Before sending data from a database to a client, you want to pick only fields that the client is allowed to see. This can be achieved by using the option `omitDisallowed: true`.
*`allowed`: an object that specifies which fields are allowed. The values can be
68
-
* a boolean: if the value is `true`, the field is allowed and *copied* to the result object
69
-
* an object: whitelist is called recursively (for nested objects)
70
-
* a function `fn(value, path)`: the result of the function is placed in the result object
69
+
*`src`: source object, array or primitive
70
+
*`allowed`: the checks on `src` are performed according to this value. The following values are accepted:
71
+
* an object `{key: value, ...}`:
72
+
* expects `src` to be an object.
73
+
* iterates over keys and uses the value for whitelisting the corresponding key/value pair in `src`
74
+
*`value` can be any value that is accepted as the `allowed` parameter
75
+
* an array with one element `[value]`:
76
+
* expects `src` to be an array
77
+
* iterates over elements of array `src` and whitelists according to `value`
78
+
*`value` can be any value that is accepted as the `allowed parameter`
79
+
* a function `fn(src, options)`:
80
+
* should return the whitelisted `src` (directly or via a promise)
81
+
* if `omitDisallowed` is `false` and `src` contains disallowed data, the function is responsible for throwing a `WhitelistError` (or rejecting the returned promise with a `WhitelistError`)
82
+
* a boolean: if the value is `true`, `src` is allowed and returned as the result
71
83
*`options`: an object with the following optional keys:
72
84
*`omitUndefined`: if set to `true`, it omits fields in the result whose values are undefined
73
-
*`omitDisallowed`: if set to `true`, it omits fields from src that are not present in `allowed`
85
+
*`omitDisallowed`: if set to `true`, it omits fields from src that are not present in `allowed`.
86
+
*`data`: custom data that is recursively passed to any function in the `allowed` parameter.
74
87
75
88
The function returns a new object with the whitelisted fields and throws a `whitelist.WhitelistError` if a field in `src` is not allowed (unless `omitDisallow` is `true`).
0 commit comments