📜  json 到架构 javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:42.430000             🧑  作者: Mango

代码示例1
var Validator = require('jsonschema').Validator;var v = new Validator(); // Address, to be embedded on Personvar addressSchema = {  "id": "/SimpleAddress",  "type": "object",  "properties": {    "lines": {      "type": "array",      "items": {"type": "string"}    },    "zip": {"type": "string"},    "city": {"type": "string"},    "country": {"type": "string"}  },  "required": ["country"]}; // Personvar schema = {  "id": "/SimplePerson",  "type": "object",  "properties": {    "name": {"type": "string"},    "address": {"$ref": "/SimpleAddress"},    "votes": {"type": "integer", "minimum": 1}  }}; var p = {  "name": "Barack Obama",  "address": {    "lines": [ "1600 Pennsylvania Avenue Northwest" ],    "zip": "DC 20500",    "city": "Washington",    "country": "USA"  },  "votes": "lots"}; v.addSchema(addressSchema, '/SimpleAddress');console.log(v.validate(p, schema));