1
votes

I want to use the following library gojsonschema to validate json structure, Currenlty I've two questions to the example below

https://github.com/xeipuuv/gojsonschema

  1. what is the #/definitions/.... and what is the purpose of it?
  2. the name should have the following:

{required: true, unique: true, pattern: '/^[A-Za-z0-9_\-\.]+$/'}

The unique is that if I've another name filed in the schema it should be unique, How can I validate it with jsonschema?

var schema = `
{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "name": ...
    "properties": {
        "username": {"$ref": "#/definitions/name"},

update

what I found is this https://spacetelescope.github.io/understanding-json-schema/reference/regular_expressions.html

But how should I check it inside the json i've provided

should It be like this

var schema = `
    {
        "$schema": "http://json-schema.org/draft-04/schema#",
        "type": "object",
        "name": {
        "type": "object",
        "pattern":'/^[A-Za-z0-9_\-\.]+$/'
}

        "properties": {
            "username": {"$ref": "#/definitions/name"},
2
So, you have searched, found the JSON schema site, read through the docs and examples on it, right? If not, please do. - kostix
@kostix - Thanks for the tip :) , Im struggling to add regular expression condition to the jsonschema ? can you assist ? - 07_05_GuyT
check my answer, that should work - Brian Patterson

2 Answers

3
votes

https://datatracker.ietf.org/doc/draft-handrews-json-schema-validation/?include_text=1

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {  
        "username": {  
            "type": "array",
            "items": {
                "type": "string",
                "pattern": "/^[A-Za-z0-9_\-\.]+$/"
            },
            "uniqueItems": true
         }
    },
    "required": [  
         "username"
    ]
}

1: The "definitions" keywords provides a standardized location for schema authors to inline re-usable JSON Schemas into a more general schema. The keyword does not directly affect the validation result.

This keyword's value MUST be an object. Each member value of this object MUST be a valid JSON Schema.

As an example, here is a schema describing an array of positive integers, where the positive integer constraint is a subschema in "definitions":

  {
   "type": "array",
   "items": { "$ref": "#/definitions/positiveInteger" },
   "definitions": {
       "positiveInteger": {
           "type": "integer",
           "exclusiveMinimum": 0
       }
   }
   }

2: uniqueItems

The value of this keyword MUST be a boolean.

If this keyword has boolean value false, the instance validates successfully. If it has boolean value true, the instance validates successfully if all of its elements are unique.

Omitting this keyword has the same behavior as a value of false.


required

The value of this keyword MUST be an array. Elements of this array, if any, MUST be strings, and MUST be unique.

An object instance is valid against this keyword if every item in the array is the name of a property in the instance.

Omitting this keyword has the same behavior as an empty array.


properties

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

The value of "properties" MUST be an object. Each value of this object MUST be a valid JSON Schema.

This keyword determines how child instances validate for objects, and does not directly validate the immediate instance itself.

Validation succeeds if, for each name that appears in both the instance and as a name within this keyword's value, the child instance for that name successfully validates against the corresponding schema.

Omitting this keyword has the same behavior as an empty object.

2
votes
  1. #/definitions/name is an internal reference to another schema. The document should have section that looks like the following that is being referenced:

    "definitions": {
        "name": {
             ...
        }
    }
    
  2. JSON schema does not have a unique validator. There is a uniqueItems validator that can be used with arrays but I don't think that is what you are looking for.