Skip to content

feat: strict types - #2792

Open
sanny-io wants to merge 5 commits into
zenstackhq:devfrom
sanny-io:feat/strict-types
Open

feat: strict types#2792
sanny-io wants to merge 5 commits into
zenstackhq:devfrom
sanny-io:feat/strict-types

Conversation

@sanny-io

@sanny-io sanny-io commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Adds support for specifying custom type definitions should reject unknown fields via a new @@strict attribute. This is reflected in both intellisense and runtime input validation.

Sample Usage

model User {
    id      String   @id @default(uuid())
    profile Profile? @json
}

type Profile {
    bio String

    @@strict
}

Why not @json(strict: true)? A type-level @@strict attribute is simpler to determine strictness, and also works with custom procedures.

Summary by CodeRabbit

  • New Features

    • Added the @@strict attribute for type definitions.
    • Strict types now reject unknown fields in JSON data and API operations.
    • Generated schemas and client types preserve strictness across supported clients.
    • Added profile support and an updateProfile operation in the example API.
  • Bug Fixes

    • Non-strict types continue allowing additional fields.
    • Invalid use of @@strict on models or enums now reports a validation error.

@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Adds @@strict for type definitions, propagates strict metadata into generated schemas, enforces unknown-field rejection in ORM validation and types, and adds client, procedure, and typed JSON tests.

Changes

Strict type definition support

Layer / File(s) Summary
Define and validate @@strict
packages/language/res/stdlib.zmodel, packages/language/src/validators/attribute-application-validator.ts, packages/language/test/attribute-application.test.ts
Declares @@strict() and validates that it applies only to type definitions.
Propagate strict schema metadata
packages/schema/src/schema.ts, packages/sdk/src/ts-schema-generator.ts, packages/cli/test/ts-schema-gen.test.ts
Adds optional strict metadata to type definition schemas and generates strict: true for annotated definitions.
Enforce strictness in ORM types and schemas
packages/orm/src/client/crud-types.ts, packages/orm/src/client/zod/factory.ts
Rejects unknown fields for strict type definitions and conditionally permits extra properties for non-strict definitions.
Exercise strict typed JSON flows
tests/e2e/orm/schemas/procedures/schema.zmodel, tests/e2e/orm/schemas/procedures/schema.ts, tests/e2e/orm/client-api/procedures.test.ts, tests/e2e/orm/client-api/typed-json-fields.test.ts
Adds the strict Profile type, the JSON-backed User.profile field, the updateProfile mutation, and rejection tests for unknown fields.
Update client custom type coverage
packages/clients/fetch-client/test/schemas/basic/*, packages/clients/fetch-client/test/fetch-client.test.ts, packages/clients/fetch-client/test/typing.test-d.ts, packages/clients/fetch-client/test/schemas/no-procs/schema.ts, packages/clients/tanstack-query/test/schemas/basic/*, packages/clients/tanstack-query/test/react/react-typing.test-d.ts, packages/clients/tanstack-query/test/svelte/svelte-typing-test.ts, packages/clients/tanstack-query/test/vue/vue-typing-test.ts
Updates client schemas and typing tests to accept known custom fields and reject unknown nested properties.

Estimated code review effort: 3 (Moderate) | ~30 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately identifies the main change: support for strict custom types through the new @@strict attribute.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/orm/src/client/zod/factory.ts`:
- Around line 465-467: Update the strictness selection in the schema factory
around isStrict to use typeDef.strict === true instead of scanning
typeDef.attributes for @@strict. Preserve the existing z.strictObject and
z.looseObject selection so runtime validation matches the shared TypeDefDef
strictness contract.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 6bdf47c3-47d3-460c-8073-cbee101456f1

📥 Commits

Reviewing files that changed from the base of the PR and between 797b5bc and acc4141.

📒 Files selected for processing (12)
  • packages/cli/test/ts-schema-gen.test.ts
  • packages/language/res/stdlib.zmodel
  • packages/language/src/validators/attribute-application-validator.ts
  • packages/language/test/attribute-application.test.ts
  • packages/orm/src/client/crud-types.ts
  • packages/orm/src/client/zod/factory.ts
  • packages/schema/src/schema.ts
  • packages/sdk/src/ts-schema-generator.ts
  • tests/e2e/orm/client-api/procedures.test.ts
  • tests/e2e/orm/client-api/typed-json-fields.test.ts
  • tests/e2e/orm/schemas/procedures/schema.ts
  • tests/e2e/orm/schemas/procedures/schema.zmodel

Comment thread packages/orm/src/client/zod/factory.ts Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/clients/tanstack-query/test/schemas/basic/schema.zmodel`:
- Around line 38-41: Make Profile.bio optional by changing its ZModel
declaration to use the optional string type in
packages/clients/tanstack-query/test/schemas/basic/schema.zmodel:38-41. Then run
zenstack generate to regenerate
packages/clients/tanstack-query/test/schemas/basic/schema-lite.ts:174-188,
ensuring Profile.fields.bio.optional is true.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: ac00b7a9-3be4-49b9-91af-1461a683f680

📥 Commits

Reviewing files that changed from the base of the PR and between 3adc94d and c84d978.

📒 Files selected for processing (10)
  • packages/clients/fetch-client/test/fetch-client.test.ts
  • packages/clients/fetch-client/test/schemas/basic/schema-lite.ts
  • packages/clients/fetch-client/test/schemas/basic/schema.zmodel
  • packages/clients/fetch-client/test/schemas/no-procs/schema.ts
  • packages/clients/fetch-client/test/typing.test-d.ts
  • packages/clients/tanstack-query/test/react/react-typing.test-d.ts
  • packages/clients/tanstack-query/test/schemas/basic/schema-lite.ts
  • packages/clients/tanstack-query/test/schemas/basic/schema.zmodel
  • packages/clients/tanstack-query/test/svelte/svelte-typing-test.ts
  • packages/clients/tanstack-query/test/vue/vue-typing-test.ts

Comment thread packages/clients/tanstack-query/test/schemas/basic/schema.zmodel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant