Proposal: `maxDepth` on objects

kai zhu kaizhu256 at gmail.com
Mon Oct 22 11:47:53 UTC 2018


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
kaizhu256 at gmail.com



> On 21 Oct 2018, at 8:45 PM, Oliver Dunk <oliver at oliverdunk.com> 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
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20181022/f5dd17b7/attachment-0001.html>


More information about the es-discuss mailing list