fast-json-stringify: oneOf/anyOf doesn't seem to work with objects
π 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:
type Foo = { foo: string, baz: string };
type Bar = { bar: string, baz: string };
type FooBar = Foo | Bar;
const fastJson = require("fast-json-stringify");
const schemaFoo = {
properties: {
foo: { type: "string" },
baz: { type: "string" },
},
required: ["foo", "baz"],
};
const schemaBar = {
properties: {
bar: { type: "string" },
baz: { type: "string" },
},
required: ["bar", "baz"],
};
const stringify = fastJson({
type: "object",
anyOf: [schemaFoo, schemaBar],
});
console.log(stringify({ foo: "foo", baz: "baz" }));
console.log(stringify({ bar: "bar", baz: "baz" }));
Output:
{}
{}
Expected behavior
Since both objects match the schema, both should be stringified.
Your Environment
- node version: v14.16.0
- fastify version: 3.13.0
- fast-json-stringify: 2.4.2
- os: Ubuntu 18.04
About this issue
- Original URL
- State: open
- Created 3 years ago
- Comments: 16 (7 by maintainers)
Commits related to this issue
- Ignore schema type if schame contains all of Fixes #290 — committed to cjsewell/fast-json-stringify by cjsewell 3 years ago
this schema works:
Defining the
type
beside theanyOf
would conflict when your data could be an object or a string like:This issue needs somebody to lead its implementation,
anyOf
andoneOf
have likely the same bugs.is there any support for discriminated union types?