fastify / fastify/fast-json-stringify

oneOf/anyOf doesn't seem to work with objects

Open
#290 18 comments 0 reactions 0 assignees View on GitHub
bug good first issue
Dominant language
JavaScript
Stars
3.7k
Forks
226
Avg merge
1d 59m
Merged PRs (30d)
3

Description

## 🐛 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:

```ts
type Foo = { foo: string, baz: string };
type Bar = { bar: string, baz: string };
type FooBar = Foo | Bar;
```

```js
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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.