Prateik
Prateik

Reputation: 605

How to validate custom types in JSON schema?

JSON Schema supports interger and number as numeric type. I have a schema which defines type as 'double' 'float'. I am using AJV in javascript to validate the schema but it is failing because of the above limitation. Is there any way I can validate the double and float types specifically?

Please note - I want to validate 2 things - 1. JSON schema 2. JSON against the JSON schema

I am stuck at 1

I have tried adding custom keyword in AJV, but its not same as the default type

My schema says

"lineSpacing": {
          "type": "double",
          "description": "Spacing between two lines"
        },

AJV compilation should allow double as a type. How to achieve that? Am I missing something very basic? Any other solution other languages will work too.

Upvotes: 1

Views: 3230

Answers (2)

leadpony
leadpony

Reputation: 264

You can use custom format for built-in number type and I think it is more desirable way for JSON Schema. Some validator implementations support user-defined value for format keyword even for types other than string.

Upvotes: 0

Kanchan Tyagi
Kanchan Tyagi

Reputation: 144

First I didn't understand why do you want to define "double" in your schema, because type "double" means you want to assign any decimal value like (10.02), but JSON schema have type "number", where you can define any decimal, positive, or negative values. ex below -

1.20     // correct
-1      // correct
200    // correct 
"42"  // wrong, because type is string, if it's inside double quotes

Upvotes: 2

Related Questions