43

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"])
mkobit
  • 43,979
  • 12
  • 156
  • 150
Destroyer
  • 639
  • 1
  • 7
  • 13

2 Answers2

69

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).

cloudfeet
  • 12,156
  • 1
  • 56
  • 57
  • 1
    Is it possible to have case insensitive pattern matching? – Ninja Jul 10 '18 at 10:07
  • 1
    @Ninja it seems it can be done only via properly designed regular expression. Check https://json-schema.org/understanding-json-schema/reference/regular_expressions.html - usage of flags is not within recommended RegExp syntax for JSON Schema due to interoperability reasons. – PsychoFish Nov 16 '18 at 00:55
  • @Ninja for cace insensitive look at https://stackoverflow.com/questions/41874810/make-json-schema-pattern-case-insensitive/65673578#65673578 – rofrol Jan 11 '21 at 19:50
13

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)$": {}
    }
Lodewijk Bogaards
  • 19,777
  • 3
  • 28
  • 52