Using RegEx in JSON Schema

55,810

Solution 1

To test a string value (not a property name) against a RegEx, you should use the "pattern" keyword:

{
    "type": "object",
    "properties": {
        "progBinaryName": {
            "type": "string",
            "pattern": "^[A-Za-z0-9 -_]+_Prog\\.(exe|EXE)$"
        }
    }
}

P.S. - if you want the pattern to match the key for the property (not the value), then you should use "patternProperties" (it's like "properties", but the key is a RegEx).

Solution 2

Your JSON schema syntax is incorrect. Change

"patternProperties": {
    "progBinaryName": "^[A-Za-z0-9 -_]+_Prog\\.(exe|EXE)$"
    }

to

"patternProperties": {
    "^[A-Za-z0-9 -_]+_Prog\\.(exe|EXE)$": {}
    }
Share:
55,810
Destroyer
Author by

Destroyer

Updated on January 12, 2021

Comments

  • Destroyer
    Destroyer over 3 years

    Trying to write a JSON schema that uses RegEx to validate a value of an item.

    Have an item named progBinaryName whose value should adhrere to this RegEx string "^[A-Za-z0-9 -_]+_Prog\\.(exe|EXE)$".

    Can not find any tutorials or examples that actually explain the use of RegEx in a JSON schema.

    Any help/info would be GREATLY appreciated!

    Thanks, D

    JSON SCHEMA

    {
        "name": "string",
        "properties": {
            "progName": {
                "type": "string",
                "description": "Program Name",
                "required": true
            },
            "ID": {
                "type": "string",
                "description": "Identifier",
                "required": true
            },
            "progVer": {
                "type": "string",
                "description": "Version number",
                "required": true
            },
            "progBinaryName": {
                "type": "string",
                "description": "Actual name of binary",
                "patternProperties": {
                    "progBinaryName": "^[A-Za-z0-9 -_]+_Prog\\.(exe|EXE)$"
                },
                "required": true
            }
        }
    }
    

    ERRORS:

    Warning! Better check your JSON.

    Instance is not a required type - http://json-schema.org/draft-03/hyper-schema#


    Schema is valid JSON, but not a valid schema.


    Validation results: failure

    [ {
        "level" : "warning",
        "schema" : {
            "loadingURI" : "#",
            "pointer" : ""
        },
        "domain" : "syntax",
        "message" : "unknown keyword(s) found; ignored",
        "ignored" : [ "name" ]
    }, {
        "level" : "error",
        "domain" : "syntax",
        "schema" : {
            "loadingURI" : "#",
            "pointer" : "/properties/ID"
        },
        "keyword" : "required",
        "message" : "value has incorrect type",
        "expected" : [ "array" ],
        "found" : "boolean"
    }, {
        "level" : "error",
        "domain" : "syntax",
        "schema" : {
            "loadingURI" : "#",
            "pointer" : "/properties/progBinaryName"
        },
        "keyword" : "required",
        "message" : "value has incorrect type",
        "expected" : [ "array" ],
        "found" : "boolean"
    }, {
        "level" : "error",
        "schema" : {
            "loadingURI" : "#",
            "pointer" : "/properties/progBinaryName/patternProperties/progBinaryName"
        },
        "domain" : "syntax",
        "message" : "JSON value is not a JSON Schema: not an object",
        "found" : "string"
    }, {
        "level" : "error",
        "domain" : "syntax",
        "schema" : {
            "loadingURI" : "#",
            "pointer" : "/properties/progName"
        },
        "keyword" : "required",
        "message" : "value has incorrect type",
        "expected" : [ "array" ],
        "found" : "boolean"
    }, {
        "level" : "error",
        "domain" : "syntax",
        "schema" : {
            "loadingURI" : "#",
            "pointer" : "/properties/progVer"
        },
        "keyword" : "required",
        "message" : "value has incorrect type",
        "expected" : [ "array" ],
        "found" : "boolean"
    } ]
    

    Problem with schema#/properties/progBinaryName/patternProperties/progBinaryName : Instance is not a required type
    Reported by http://json-schema.org/draft-03/hyper-schema#
    Attribute "type" (["object"])
    
  • Destroyer
    Destroyer almost 11 years
    Thank you for providing an answer and not wasting my time. Much, much appreciated!
  • cloudfeet
    cloudfeet almost 11 years
    Wasn't the question was about testing the value, not the property name?
  • Ninja
    Ninja almost 6 years
    Is it possible to have case insensitive pattern matching?
  • PsychoFish
    PsychoFish over 5 years
    @Ninja it seems it can be done only via properly designed regular expression. Check json-schema.org/understanding-json-schema/reference/… - usage of flags is not within recommended RegExp syntax for JSON Schema due to interoperability reasons.
  • rofrol
    rofrol over 3 years
    @Ninja for cace insensitive look at stackoverflow.com/questions/41874810/…