2

I would like to validate a schema based on either its maximum/minimum (number) OR maximumLength/minimumLength (string). I have a json form:

[
  {
    "key":"foo",  
    "title":"Test",
    "type":"string" 
  }
]

and a json schema:

    {
 "type": "object",
  "properties": {
    "foo": {
        "type": ["number","string"],
        "maxLength":2,
        "minLength":0,
        "minimum":3,
        "maximum":10
    }
  }
}

and a json model:

{
  "foo": "bar"
}

Why does this example not work with validation? The model I have is not validated to false. According to this document it is possible to have different types defined in an array, but how can we do validation based on min/max values?

3 Answers 3

2

Your schema is correct. The validator you are using doesn't work properly. Here is an alternative that uses anyOf instead.

{
    "type": "object",
    "properties": {
        "foo": {
            "anyOf": [
                { "$ref": "#/definitions/boundedNumber" }
                { "$ref": "#/definitions/boundedString" }
            ]
        }
    },
    "definitions": {
        "boundedString": {
            "type": "string",
            "maxLength": 2,
            "minLength": 0
        },
        "boundedNumber": {
            "type": "number",
            "minimum": 3,
            "maximum": 10
        }
    }
}

Although it is quite a bit longer, some would argue that this is actually easier to read/maintain because of the separation of the type specific keywords.

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

3 Comments

Your suggestion works with the json schema, but it still will not work with the library I am using: angular-schema-form validation, which is also using json schema. I think maybe there is something wrong withe the library.
I played around with the angular-schema-form validation demo page and it looks like it supports only the most basic JSON Schema features. What it does support isn't even entirely compliant with the JSON Schema spec.
Nor should it be, UI is far more complex than JSON Schema can currently handle and as the goal is to work within the UI, many JSON Schema rules are quite problematic, which is why there is a proposal to add a UI vocabulary to JSON Schema to reduce confusion and make it easy to work with for UI generation.
2

Your schema is validating JSON objects ("type":"object"). In addition, if they have a property with key "foo", its value must be either a number between 3 an 10, or a string of maximum length 2.

Valid objects according to your schema:

{"foo":6}
{"foo":"as"}

Invalid objects:

{"foo":60}
{"foo":"asereje"}

If you want to validate arrays you must define your parent object as an array and use items tag to specify the schema for the array items, for instance:

{
    "type" : "array",
    "items" : {
        "type" : "object",
        "properties" : {
            "foo" : {
                "type" : ["number", "string"],
                "maxLength" : 2,
                "minLength" : 0,
                "minimum" : 3,
                "maximum" : 10
            }
        }
    }
}

The schema above would validate the following JSON array:

[{
        "foo" : 6
    }, {
        "foo" : "as"
    }
]

3 Comments

I edited my question to add a model, to avoid confusion with the form. I am not able to get the example you provided to work as intended. Do I have to change my form as well?
You can test that the example validates the provided JSON here: json-schema-validator.herokuapp.com
You are completely right. I think maybe it is something wrong with the angular-schema-form validation.
0

John the issue is the type "type" : ["number", "string"]

Angular Schema Form is generating fields from the combined JSON Schema and the UI Schema (form), when the field type is defined it knows which type to validate against, when there are multiple types it doesn't know which part of the spec to base the form field against, so it falls back to a textbox and does not add the appropriate validation requirements for string alone.

To achieve your desired outcome you would need to tell it which field type you wish to use. There is a bug in the 0.8.x versions where it does not validate based on the type set in the UI schema if it cannot determine the type in the data schema, I believe that is fixed in the latest development branch. If not if there is an issue raised in Git, I would prioritise it.

Comments

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.