我正在使用AJV来验证针对JSON模式(摇摇欲坠)的API响应。这是执行验证的脚本:
var Ajv = require('ajv');
var ajv = new Ajv();
var schema = {
"paths": {
"/users": {
"get": {
"security": [
{
"3_legged": [
"userprofile-search"
]
}
],
"parameters": [
{
"$ref": "#/parameters/IdentitiesId"
},
{
"$ref": "#/parameters/IdDocumentValue"
},
{
"$ref": "#/parameters/IdDocumentType"
}
],
"responses": {
"200": {
"headers": {
"x-correlator": {
"type": "string",
"format": "uuid",
}
},
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/UserProfile"
}
}
}
}
}
},
"/users/{user_id}": {
"get": {
"security": [
{
"3_legged": [
"userprofile-read"
]
}
],
"tags": [
"users"
],
"operationId": "getUserProfileInfo",
"parameters": [
{
"$ref": "#/parameters/UserId"
}
],
"responses": {
"200": {
"description": "OK",
"headers": {
"x-correlator": {
"type": "string",
"format": "uuid",
"description": "Correlation id"
}
},
"schema": {
"$ref": "#/definitions/UserProfile"
},
"examples": {
"application/json": {
"id": "A000-0000-0001",
"name": "Andrés Iniesta",
"id_document": {
"country": "ES",
"type": "NIF",
"value": "value"
},
"identities": [
{
"type": "email",
"id": "id",
"services": [
"iptv",
"email"
]
},
{
"type": "phone_number",
"id": "id",
"services": [
"mobile"
]
},
{
"type": "phone_number",
"id": "id",
"services": [
"mobile"
]
},
{
"type": "phone_number",
"id": "id",
"services": [
"landline",
"broadband"
]
}
]
}
}
}
}
}
}
},
"definitions": {
"UserProfile": {
"type": "object",
"required": [
"id",
"name",
"identities"
],
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"id_document": {
"$ref": "common.json#/definitions/IdDocument"
},
"identities": {
"type": "array",
"items": {
"$ref": "#/definitions/Identity"
}
}
}
},
"Identity": {
"type": "object",
"required": [
"id",
"services",
"type"
],
"properties": {
"type": {
"type": "string",
"enum": [
"phone_number",
"email",
"uid"
]
},
"services": {
"type": "array",
"items": {
"type": "string",
"enum": [
"mobile",
"invoicing"
]
}
},
"id": {
"type": "string"
}
}
}
}
}
var common = {
"definitions": {
"MoneyAmount": {
"type": "object",
"properties": {
"amount": {
"type": "number"
}
}
},
"IdDocument": {
"type": "object",
"required": [
"country",
"type",
"value"
],
"properties": {
"country": {
"type": "string"
},
"type": {
"type": "string"
},
"value": {
"type": "string"
}
}
}
}
}
var response={
"id": "123456789",
"name": "pruebas trocafone prepago",
"id_document": {
"country": "ARG",
"type": "P",
"value": "15042016"
},
"identities": [
{
"type": "uid",
"services": [
"invoicing"
],
"id": "511644813"
},
{
"type": "phone_number",
"services": [
"mobile"
],
"id": "00123456789"
},
{
"type": "email",
"services": [
"email"
],
"id": ""
}
]
}
ajv.addSchema(schema, 'user_profile.json');
ajv.addSchema(common, 'common.json');
var testajv = ajv.compile({ $ref: 'common.json#/definitions/IdDocument' });
console.log(testajv(JSON.stringify(response)), testajv.errors);
然后,我得到以下输出:
schema id ignored A000-0000-0001
false [ { keyword: 'type',
dataPath: '',
schemaPath: 'common.json#/definitions/IdDocument/type',
params: { type: 'object' },
message: 'should be object' } ]
1-我不明白为什么ajv告诉我“ schema id”被忽略。为什么重要?
2-为什么它告诉我IdDocument /类型“应该是对象”?它是响应中的对象,定义如下:
"id_document": {
"country": "ARG",
"type": "P",
"value": "15042016"
}
有人可以帮助我理解它吗?预先感谢!
答案 0 :(得分:2)
Swagger Schema不是JSON Schema,因此您必须在Swagger Schema(schema.definitions.UserProfile
)中找到正确的位置。
尽管Swagger Definition与JSON Schema不100%兼容,但是在大多数情况下,您可以使用通用验证器。
更多信息:https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#schemaObject。
您需要删除JSON.stringify
,因为它会从您的数据中创建一个string
(string
不是object
)。
ajv.addSchema(schema.definitions.UserProfile, 'user_profile.json');
ajv.addSchema(common, 'common.json');
var testajv = ajv.compile({ $ref: 'common.json#/definitions/IdDocument' });
console.log(testajv(response), ajv.errorsText(testajv.errors));