2013-08-06 18 views
5

Tôi muốn xác thực đầu vào từ một biểu mẫu HTML bằng cách sử dụng Lược đồ JSON mô tả nó. Tôi đang sử dụng JSV của Gary Court để xác thực nó và nó trả về một lỗi nhất quán. Tôi đã sử dụng lược đồ JSON Lint (jsonschemalint.com) để kiểm tra lược đồ của tôi. Trong Lược đồ Chrome Lint cho tôi biết lược đồ của tôi là hợp lệ, nhưng trong Firefox, Safari & Opera trang web cho tôi biết rằng lược đồ của tôi là JSON hợp lệ nhưng không phải là lược đồ JSON hợp lệ. Bất cứ ai có thể giúp tôi ra ngoài. Sơ đồ của tôi ở bên dưới.Làm cách nào để xác thực biểu mẫu bằng JSON Schema?

CẬP NHẬT 8/6/13 Cảm ơn tất cả các thư trả lời của bạn. JSON cập nhật của tôi (được cập nhật bên dưới) hiện đang xác thực trong tất cả các trình duyệt. Tuy nhiên, tôi vẫn nhận được lỗi sau từ 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 

Hãy để tôi nói rằng tôi có thể diễn giải thông báo lỗi không chính xác. Tuy nhiên tôi khá chắc chắn điều này là đề cập đến các dòng "type": "object" trực tiếp sau khi mở dấu ngoặc nhọn. Tuy nhiên, khóa "type": "object": giá trị là một phần của thông số Draft 03 tại http://tools.ietf.org/html/draft-zyp-json-schema-03. Điều này là khó hiểu vì JSON Schema Lint cũng sử dụng thư viện JSV ... Cảm ơn tất cả sự giúp đỡ của bạn cho đến nay.

{ 
"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

có thể là "bắt buộc" chỉ có thể là 'đúng' hoặc 'sai' gần đầu? bạn đã đặt nó thành một số giá trị không boolean. –

+0

Hãy thử cái này cũng: http://jsonlint.com/ –

Trả lời

4

Tôi đã kiểm tra nó một lần nữa trong the JSON schema website và có vẻ như tên "Turbidity (ntu)" không phải là một chìa khóa hợp lệ. JSON schema không 'thích' dấu ngoặc đơn trong khóa. Nó hoạt động nếu bạn để lại dấu ngoặc đơn, như trong "Turbidity ntu".

Tôi vừa nhận xét về mục nhập của @agagunia khi anh ấy dường như đã rút nó. Ông nhận xét rằng required chỉ có thể chứa các giá trị boolean. Dường như với tôi rằng thuộc tính required ở trên cùng thực sự không cần thiết. Tôi vừa thử nghiệm nó trong JSON Schema Lint và lược đồ được cho là hợp lệ mà không có nó. Nhưng required chắc chắn chỉ có thể chỉ giữ giá trị boolean. mảng của bạn

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

được biến đổi bởi JSON Schema Lint thành một chuỗi không thể viện chứng

TestSite,Date of Test,Latitude,Longitude 

đó chắc chắn sẽ là không hợp lệ JSON!

+0

Không, không phải vậy. Chỉ cần kiểm tra lại. Lần này tất cả các dấu ngoặc đơn đã được chấp nhận. Lạ thật. Xin lỗi ... – cars10m

+0

cars10 là chính xác; mảng các thuộc tính bắt buộc chỉ hợp lệ trong bản dự thảo v4 của lược đồ json và do đó sẽ được loại bỏ khi bạn đang sử dụng bản nháp v3. –

2

Json của bạn có khoảng trắng trong khóa. Giống như Air Temperature (C). Đồng thời xóa dấu ngoặc đơn. Nếu bạn sẽ loại bỏ khoảng trắng từ các khóa của bạn thì nó sẽ là lược đồ hợp lệ.

Các vấn đề liên quan