Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Support for simple recursive schemes #173

Open
DrewMikola opened this issue Mar 6, 2023 · 0 comments
Open

Enhancement: Support for simple recursive schemes #173

DrewMikola opened this issue Mar 6, 2023 · 0 comments

Comments

@DrewMikola
Copy link

A common pattern (at least for me) is something like this:

(I'm using YAML here for ease of typing)

id: MySchema
properties:
  - name: 
       type: string
  - children:
       type: array
       items:
         $ref: "#" #Reference to this schema

This should validate a document where each object has a list of child objects that are of the same schema type as the parent object.

"Out of the box" valijson crashes with a stack-overflow, however I found a simple hack that enables this use case.

In schema_parser.h - makeOrReuseSchema:

        if (actualDocumentUri && (!currentScope || *actualDocumentUri != *currentScope)) {
            // Removed for brevity
        }
        // ADDED
        else if (*actualDocumentUri == *currentScope)
        {
            // Recursive document, return the parent schema
            return &rootSchema;
        }

This seems to work and handles the trivial example above.

A more robust solution would be capable of detecting any cyclic schema references. A simple solution would be an enhanced fetchDoc signature that would allow users to return a valijson::Schema document instead of an adapter document, pushing the problem to the user.

Thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant