Skip to content

feat: support external JSON schema file references in type generation#14749

Merged
GermanJablo merged 1 commit into
payloadcms:mainfrom
tak-amboss:feat-external-schema-refs
Nov 27, 2025
Merged

feat: support external JSON schema file references in type generation#14749
GermanJablo merged 1 commit into
payloadcms:mainfrom
tak-amboss:feat-external-schema-refs

Conversation

@tak-amboss
Copy link
Copy Markdown
Contributor

Description

Enables the use of external JSON schema file references in type generation by adding the cwd parameter to json-schema-to-typescript's compile() function.

Changes

  • Added cwd: process.cwd() parameter to generateTypes() compile call
  • Enables $ref pointers to external .json schema files in typescriptSchema field config
  • Added test case with external schema file reference
  • Documented external schema reference usage in generating-types.mdx

Test Coverage

✅ New test: resolves external schema file references

  • Created test/types/schemas/custom-type.json as external schema
  • Added field with typescriptSchema referencing the external file
  • Verified generated types include the external type correctly

Example Usage

// payload.config.ts
{
  typescript: {
    schema: [
      ({ jsonSchema }) => {
        jsonSchema.definitions.MyType = {
          $ref: './schemas/my-type.json'
        }
        return jsonSchema
      },
    ]
  }
}

External references are resolved relative to process.cwd().

Related Issue

This was identified during investigation of JSON Schema parser capabilities. The cwd parameter is a standard json-schema-to-typescript option that was not being passed.

- Add cwd parameter to json-schema-to-typescript compile() call
- Enables  to external .json schema files in typescriptSchema
- Add test with external schema file reference
- Document external schema reference usage
Copy link
Copy Markdown
Contributor

@GermanJablo GermanJablo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense to me! Thank you!

@GermanJablo GermanJablo merged commit cb3a078 into payloadcms:main Nov 27, 2025
98 checks passed
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Dec 3, 2025

🚀 This is included in version v3.66.0

zubricks pushed a commit that referenced this pull request Jan 6, 2026
…#14749)

## Description

Enables the use of external JSON schema file references in type
generation by adding the `cwd` parameter to
`json-schema-to-typescript`'s `compile()` function.

## Changes

- Added `cwd: process.cwd()` parameter to `generateTypes()` compile call
- Enables `$ref` pointers to external `.json` schema files in
`typescriptSchema` field config
- Added test case with external schema file reference
- Documented external schema reference usage in generating-types.mdx

## Test Coverage

✅ New test: `resolves external schema file references`
- Created `test/types/schemas/custom-type.json` as external schema
- Added field with `typescriptSchema` referencing the external file
- Verified generated types include the external type correctly

## Example Usage

```typescript
// payload.config.ts
{
  typescript: {
    schema: [
      ({ jsonSchema }) => {
        jsonSchema.definitions.MyType = {
          $ref: './schemas/my-type.json'
        }
        return jsonSchema
      },
    ]
  }
}
```

External references are resolved relative to `process.cwd()`.

## Related Issue

This was identified during investigation of JSON Schema parser
capabilities. The `cwd` parameter is a standard
`json-schema-to-typescript` option that was not being passed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants