定义一个JSON接口,如何准确的描述它?
JSON schema可能是个好的方式。
https://json-schema.org/understanding-json-schema/reference/array.html
{
"type": "array",
"minItems": 4,
"maxItems": 4,
"items": {
"type": "integer",
"minimum": 0,
"maximum": 255
}
}
在线验证
https://www.jsonschemavalidator.net/
Ajv
https://ajv.js.org/guide/getting-started.html
yarn add ajv
Example
import Ajv from 'ajv';
const schema = { type: 'array', minItems: 4, maxItems: 4, items: { type: 'integer', minimum: 0, maximum: 255 } };
const ajv = new Ajv();
const validate = ajv.compile(schema);
console.log(validate([1, 2, 3, 4]));