在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:krg7880/json-schema-generator开源软件地址:https://github.com/krg7880/json-schema-generator开源编程语言:JavaScript 100.0%开源软件介绍:json-schema-generatorJSON schema generated based on draft-v4 of the specification. Note that the full spec if not yet supported. The compiler will be enhanced to support as much as possible. More specifically, there's no support for $ref nodes or special nodes like location (lat, long), etc. These features will be added in future releases or you can always fork and make it better :-) Install (GIT)git clone https://github.com/krg7880/json-schema-generator
cd json-schema-generator
npm install . Install as cli (NPM)Run on the command line: npm install -g json-schema-generator Then use (for example): #### JSON PATH
json-schema-generator path/to/input.json -o path/to/output.json
#### JSON URL
json-schema-generator https://sample.com/path/to/input.json --jsondir ./source/backup -o ./path/to/dir/
#### JSON STDIN | STDOUT
cat input.json | json-schema-generator > output.json Install as lib (NPM)Run on the command line: npm install json-schema-generator --save-dev Then, in your project: var jsonSchemaGenerator = require('json-schema-generator'),
obj = { some: { object: true } },
schemaObj;
schemaObj = jsonSchemaGenerator(json); Cli usage
Example JSON{
"title": "fresh fruit schema v1",
"type": "object",
"required": ["skin", "colors", "taste"],
"properties": {
"colors": {
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": {
"type": "string"
}
},
"skin": {
"type": "string"
},
"taste": {
"type": "number",
"minimum": 5
}
}
} Example Output{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "",
"type": "object",
"properties": {
"title": {
"type": "string",
"minLength": 1
},
"type": {
"type": "string",
"minLength": 1
},
"required": {
"type": "array",
"items": {
"required": [
],
"properties": {
}
}
},
"properties": {
"type": "object",
"properties": {
"colors": {
"type": "object",
"properties": {
"type": {
"type": "string",
"minLength": 1
},
"minItems": {
"type": "number"
},
"uniqueItems": {
"type": "boolean"
},
"items": {
"type": "object",
"properties": {
"type": {
"type": "string",
"minLength": 1
}
},
"required": [
"type"
]
}
},
"required": [
"type",
"minItems",
"uniqueItems",
"items"
]
},
"skin": {
"type": "object",
"properties": {
"type": {
"type": "string",
"minLength": 1
}
},
"required": [
"type"
]
},
"taste": {
"type": "object",
"properties": {
"type": {
"type": "string",
"minLength": 1
},
"minimum": {
"type": "number"
}
},
"required": [
"type",
"minimum"
]
}
},
"required": [
"colors",
"skin",
"taste"
]
}
},
"required": [
"title",
"type",
"required",
"properties"
]
} BackgroundI created this schema generator to validate JSON responses from APIs. As the JSON API is enhanced and nodes are added or removed from the response, the schema is regenerated and validated against the newly deployed API. TestsTo run tests, including fetching documents via HTTP, we've added node-stubby-server-cli to help with serving mock data. The ports for the stub server is defined under test/helpers/stubby-cli, in the event the default port is in use, you can change them there. npm install -g stubby Install mocha globally (as cli) and run npm test Validating DocumentsJSON documents can be validated against schemas using chai-json-schema. See the tests under test for example usage. ContributorsThanks to those who have contributed. These kind folks are listed below: |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论