hi Oliver, a practical solution to your nested-validation-problem is to use a
recursive tree-walker that keeps track of depth. here's a real-world example
that limits the depth (to 3) for auto-generating swagger-data from
nested-schemas using technique [1].
```javascript
local.dbFieldRandomCreate = function (options) {
/*
* this function will create a random dbField from options.schemaP
*/
var depth, ii, max, min, schemaP, value;
depth = Number.isFinite(options.depth)
? options.depth
: 3;
...
// 5.4. Validation keywords for objects
default:
if (depth <= 0) {
break;
}
// recurse dbRowRandomCreate
value = local.dbRowRandomCreate({
depth: depth - 1,
modeNotRandom: options.modeNotRandom,
prefix: ['schema<' + JSON.stringify(schemaP) + '>'],
schema: schemaP
});
break;
```
hi TJ, a practical solution to circular-recursion is to have an Array/Set that
records all unique objects the tree-walker has traversed, and which it checks
against recursion. here's a real-world example of swagger-validation guarding
itself against circular schema-definitions using technique [2].
```javascript
local.swaggerValidateDataSchema = function (options) {
/*
* this function will validate options.data against the swagger options.schema
* http://json-schema.org/draft-04/json-schema-validation.html#rfc.section.5
*/
var $ref,
circularList,
...
circularList = [];
while (true) {
...
// dereference schema.$ref
$ref = schema && schema.$ref;
if (!$ref) {
break;
}
test = circularList.indexOf($ref) < 0;
local.throwSwaggerError(!test && {
data: data,
errorType: 'schemaDereferenceCircular',
prefix: options.prefix,
schema: schema
});
circularList.push($ref);
...
```
[1] maxDepth guard in auto-generating swagger-data from nested-schemas
https://github.com/kaizhu256/node-swgg/blob/2018.9.8/lib.swgg.js#L2377
[2] circular-schema guard in swagger-validation
https://github.com/kaizhu256/node-swgg/blob/2018.9.8/lib.swgg.js#L3940
kai zhu
[email protected]
> On 21 Oct 2018, at 8:45 PM, Oliver Dunk <[email protected]> wrote:
>
> I’d love to see some sort of `maxDepth` property on objects.
>
> For `{}`, it would return `0`.
> In the case of `{keyOne: true}`, it would return `1`.
> For `{keyOne: {anotherKey: false}, keyTwo: false}`, it would return `2`.
>
> My particular use case is validating a JSON payload sent by a user to prevent
> abuse. I don’t want to force a particular structure or set of keys, but I do
> want to make sure the level of nesting doesn’t get silly.
>
> The code to implement this is fairly short, but looks a bit messy. It’d be
> really nice to have a property instead.
> _______________________________________________
> es-discuss mailing list
> [email protected]
> https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss