学习 JSON Schema 的编写方式, 方便在使用 monaco 的时候对 编写的json数据做代码提示和规则检查. 同时也能方便以后在前后端统一数据检查规则. 引用: Understanding JSON Schema json-schema

概述

JSON Schema is a vocabulary that allows you to annotate and validate JSON documents.

  • Describes your existing data format(s).
  • Provides clear human- and machine- readable documentation.
  • Validates data which is useful for:
    • Automated testing.
    • Ensuring quality of client submitted data.

JSON Schema是一个词汇表,允许你注释和验证JSON文档。

  • 描述现有的数据格式
  • 提供清晰可供人和机器同时阅读的文档
  • 对数据进行校验
    • 自动化测试
    • 确保用户输入数据的质量

下面是一个 JSON Schema 的样例:

  • 数据 ```json { “name”: “George Washington”, “birthday”: “February 22, 1732”, “address”: “Mount Vernon, Virginia, United States” }

{ “first_name”: “George”, “last_name”: “Washington”, “birthday”: “1732-02-22”, “address”: { “street_address”: “3200 Mount Vernon Memorial Highway”, “city”: “Mount Vernon”, “state”: “Virginia”, “country”: “United States” } }

  1. - **Schema**
  2. ```json
  3. {
  4. "type": "object",
  5. "properties": {
  6. "first_name": { "type": "string" },
  7. "last_name": { "type": "string" },
  8. "birthday": { "type": "string", "format": "date" },
  9. "address": {
  10. "type": "object",
  11. "properties": {
  12. "street_address": { "type": "string" },
  13. "city": { "type": "string" },
  14. "state": { "type": "string" },
  15. "country": { "type" : "string" }
  16. }
  17. }
  18. }
  19. }

第一个结构不能验证通过, 而第二个可以.