Javascript 如何使用JSON模式验证表单?
我想使用描述HTML表单的JSON模式验证它的输入。我使用Gary Court的JSV来验证它,它会一致地返回一个错误。我使用JSON模式Lint(jsonschemalint.com)检查我的模式。在Chrome模式中,Lint告诉我我的模式是有效的,但在Firefox、Safari和Opera中,网站告诉我我的模式是有效的JSON,但不是有效的JSON模式。有人能帮我吗。我的模式如下 更新8/6/13感谢您的回复。我更新的JSON(更新如下)现在正在所有浏览器中验证。但是,我仍然从JSV得到以下错误:Javascript 如何使用JSON模式验证表单?,javascript,json,validation,jsonschema,Javascript,Json,Validation,Jsonschema,我想使用描述HTML表单的JSON模式验证它的输入。我使用Gary Court的JSV来验证它,它会一致地返回一个错误。我使用JSON模式Lint(jsonschemalint.com)检查我的模式。在Chrome模式中,Lint告诉我我的模式是有效的,但在Firefox、Safari和Opera中,网站告诉我我的模式是有效的JSON,但不是有效的JSON模式。有人能帮我吗。我的模式如下 更新8/6/13感谢您的回复。我更新的JSON(更新如下)现在正在所有浏览器中验证。但是,我仍然从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
首先,我要说的是,我可能错误地解释了错误消息。但是,我相当肯定这是指开头的花括号后面的“type”:“object”
行。但是,“类型”:“对象”
键:值是03规范草案的一部分。这令人困惑,因为JSON模式Lint也使用JSV库。。。谢谢你迄今为止的帮助
{
"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
}
}
}
我再次签入,似乎名称“浊度(ntu)”
不是有效密钥JSON模式
不“喜欢”键中的括号。如果不使用括号,则可以使用,如中的“ntu”
我只是在评论@pmagunia的条目时,他显然撤回了它。他指出,required
只能包含布尔值。
在我看来,顶部的required
属性实际上是多余的。我刚刚在中测试了它,据说没有它模式是有效的。但是required
肯定只能保存布尔值。你的阵列
[ "TestSite", "Date of Test", "Latitude", "Longitude" ]
由JSON模式Lint转换为无引号字符串
TestSite,Date of Test,Latitude,Longitude
这肯定是无效的JSON json的键中有空格。如
空气温度(C)
。同时删除括号。如果您要从键中删除空白,则它将是有效的架构。是否“必需”只能在开头附近为“真”或“假”?您已将它设置为许多非布尔值。请也尝试此值:不,不是这样。刚刚又查了一遍。这次所有的括号都被接受了。奇怪。对不起…cars10是正确的;所需属性的数组仅在json模式的v4草稿中有效,因此在使用v3草稿时应将其删除。