javascript - 如何使用 JSON Schema 验证表单?

标签 javascript json validation jsonschema

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

UPDATE 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" 紧跟在左大括号之后的行。然而 "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
    }
}
}

最佳答案

我在the JSON schema website又查了一遍似乎名称 "Turbidity (ntu)" 不是有效的键。 JSON 架构不“喜欢”键中的括号。如果您省略括号,它就会起作用,例如 "Turbidity ntu"

当他显然撤回时,我只是在评论@pmagunia 的条目。他指出 required 只能包含 bool 值。 在我看来,顶部的 required 属性实际上是多余的。我刚刚在 JSON Schema Lint 中测试过它并且据说模式在没有它的情况下有效。但是 required 绝对只能保存 bool 值。你的数组

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

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

TestSite,Date of Test,Latitude,Longitude

这肯定是无效的 JSON!

关于javascript - 如何使用 JSON Schema 验证表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18072043/

相关文章:

javascript - 单击时 Jquery 图像交换

javascript - 从 JSON 创建数组 - Javascript

javascript - 为什么 Array.prototype.reduce() 不接受 Map 对象作为初始值?

javascript - 关于此 JSON 的路径语法

json - 单击 Tableview 单元格不显示数据

xml - Zend Framework 站点地图验证

javascript 日期验证未验证 2 月 31 日

validation - 如何使用 aurelia-validation 来排列项目

javascript - 按钮启用后失去其功能?

javascript - 使用 willdurand/BazingaJsTranslationBundle 的问题