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

未关闭
#778 5 条评论 0 个 reaction 已指派 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 小时
30 天内合并 PR
16

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

fastify/fast-json-stringify 的其他 Issue

查看 fastify/fast-json-stringify 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。