require("ajv")
const options: Ajv.Options = {
allErrors: true
};
function go() {
const ajv = new Ajv({
nullable: true,
allErrors: true
});
const schema = {
"type": "object",
"properties": {
"root": {
"$ref": "#/components/schemas/ObjectA"
}
},
"components": {
"schemas": {
"ObjectA": {
"type": "object",
"properties": {
"property1": {
"allOf": [{ "$ref": "#/components/schemas/ObjectB" }],
"nullable": true
}
}
},
"ObjectB": {
"type": "object",
"properties": {
"foo": {
"type": "boolean"
}
}
}
}
}
}
const schemaInline = {
"type": "object",
"properties": {
"root": {
"$ref": "#/components/schemas/UpdateAudienceLoadBoardRequestOptions"
}
},
"additionalPropertes": false,
"components": {
"schemas": {
"UpdateAudienceLoadBoardRequestOptions": {
"type": "object",
"properties": {
"transactionDetails": {
"type": "object",
"properties": {
"test": {
"type": "boolean"
}
},
"required": ["test"],
"additionalProperties": false,
"nullable": true
}
}
},
"UpdateTransactionDetails": {
"type": "object",
"properties": {
"test": {
"type": "boolean"
}
},
"required": ["test"],
"additionalProperties": false
}
}
}
}
let validateMessage = ajv.compile(schema);
const nulled = {
"root": {
"property1": null
}
} as any;
const good = {
"root": {
"property1": { "test": true }
}
} as any;
if (validateMessage(nulled)) {
console.log('good2');
}
else {
console.log(JSON.stringify(validateMessage.errors));
// output is: [{"keyword":"type","dataPath":".root.property1","schemaPath":"#/components/schemas/ObjectB/type","params":{"type":"object"},"message":"should be object"}]
}
}
go();