5

我想使用描述它的 JSON 模式来验证来自 HTML 表单的输入。我正在使用 Gary Court 的 JSV 对其进行验证,并且它始终返回错误。我使用 JSON Schema Lint (jsonschmalint.com) 来检查我的架构。在 Chrome 架构中,Lint 告诉我我的架构是有效的,但在 Firefox、Safari 和 Opera 中,网站告诉我我的架构是有效的 JSON,但不是有效的 JSON 架构。谁能帮我吗。我的架构如下。

2013 年 8 月 6 日更新感谢您的所有回复。我更新的 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"直接在左大括号之后的行。然而,"type": "object"key:value 是https://datatracker.ietf.org/doc/html/draft-zyp-json-schema-03上的 Draft 03 规范的一部分。这很令人困惑,因为 JSON Schema 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
    }
}
}
4

2 回答 2

4

我在JSON 模式网站中再次检查了它,似乎该名称"Turbidity (ntu)"不是有效的密钥。JSON schema不“喜欢”键中的括号。如果你把括号去掉,它就可以工作,比如 in "Turbidity ntu"

当他显然撤回时,我只是在评论@pmagunia 的条目。他说required只能包含布尔值。在我看来,required顶部的财产实际上是多余的。我刚刚在JSON Schema Lint中对其进行了测试,据说没有它该模式是有效的。但required绝对只能保存布尔值。你的阵列

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

由 JSON Schema Lint 转换为不带引号的字符串

TestSite,Date of Test,Latitude,Longitude

这肯定是无效的 JSON!

于 2013-08-06T05:10:43.150 回答
3

您的 json 键中有空格。喜欢Air Temperature (C)。同时删除括号。如果您要从键中删除空格,那么它将是有效的模式。

于 2013-08-06T05:11:37.077 回答