question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hi

I have this json:

data = {
    "company": "Google",
    "members": [
        {
            "type": 2,
            "name": "James",
            "age": 25
        },
        {
            "type": 1,
            "name": "Maria",
            "age": 22
        },
        {
            "type": 2,
            "name": "George",
            "age": 44
        }
    ]
}

I just don’t know how to validate the members age, because the number of members is not fixed, it could be 1, 2 or more and it needs to be validated by the type field.

For example: George’s age must return an error because his type (2) needs to be between 18 and 30

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
deckar01commented, Jan 29, 2020

Validating a relationship between two fields can also be accomplished using schema level validation. With schema validation, you end up writing the polymorphism discriminator in the validator and writing the validation procedurally. If you have more than one field that needs to customize validation based on the type, it is more practical to use a library like marshmallow-polyfield to avoid duplicating the discriminator and allow using the declarative validators marshmallow provides.

https://marshmallow.readthedocs.io/en/stable/extending.html#schema-level-validation

https://github.com/Bachmann1234/marshmallow-polyfield

0reactions
GabrielFielcommented, Jan 29, 2020

I managed to make using Marshmallow Schema. Thank you

Read more comments on GitHub >

github_iconTop Results From Across the Web

JSON Online Validator and Formatter - JSON Lint
JSONLint is an online editor, validator, and reformat tool for JSON, which allows you to directly type your code, copy and paste it,...
Read more >
JSON Formatter & Validator
The JSON Formatter & Validator beautifies and debugs JSON data with advanced formatting and validation algorithms.
Read more >
Best JSON Formatter and JSON Validator: Online JSON ...
Online JSON Formatter / Beautifier and JSON Validator will format JSON data, and helps to validate, convert JSON to XML, JSON to CSV....
Read more >
JSONLint tool to validate JSON data - Code Beautify
JSON Validator ( JSON Lint ) is easy to use tool to validate JSON data. Copy, Paste, and Validate. JSON is used by...
Read more >
JSON Validator - Free Tool To Validate JSON Code Online
JSON Validator is an online web-based tool designed to help users validate their JSON codes. By merely e ntering the JSON code in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found