@@ -2768,6 +2768,18 @@ function refuseDuplicateActionNames(objectName: unknown, actions: unknown, ctx:
27682768 }
27692769}
27702770
2771+ /**
2772+ * The key grammar of an object's field map: a field name is lowercase
2773+ * snake_case. ONE schema, read by every record keyed by a field name of an
2774+ * object — `ObjectSchema.fields` and `ObjectExtensionSchema.fieldPatches`
2775+ * (#29366) — so a key the field map can never hold is refused where it is
2776+ * written, never accepted as a patch that no field can match. Declared above
2777+ * `ObjectSchemaBase` for the order reason the block below gives.
2778+ */
2779+ const FieldNameKeySchema = z . string ( ) . regex ( / ^ [ a - z _ ] [ a - z 0 - 9 _ ] * $ / , {
2780+ message : 'Field names must be lowercase snake_case (e.g., "first_name", "company", "annual_revenue")' ,
2781+ } ) ;
2782+
27712783// ⚠️ ORDER IS LOAD-BEARING (#5593). This map used to live ~700 lines BELOW
27722784// `ObjectSchemaBase`, and the error map that reads it was built lazily
27732785// (`objectUnknownKeyErrorImpl ??= …`) purely to step around the temporal dead
@@ -3189,13 +3201,7 @@ const ObjectSchemaBase = strictObject(
31893201 message : 'Field names must not be "constructor" or "prototype" (reserved JavaScript prototype property names).' ,
31903202 path : [ reserved ] ,
31913203 } ) ,
3192- z . record (
3193- z . string ( )
3194- . regex ( / ^ [ a - z _ ] [ a - z 0 - 9 _ ] * $ / , {
3195- message : 'Field names must be lowercase snake_case (e.g., "first_name", "company", "annual_revenue")' ,
3196- } ) ,
3197- FieldSchema ,
3198- ) ,
3204+ z . record ( FieldNameKeySchema , FieldSchema ) ,
31993205 ) ,
32003206 'fields' ,
32013207 ) . describe ( 'Field definitions map. Keys must be snake_case identifiers; "__proto__", "constructor" and "prototype" are refused.' ) ,
@@ -4933,7 +4939,8 @@ function refuseFieldPatchOfReplacedField(fields: unknown, fieldPatches: unknown,
49334939/** [ADR-0146 D10] The `fieldPatches` description, its member list read from {@link BASE_FIELD_DISPLAY_WHITELIST}. */
49344940function fieldPatchesDescription ( ) : string {
49354941 return (
4936- 'Display patches of the target object\'s own fields, keyed by field name: change a vendor field\'s ' +
4942+ 'Display patches of the target object\'s own fields, keyed by field name (lowercase snake_case, the ' +
4943+ 'key grammar of an object\'s `fields`): change a vendor field\'s ' +
49374944 `display properties (BASE_FIELD_DISPLAY_WHITELIST: ${ backtickList ( BASE_FIELD_DISPLAY_WHITELIST . field ) } ` +
49384945 `on the field, ${ backtickList ( BASE_FIELD_DISPLAY_WHITELIST . option ) } on an option under \`options\`, ` +
49394946 'keyed by its stored value) without taking the field over, so its type, values and rules stay the ' +
@@ -5030,7 +5037,9 @@ export const ObjectExtensionSchema = lazySchema(() => strictObject({
50305037
50315038 /**
50325039 * [ADR-0146 D10, second rule] Per-field display patch of the target's own
5033- * fields. Keys are DERIVED from `BASE_FIELD_DISPLAY_WHITELIST`; structural
5040+ * fields. Each record key is a field name, judged by the field map's own
5041+ * key grammar ({@link FieldNameKeySchema}, as `ObjectSchema.fields`). A
5042+ * patch's keys are DERIVED from `BASE_FIELD_DISPLAY_WHITELIST`; structural
50345043 * keys are refused by name with the D6 prescription; an `__c` key, and a
50355044 * field the same extension replaces in `fields`, are refused by the root
50365045 * refinement below; `__proto__` is refused on the raw input. Pure shape: whether the target has the field
@@ -5039,7 +5048,19 @@ export const ObjectExtensionSchema = lazySchema(() => strictObject({
50395048 * `os validate`, named in the boot log — and the merge that applies the patch
50405049 * is `@objectstack/objectql`'s `mergeObjectDefinitions`.
50415050 */
5042- fieldPatches : refuseRecordProtoKey ( z . record ( z . string ( ) , FieldDisplayPatchSchema ) , 'fieldPatches' )
5051+ fieldPatches : refuseRecordProtoKey ( z . record ( FieldNameKeySchema , FieldDisplayPatchSchema , {
5052+ // [#29366] Keyed by the field map's own key grammar: a key no field name
5053+ // can be (`Industry`, `Annual Revenue`) is refused here, where it is
5054+ // written, instead of parsing and reaching only the target judge.
5055+ error : ( issue ) => {
5056+ if ( issue . code === 'invalid_key' ) {
5057+ return `\`${ String ( issue . input ) } \` cannot be a field name, so no field sits under it: \`fieldPatches\` is ` +
5058+ 'keyed by the name of a field the extended object declares, lowercase snake_case such as ' +
5059+ '`industry` or `annual_revenue` (the key of its `fields` entry), never by its label.' ;
5060+ }
5061+ return undefined ;
5062+ } ,
5063+ } ) , 'fieldPatches' )
50435064 . optional ( )
50445065 . describe ( fieldPatchesDescription ( ) ) ,
50455066
0 commit comments