-
-
Notifications
You must be signed in to change notification settings - Fork 208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
oneOf/anyOf doesn't seem to work with objects #290
Comments
Would you mind trying to add |
Yes I did that as well before, but it didn't make a difference. |
this schema works:
Defining the
|
Good catch, it makes certainly more sense without specifying I just did a quick check and ran {
"type": "object",
"anyOf": [
{
"properties": {
"foo": { "type": "string" }
}
},
{
"properties": {
"bar": { "type": "string" }
}
}
]
} through the online JSON schema validator, which considers it valid against all schema drafts, but probably cannot verify such semantic violations anyway. |
I think this is a bug in this module: we should throw if we cannot deal with this ambiguity. Better of, we could handle it if it wasn't really hard. The schema is valid JSON schema (we validate it on creation). |
Would you like to send a PR? |
Sure, I can have a look, but it may take a bit until I get to it. |
same issue on my side, trying to serialize const schema = {
type: 'array',
items: {
anyOf: [
{
type: 'object',
required: [ 'id', 'createdAt', 'author', 'type', 'body' ],
properties: {
id: { type: 'string' },
createdAt: { type: 'string', format: 'date-time' },
author: {
type: 'object',
required: [ '_id', 'fullName' ],
properties: { _id: { type: 'string' }, fullName: { type: 'string' } },
additionalProperties: false
},
type: { type: 'string', enum: [ 'Text' ] },
body: { type: 'string' }
},
additionalProperties: false
},
{
type: 'object',
required: [ 'id', 'createdAt', 'author', 'type', 'status' ],
properties: {
id: { type: 'string' },
createdAt: { type: 'string', format: 'date-time' },
author: {
type: 'object',
required: [ '_id', 'fullName' ],
properties: { _id: { type: 'string' }, fullName: { type: 'string' } },
additionalProperties: false
},
type: { type: 'string', enum: [ 'StatusTransition' ] },
status: {
type: 'string',
}
},
additionalProperties: false
}
]
}
} And I get : {
"messages": [
null
]
} Update: if I add {
"messages": [
{}
]
} Update 2: Full example to test: https://gist.github.com/stalniy/05f3940d5a55d80a38557edd8ffbfe14 |
is there any support for discriminated union types? |
As a general rule, I suggest running the debugMode whenever there is some issue to understand what is going on. Debugging a bit your gist with this feature I found:
items: {
- type: "object",
anyOf: [
With these two changes, your code will works 👍🏽 |
Are there any known workarounds at the moment for using the
Actually, it looks like going with option 1 allows arbitrary non-objects to pass validation and fall through, so that's no good :( It seems like the only thing that works at the moment is modifying the schema before it's used as an output schema in Fastify and setting the relevant property to an empty object. |
No: it is a OpenAPI keyword and fast-json-stringify requires a JSON Schema Draft 7 |
Sorry, to clarify, it's not that I need support for the |
@stalniy Here is another implementation based on AJV JTD Serializer that should ensure better soundness regarding the validation of the schema cc @epoberezkin const Ajv = require("ajv/dist/jtd");
const ajv = new Ajv(); // options can be passed, e.g. {allErrors: true}
const schemaFoo = {
properties: {
id: { type: "string" },
createdAt: { type: "timestamp" },
author: {
properties: { _id: { type: "string" }, fullName: { type: "string" } },
},
type: { enum: ["Test"] },
body: { type: "string" },
},
};
const schemaBar = {
properties: {
id: { type: "string" },
createdAt: { type: "timestamp" },
author: {
properties: { _id: { type: "string" }, fullName: { type: "string" } },
},
type: { enum: ["StatusTransition"] },
status: {
type: "string",
},
},
};
const schema = {
elements: {
metadata: {
union: [schemaFoo, schemaBar],
},
},
};
// const dataFoo = [
// {
// type: "Test",
// body: "bla",
// id: "1",
// createdAt: new Date(),
// author: {
// _id: "2",
// fullName: "John Doe",
// },
// },
// ];
const dataBar = [
{
type: "StatusTransition",
status: "bla",
id: "1",
createdAt: new Date(),
author: {
_id: "2",
fullName: "John Doe",
},
},
];
const validate = ajv.compile(schema);
const serialize = ajv.compileSerializer(schema);
function validateAndSerialize(data) {
const valid = validate(data);
if (valid) {
console.log(serialize(data));
} else {
console.log("invalid", validate.errors);
}
}
for (const data of [
// dataFoo,
dataBar,
]) {
validateAndSerialize(data);
} |
I seem to have ran in this issue with a GeoJSON structure in which I had a It scrambled the body of the request, altering the first coordinates of the geometry. |
This issue needs somebody to lead its implementation, |
🐛 Bug Report
oneOf/anyOf doesn't seem to work with objects.
To Reproduce
The following schema models a union type, i.e. in TypeScript terms:
Output:
Expected behavior
Since both objects match the schema, both should be stringified.
Your Environment
The text was updated successfully, but these errors were encountered: