2013-08-06 7 views
5

Voglio convalidare l'input da un modulo HTML utilizzando uno schema JSON che lo descrive. Sto usando JSV di Gary Court per convalidarlo e restituisce un errore in modo coerente. Ho usato JSON Schema Lint (jsonschemalint.com) per controllare il mio schema. In Chrome Schema Lint mi dice che il mio schema è valido, ma in Firefox, Safari & Opera, il sito web mi dice che il mio schema è JSON valido ma non valido schema JSON. Qualcuno mi può aiutare. Il mio schema è sottoCome convalidare un modulo utilizzando JSON Schema?

AGGIORNAMENTO 8/6/13 Grazie per tutte le vostre risposte. Il mio JSON aggiornato (aggiornato di seguito) è ora valido in tutti i browser. Tuttavia ancora sto ottenendo il seguente errore da 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 

Permettetemi di dire che io possa essere interpretare il messaggio di errore in modo non corretto. Tuttavia sono abbastanza sicuro che si riferisca alla linea "type": "object" direttamente dopo la parentesi graffa aperta. Tuttavia, la chiave "type": "object": value fa parte della specifica Draft 03 su http://tools.ietf.org/html/draft-zyp-json-schema-03. Ciò è fonte di confusione perché JSON Schema Lint usa anche la libreria JSV ... Grazie per tutto il tuo aiuto fino ad ora.

{ 
"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 
    } 
} 
} 
+0

potrebbe essere "richiesto" può essere solo "vero" o "falso" all'inizio? hai impostato un numero di valori non booleani. –

+0

Prova anche questo: http://jsonlint.com/ –

risposta

4

ho controllato di nuovo in the JSON schema website e sembra il nome "Turbidity (ntu)" non è una chiave valida. JSON schema non simula le parentesi in una chiave. Funziona se si lasciano le parentesi, come in "Turbidity ntu".

Stavo commentando la voce di @ pmagunia quando apparentemente l'ha ritirata. Ha osservato che required può contenere solo valori booleani. Mi sembra che la proprietà required nella parte superiore sia effettivamente superflua. L'ho appena testato in JSON Schema Lint e si dice che lo schema sia valido senza di esso. Ma required può sicuramente solo contenere valori booleani. La matrice

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

è stato trasformato da JSON Schema Lint in una stringa non quotate

TestSite,Date of Test,Latitude,Longitude 

che sarà sicuramente valida JSON!

+0

No, non era quello. Ho appena controllato di nuovo. Questa volta sono state accettate tutte le parentesi. Strano. Scusa ... – cars10m

+0

auto10 è corretta; la matrice delle proprietà richieste è valida solo nella bozza v4 di json-schema e dovrebbe quindi essere rimossa man mano che si utilizza la bozza v3. –

2

Il tuo json ha gli spazi bianchi nelle chiavi. Come Air Temperature (C). Rimuovi anche la parentesi. Se rimuoverai gli spazi bianchi dalle tue chiavi, allora sarebbe uno schema valido.