Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate a form using JSON Schema?

I want to validate the input from an HTML form using a JSON Schema describing it. I'm using Gary Court's JSV to validate it and it returns an error consistently. I used JSON Schema Lint (jsonschemalint.com) to check my schema. In Chrome Schema Lint tells me my schema is valid, but in Firefox, Safari & Opera the website tells me that my schema is valid JSON but not valid JSON schema. Can anyone help me out. My schema is below.

UPDATE 8/6/13 Thank you for all your replies. My updated JSON (updated below) is now validating in all browsers. However I am still getting the following error from JSV:

Report {errors: Array[1], validated: Object, instance: JSONInstance, schema: JSONSchema,   schemaSchema: JSONSchema…}
errors: Array[1]
    0: Object
        attribute: "type"
        details: Array[1]
            0: "object"
            length: 1
            __proto__: Array[0]
        message: "Instance is not a required type"
        schemaUri: "http://json-schema.org/draft-03/hyper-schema#"
        uri: "urn:uuid:808fe74b-b0d0-4774-8975-289f105dfeaa#"
        __proto__: Object
    length: 1
    __proto__: Array[0]
instance: JSONInstance
schema: JSONSchema
schemaSchema: JSONSchema
validated: Object
__proto__: Report

Let me first say that I may be interpreting the error message incorrectly. However I'm fairly certain this is referring to the "type": "object" line directly after the opening curly bracket. However the "type": "object" key:value is part of the Draft 03 spec at https://datatracker.ietf.org/doc/html/draft-zyp-json-schema-03. This is confusing because JSON Schema Lint uses the JSV library as well... Thanks for all your help so far.

{
"type": "object",
"$schema": "http://json-schema.org/draft-03/schema#",
"title": "FormValidation",
"description": "Describes the types of and valid inputs to a form generated via Form Creator",
"properties": {
    "Air Temperature (C)": {
        "type": "number",
        "description": "Air Temperature measurement in centigrade.",
        "required": false
    },
    "Ammonia": {
        "type": "number",
        "description": "Ammonia measurement at test site.",
        "required": false
    },
    "Aquatic Life Present": {
        "type": "string",
        "description": "Are organisms such as fish or frogs living near the test site?",
        "required": false
    },
    "Chlorophyll a": {
        "type": "number",
        "description": "Chlorophyll a measurement at test site.",
        "required": false
    },
    "Conductivity": {
        "type": "number",
        "description": "Water conductivity measurement at test site.",
        "required": false
    },
    "Date of Test": {
        "type": "string",
        "description": "Date the measurements were recorded.",
        "required": true
    },
    "Dissolved Oxygen 1": {
        "type": "number",
        "description": "Disolved oxygen reading at first depth.",
        "required": false
    },
    "Dissolved Oxygen 2": {
        "type": "number",
        "description": "Dissolved oxygen reading at second depth.",
        "required": false
    },
    "Latitude": {
        "type": "number",
        "description": "Latitude of the measurement site in degrees.",
        "required": true
    },
    "Longitude": {
        "type": "number",
        "description": "Longitude of the measurement site in degrees.",
        "required": true
    },
    "Nitrates": {
        "type": "number",
        "description": "Nitrate measurement at test site.",
        "required": false
    },
    "Orthophosphates": {
        "type": "number",
        "description": "Orthophosphate measurement at site of testing.",
        "required": false
    },
    "Phosphates": {
        "type": "number",
        "description": "Phosphate reading at measurement site.",
        "required": false
    },
    "Secchi Disk": {
        "type": "number",
        "description": "Secchi Disk depth reading at measurement site.",
        "required": false
    },
    "Site Change": {
        "type": "string",
        "description": "Has the site undergone noticeable physical change since the last measuring event?",
        "required": false
    },
    "Test Site": {
        "type": "string",
        "description": "Location where the measurements were recorded.",
        "required": true
    },
    "Turbidity (ntu)": {
        "type": "number",
        "description": "Cloudiness or haziness of water, measured in Nephelometric Turbidity Units (NTU).",
        "required": false
    },
    "Water Color or Odor": {
        "type": "string",
        "description": "Does the water have an strange colorations or emit a noticeable odor?",
        "required": false
    },
    "Water Temperature (C)": {
        "type": "number",
        "description": "Water Temperature measurement in centigrade.",
        "required": false
    },
    "pH": {
        "type": "number",
        "description": "pH measurement at test site.",
        "required": false
    }
}
}
like image 570
Otto Borden Avatar asked Aug 06 '13 04:08

Otto Borden


2 Answers

I checked it again in the JSON schema website and it seems the name "Turbidity (ntu)" is not a valid key. JSON schemadoes not 'like' parentheses in a key. It works if you leave the parentheses out, like in "Turbidity ntu".

I was just commenting on @pmagunia's entry when he apparently withdrew it. He remarked that required can only contain boolean values. It seems to me that the required property at the top is actually superfluous. I just tested it in JSON Schema Lint and the schema is said to be valid without it. But required can definitely only hold boolean values. Your array

[ "TestSite", "Date of Test", "Latitude", "Longitude" ]

was transformed by JSON Schema Lint into an unquoted string

TestSite,Date of Test,Latitude,Longitude

which will definitely be invalid JSON!

like image 132
Carsten Massmann Avatar answered Oct 04 '22 08:10

Carsten Massmann


Your json have whitespaces in keys. Like Air Temperature (C). Also remove the parenthesis. If you will remove whitespaces from your keys then it would be valid schema.

like image 35
Sharad Avatar answered Oct 04 '22 06:10

Sharad