52

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"])
6
  • 1
    What is not working? (You might want to put that hyphen at the end of the character class) Commented May 10, 2013 at 23:00
  • Any online validation does not work right. Commented May 10, 2013 at 23:03
  • 2
    What does "does not work right" mean? Do you get false positives? Do you get false negatives? Do you get some kind of error? Commented May 10, 2013 at 23:05
  • Validation fails with errors. json-schema-validator.herokuapp.com/syntax.jsp jsonschemavalidator.herokuapp.com jsonschemalint.com Too many to list. Commented May 10, 2013 at 23:08
  • 2
    Don't you think it might help potential answerers if you included those error messages in the question? Commented May 10, 2013 at 23:12

2 Answers 2

80

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

Sign up to request clarification or add additional context in comments.

3 Comments

Is it possible to have case insensitive pattern matching?
@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.
@Ninja for cace insensitive look at stackoverflow.com/questions/41874810/…
14

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)$": {}
    }

3 Comments

Thank you for providing an answer and not wasting my time. Much, much appreciated!
Wasn't the question was about testing the value, not the property name?
hey, it needs also to add "additionalProperties": false, to work properly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.