
Security News
Axios Maintainer Confirms Social Engineering Attack Behind npm Compromise
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.
json-schema-conditionals
Advanced tools
This package allows you to define custom conditionals within a schema and then will translate that into valid JSON schema.
const injectConditionals = require('json-schema-conditionals');
const schemaWithConditionals = injectConditionals(schemaValues);
To create conditional validation through json schema, you need to use a series of if, then statements within an allOf key, however this can get very messy. Instead of defining these by hand you can instead define a new conditionals key and then when json schema conditionals complies the schema, it will inject all of the necessary if, then inside of an allOf. The conditionals key be placed at the top level of the schema.
Example definition:
"conditionals": [
{
"fields": ["picked_for_match"],
"dependsOn": {
"available_for_match": "yes"
}
},
{
"fields": ["starting_position"],
"dependsOn": {
"picked_for_match": "yes"
}
},
{
"fields": ["reason_for_not_being_picked"],
"dependsOn": {
"picked_for_match": "no"
}
}
]
Example Schema output:
"allOf": [
{
"if": {
"required": [
"available_for_match"
],
"properties": {
"available_for_match": {
"const": "yes"
}
}
},
"then": {
"required": [
"picked_for_match"
]
}
},
{
"if": {
"required": [
"available_for_match",
"picked_for_match"
],
"properties": {
"available_for_match": {
"const": "yes"
},
"picked_for_match": {
"const": "yes"
}
}
},
"then": {
"required": [
"starting_position"
]
}
},
{
"if": {
"required": [
"available_for_match",
"picked_for_match"
],
"properties": {
"available_for_match": {
"const": "yes"
},
"picked_for_match": {
"const": "no"
}
}
},
"then": {
"required": [
"reason_for_not_being_picked"
]
}
}
]
You can also have nested conditionals. For the fields string include the path by separating keys with a dot.
Example definition:
"conditionals": [
{
"fields": ["fieldToRequire.nestedKey.secondNestedKey"],
"dependsOn": {
"conditional": "no"
}
}
]
Example Schema Output:
"allOf": [
{
"if": {
"required": [
"conditional"
],
"properties": {
"conditional": {
"const": "no"
}
}
},
"then": {
"properties": {
"fieldToRequire": {
"type": "object",
"properties": {
"nestedKey": {
"type": "object",
"required": [
"secondNestedKey"
]
}
}
}
}
}
}
]
const schemaWithConditionals = injectConditionals(jsonSchemas)
Arguments
jsonSchemas e.g. [schema1, schema2]
Return Value
schemaWithConditionals e.g [schema1WithConditionals, schema2WithConditionals]
allOf propertyCreated by Wealth Wizards Software Engineering - http://wealthwizards.com
FAQs
Package to create conditionals within JSON schema
We found that json-schema-conditionals demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.