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.

Conditional Schema

See original GitHub issue

Suppose I want to support an abstract payload that uses another field to drive nested validations:

TEST_TASK = {
    "folder": {
        "required": True
    }
}

BUILD_TASK = {
    "environment": {
        "allowed": ["staging", "product"]
    }
}

ABSTRACT_TASK = {
    'type': {
        'allowed': ['test', 'build']
    },
    'data': {
        'type': 'dict',
        'required': True,
        'anyof': [
            {'schema': TEST_TASK},
            {'schema': BUILD_TASK}
        ]
    }
}

>>> validator = cerberus.Validator(schemas.ABSTRACT_TASK)
>>> validator.validate({
    "type": "build",
    "data": {
        "environment": "bad"
    }
})
>>> pp validator.errors
{'data': {'anyof': 'no definitions validated',
      'definition 0': {'environment': 'unknown field',
                       'folder': 'required field'},
      'definition 1': {'environment': 'unallowed value bad'}}}

Is there a way to conditionally use definition 1 when the parent has a type of build? For example, if the ABSTRACT_TASK is modified so that anyof is replaced with the BUILD_TASK schema:

>>> pp validator.errors
{'data': {'environment': 'unallowed value bad'}}

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
funkyfuturecommented, Sep 10, 2015

what about such constructs?

field: {'if': {rule: constraint, …},
        'then': {rule: constraint, …},
        'else': {rule: constraint, …}
       }
1reaction
funkyfuturecommented, Sep 10, 2015

or?

field: {'case': {condition_schema1: conditional_schema1}, 
                {'condition_schema2: conditional_schema2}}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Applying Subschemas Conditionally - JSON Schema
The dependentSchemas keyword conditionally applies a subschema when a given property is present. This schema is applied in the same way allOf applies ......
Read more >
Applying subschemas conditionally | Opis JSON Schema
This is a conditional structure containing three keywords: if , then and else . Every keyword value must be a valid JSON schema...
Read more >
jsonSchema attribute conditionally required - Stack Overflow
The dependentSchemas keyword is a conditional way to apply a schema. Foreach property in dependentSchemas , if the property is present in ...
Read more >
How to conditionally validate based on the values of external ...
Essentially, I need conditional validation (if-then-else) where the if schema can refer to either some sort of external variable or a field in...
Read more >
Conditional display and validation of properties - Sitefinity ...
Conditional display and validation of properties. This article contains. Overview · Procedure · JSON schema · Overview. Each type can have properties assigned ......
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