fastify/fast-json-stringify

oneOf/anyOf doesn't seem to work with objects

Aberta

#290 aberto em 6 de mar. de 2021

 (18 comentários) (0 reação) (0 responsável)JavaScript (211 forks)github user discovery
buggood first issue

Métricas do repositório

Stars
 (3.700 estrelas)
Métricas de merge de PR
 (Mesclagem média 8d 13h) (6 fundiu PRs em 30d)

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:

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

Guia do colaborador