Skip to content

Enforce snake_case validation for object field names#441

Merged
hotlong merged 2 commits into
mainfrom
copilot/add-field-name-format-constraint
Jan 31, 2026
Merged

Enforce snake_case validation for object field names#441
hotlong merged 2 commits into
mainfrom
copilot/add-field-name-format-constraint

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 31, 2026

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

  • ObjectSchema validation: Added key validator to fields record using z.record(z.string().regex(/^[a-z_][a-z0-9_]*$/), FieldSchema) to enforce snake_case at parse time
  • Test coverage: Added validation tests for PascalCase, camelCase, kebab-case, and other invalid patterns
  • Example fix: Updated msw-react-crud example to use correct field names

Example

Before (rejected):

const Lead = {
  name: 'lead',
  fields: {
    FirstName: { type: 'text', label: '名' },  // ❌ Validation error
    Company: { type: 'text', label: '公司' }
  }
};

After (accepted):

const Lead = {
  name: 'lead',
  fields: {
    first_name: { type: 'text', label: '名' },  // ✅ Valid
    company: { type: 'text', label: '公司' }
  }
};

Error message: Field names must be lowercase snake_case (e.g., "first_name", "company", "annual_revenue")

Original prompt

ai生成的对象是这样的,是否应该约束字段名的格式。
const Lead = {
name: 'lead',
label: '线索',
labelPlural: '线索',
icon: 'user-plus',
description: '潜在客户线索管理,包括线索打分、公海池和自动分配',
enable: {
searchable: true,
trackHistory: true,
activities: true,
feeds: true,
files: true,
enableDuplicateDetection: true
},
fields: {
// Basic Information
FirstName: {
type: 'text',
label: '名',
maxLength: 40
},
LastName: {
type: 'text',
label: '姓',
required: true,
maxLength: 80,
searchable: true
},
Company: {
type: 'text',
label: '公司',
required: true,
maxLength: 255,
searchable: true
},


💡 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.

@vercel
Copy link
Copy Markdown

vercel Bot commented Jan 31, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
spec Ready Ready Preview, Comment Jan 31, 2026 8:57am

Request Review

- 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>
@hotlong hotlong marked this pull request as ready for review January 31, 2026 08:33
Copilot AI review requested due to automatic review settings January 31, 2026 08:33
@github-actions github-actions Bot added documentation Improvements or additions to documentation protocol:data tests size/m labels Jan 31, 2026
Copilot AI changed the title [WIP] Add field name format constraint for lead object Enforce snake_case validation for object field names Jan 31, 2026
Copilot AI requested a review from hotlong January 31, 2026 08:36
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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_case for fields map keys in ObjectSchema via z.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_case field 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.

Comment on lines +220 to +222
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.'),
Copy link

Copilot AI Jan 31, 2026

Choose a reason for hiding this comment

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

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).

Copilot uses AI. Check for mistakes.
@hotlong hotlong merged commit 25a9747 into main Jan 31, 2026
15 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation protocol:data size/m tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants