Enforce snake_case validation for object field names#441
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
- Modified ObjectSchema to validate field keys must be snake_case - Added comprehensive tests for field name validation - Fixed msw-react-crud example to use snake_case field names - All 2892 tests passing Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Adds runtime validation to ensure Object field keys are lowercase snake_case, preventing AI-generated PascalCase/camelCase field identifiers from being accepted.
Changes:
- Enforced
snake_caseforfieldsmap keys inObjectSchemaviaz.record()key validation. - Added tests covering valid/invalid field key formats (including AI-generated PascalCase examples).
- Updated generated JSON Schema, docs, and the MSW React CRUD example to use
snake_casefield keys.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| packages/spec/src/data/object.zod.ts | Adds a regex constraint for fields record keys to enforce snake_case. |
| packages/spec/src/data/object.test.ts | Adds unit tests verifying snake_case acceptance and rejecting common invalid formats. |
| packages/spec/json-schema/data/Object.json | Updates generated JSON Schema with propertyNames.pattern for fields keys. |
| examples/msw-react-crud/objectstack.config.ts | Updates example object field keys to comply with the new constraint. |
| content/docs/references/data/object.mdx | Updates docs to state that fields keys must be snake_case. |
| fields: z.record(z.string().regex(/^[a-z_][a-z0-9_]*$/, { | ||
| message: 'Field names must be lowercase snake_case (e.g., "first_name", "company", "annual_revenue")', | ||
| }), FieldSchema).describe('Field definitions map. Keys must be snake_case identifiers.'), |
There was a problem hiding this comment.
The PR description says field.zod.ts was updated so field.name must match the corresponding key in fields when provided, but the current change only validates the fields object keys. As a result, configs like fields: { first_name: { name: 'FirstName', ... } } will still pass validation, which can create ambiguous/contradictory identifiers.
Consider adding an ObjectSchema-level refinement to enforce field.name === key when name is present (or update the PR description if that behavior is no longer intended).
AI-generated object definitions were using PascalCase field names (
FirstName,LastName,Company) instead of the required snake_case convention (first_name,last_name,company), causing inconsistency with the platform's naming standards.Changes
fieldsrecord usingz.record(z.string().regex(/^[a-z_][a-z0-9_]*$/), FieldSchema)to enforce snake_case at parse timemsw-react-crudexample to use correct field namesExample
Before (rejected):
After (accepted):
Error message:
Field names must be lowercase snake_case (e.g., "first_name", "company", "annual_revenue")Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.