allOf Fails to Handle Array of if/then as Chained else Conditions

オープン
#778 コメント 5 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

@junaidkhan80121 がすでに取り組んでいます。

2026年11月8日 から。

  • #920 @junaidkhan80121 による — オープン

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
javascript
領域
backend

調査の方向性

fastJson(schema) エントリポイントと allOf/if/then の処理から始め、Node.js 22.14.0 を使用して、提供された再現例で fast-json-stringify の出力と AJV のバリデーションを比較します。完了条件は、両方の条件分岐が適用されることです。obj_1 は prop_1 を保持し、obj_2 は prop_2 を保持し、無効なケース obj_3 は有効なケースと区別可能なままである必要があります。

索引モデルが issue の本文から書いたものです。

説明

bug
Prerequisites
  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported
Fastify version

5.4.0

Plugin version

No response

Node.js version

22.14.0

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

15.5

Description

It appears that allOf cannot handle a sequence of if/then conditions as a chain of else clauses. This pattern would be extremely useful for scaling conditional validation logic, especially since libraries like AJV support it correctly.

When using allOf with an if/then block that includes array-based conditions, fast-json-stringify fails to serialize or apply logic as expected. AJV validates the same schema correctly.

When multiple if/then pairs are used inside allOf, only the first condition seems to be evaluated, and subsequent ones are ignored or misinterpreted.

Link to code that reproduces the bug
import fastJson from "fast-json-stringify";

const schema = {
  type: "object",
  properties: {
    kind: {
      enum: ["obj_1", "obj_2"],
    },
  },
  allOf: [
    {
      if: {
        properties: {
          kind: { type: "string", const: "obj_1" },
        },
      },
      then: {
        type: "object",
        properties: {
          kind: { type: "string" },
          prop_1: { type: "integer" },
        },
      },
    },
    {
      if: {
        properties: {
          kind: { type: "string", const: "obj_2" },
        },
      },
      then: {
        type: "object",
        properties: {
          kind: { type: "string" },
          prop_2: { type: "string" },
        },
      },
    },
  ],
  required: ["kind"],
};

doTest("obj 1", { kind: "obj_1", prop_1: 5 }, schema); // {"kind":"obj_1","prop_1":5} { ajv_valid: true }
doTest("obj 2", { kind: "obj_2", prop_2: "5" }, schema); // {"kind":"obj_2"} { ajv_valid: true }
doTest("obj 3", { kind: "obj_2", prop_2: 5 }, schema); // {"kind":"obj_2"} { ajv_valid: false }

function doTest(name, obj, schema) {
  const stringify = fastJson(schema);
  const validate = fastJson(obj, { mode: "debug" }).ajv.compile(schema);
  const ajv_valid = validate(obj);

  console.log(name, stringify(obj), { ajv_valid });
}
Expected Behavior

allOf should evaluate each if/then block in sequence, effectively allowing for a chain of mutually exclusive conditional branches (as a substitute for else if logic).

主要言語
JavaScript
スター
3.7k
フォーク
245
平均マージ
3日 5時間
マージ済み PR(30日)
16

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

fastify/fast-json-stringify のほかの issue

fastify/fast-json-stringify の issue をすべて見る

似ている issue

JavaScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。