New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

json-schema-conditionals

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-schema-conditionals

Package to create conditionals within JSON schema

latest
Source
npmnpm
Version
0.0.2
Version published
Maintainers
1
Created
Source

NPM

JSON Schema Conditionals CircleCI

This package allows you to define custom conditionals within a schema and then will translate that into valid JSON schema.

Example

const injectConditionals = require('json-schema-conditionals');

const schemaWithConditionals = injectConditionals(schemaValues);

Conditionals

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"
                                ]
                            }
                        }
                    }
                }
            }
        }
    ]

Docs

const schemaWithConditionals = injectConditionals(jsonSchemas)

Arguments

jsonSchemas e.g. [schema1, schema2]

  • Array of all the json schemas that you want the conditionals being applied to.
  • If you have multiple schemas at once then inject conditionals will only be able to reference and create conditionals for keys within the same schema.

Return Value

schemaWithConditionals e.g [schema1WithConditionals, schema2WithConditionals]

  • The return value is an array of json schema with the additional allOf property
  • This schema can now be read/used by other schema validation packages such as Ajv

Created by Wealth Wizards Software Engineering - http://wealthwizards.com

FAQs

Package last updated on 25 Feb 2022

Did you know?

Socket

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.

Install

Related posts