Skip to content

[Bug] anyOf type should not output OneOf #241

@mrafnadeem-apimatic

Description

@mrafnadeem-apimatic

Is this a new bug?

  • I believe this is a new bug
  • I have searched the existing issues, and I could not find an existing issue for this bug

Current Behavior

In the schema library, in anyOf.ts, the type function is implemented as

type: () => `OneOf<${schemas.map((schema) => schema.type()).join(' | ')}>`

for both with and without discriminator.

Expected Behavior

It should be:

type: () => `AnyOf<${schemas.map((schema) => schema.type()).join(' | ')}>`

for both with and without discriminator.

Steps To Reproduce

  1. Using this setup
ts-test-app/
├── .gitignore
├── package-lock.json
├── package.json
├── tsconfig.json
└── src/
    └── app.ts

with this tsconfig.json:

{
  "compilerOptions": {
    "module": "NodeNext",                                /* Specify what module code is generated. */
    "rootDir": "./src",                                  /* Specify the root folder within your source files. */
    "outDir": "./dist",                                  /* Specify an output folder for all emitted files. */
    "verbatimModuleSyntax": true,                        /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
    "esModuleInterop": true,                             /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
    "forceConsistentCasingInFileNames": true,            /* Ensure that casing is correct in imports. */
    "strict": true,                                      /* Enable all strict type-checking options. */
    "skipLibCheck": true                                 /* Skip type checking all .d.ts files. */
  }
}

with this package.json:

{
  "name": "ts-test-app",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "devDependencies": {
    "typescript": "^5.8.3"
  },
  "dependencies": {
    "@apimatic/schema": "^0.7.16"
  },
  "type": "module"
}
  1. Run this code.
import { anyOf, number, object, string, validateAndMap } from "@apimatic/schema";

const anyOfSchema = anyOf([
    object({
        pawSize: ['pawSize', string()]
    }),
    object({
        age: ['age', number()]
    })
]);

const summary = validateAndMap({
    pawSize: 1
}, anyOfSchema);

if (summary.errors) {
    console.error(summary.errors);
}

Running this will output:

[
  {
    value: { pawSize: 1 },
    type: 'OneOf<Object<{pawSize}> | Object<{age}>>',
    branch: [ [Object] ],
    path: [],
    message: 'Could not match against any acceptable type.\n' +
      '\n' +
      'Given value: {"pawSize":1}\n' +
      "Type: 'object'\n" +
      "Expected type: 'OneOf<Object<{pawSize}> | Object<{age}>>'"
  }
]

But it should be AnyOf.

Environment

- **OS**: Windows 11
- **Node.js Version**: v22.14.0
- **TypeScript Version**: ^5.8.3
- **Package Manager & Version**: 10.9.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions