0
var schema = {
"id": "test.json#",
"definitions": {
"body": {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
}
},
"request": {
"properties": {
"user": {
"$ref": "#/definitions/body"
}
}
}
},
"typeA": {
"$ref": "#/definitions/request"
},
"typeB": {
"allOf": [
{"$ref": "#/definitions/request"},
{"required": ["name"]} // Should require the name field in the user object.
]
}
}
var Ajv = require('ajv')
var ajv = Ajv({
schemas: [schema]
})
var data = {
user: {
"name": "tom",
"age": 1
}
}
var valid = ajv.validate({$ref: "test.json#/typeA"}, data)
console.log('VALID', valid) // Returns true.
var valid = ajv.validate({$ref: "test.json#/typeB"}, data)
console.log('VALID', valid) // Returns false.
私はJSONスキーマが初めてです。私は、typeAとtypeBの定義リクエストスキーマを再利用したいと思います。 typeBはユーザオブジェクトにnameフィールドを必要とするはずです。これをどのように達成するのですか?私は上記の私のソリューションが動作していない理由を知っています。必須は "user.name"のようなものでなければなりません。JSONスキーマ - ネストされたフィールドが必要です
ご協力いただきありがとうございます。ここ