@@ -34,6 +34,12 @@ import { RLS_PREDICATE_UNKNOWN_FIELD, validateRlsPredicateEnforceability } from
3434import { SHARING_RULE_UNLOWERABLE_CONDITION , validateSharingRuleEnforceability } from './validate-sharing-rule-enforceability.js' ;
3535import { THANK_YOU_RECORD_FIELD , validateThankYouRecordFields } from './validate-thank-you-record-fields.js' ;
3636import { MEASURE_AGGREGATE_INCOHERENT , WIDGET_FILTER_FIELD_UNKNOWN , validateWidgetBindings } from './validate-widget-bindings.js' ;
37+ import { validateStackExpressions } from './validate-expressions.js' ;
38+ import { FLOW_NODE_WRITE_UNKNOWN_FIELD , validateFlowNodeWrites } from './validate-flow-node-writes.js' ;
39+ import { ACTION_BODY_WRITE_UNKNOWN_FIELD , validateActionBodyWrites } from './validate-action-body-writes.js' ;
40+ import { HOOK_BODY_WRITE_UNKNOWN_FIELD , validateHookBodyWrites } from './validate-hook-body-writes.js' ;
41+ import { validateReactPageProps } from './validate-react-page-props.js' ;
42+ import { TRANSLATION_TARGET_UNKNOWN , validateTranslationReferences } from './validate-translation-references.js' ;
3743// [#16340] Read back the registry's OWN definition table to assert the graph
3844// derives its injected types rather than carrying a second copy of them.
3945import { injectedSystemColumnDefs } from '@objectstack/spec/data' ;
@@ -805,3 +811,136 @@ describe('[#30383] the field-path rules read the objects as served', () => {
805811 } ) ;
806812 } ) ;
807813} ) ;
814+
815+ // [#30471] The field-NAME indexes outside the graph read the same fold: the
816+ // `record.FIELD` index of the expression passes (validations, formulas, flows,
817+ // hooks, sharing-rule conditions), the write rules' index (flow nodes, action
818+ // and hook bodies), a react page's field props and the translation universe.
819+ // The #30383 artifact, plus one extension rule for the translation rung.
820+ describe ( '[#30471] the field-name indexes read the objects as served' , ( ) => {
821+ type Rec = Record < string , unknown > ;
822+ type Finding = { rule : string ; severity ?: unknown ; where ?: unknown ; path ?: unknown ; message ?: unknown } ;
823+ type Run = ( stack : Rec ) => ReadonlyArray < Finding > ;
824+ const deal = { name : 'xs_deal' , label : 'Deal' , fields : { name : { type : 'text' } , amount : { type : 'number' } } } ;
825+ const account = { name : 'xs_account' , label : 'Account' , fields : { name : { type : 'text' } } } ;
826+ const added = { region : { type : 'text' } , score : { type : 'number' } } ;
827+ const extensions = [ {
828+ extend : 'xs_deal' ,
829+ fields : added ,
830+ validations : [ { name : 'region_set' , type : 'script' , condition : 'record.region == null' , message : 'Set a region' } ] ,
831+ } ] ;
832+ const artifact = ( extra : Rec , objectExtensions : unknown [ ] = extensions , objects : unknown [ ] = [ deal , account ] ) : Rec => ( {
833+ objects,
834+ packages : [
835+ { manifest : { id : 'app' , objects } } ,
836+ { manifest : { id : 'mod' , objectExtensions } } ,
837+ ] ,
838+ ...extra ,
839+ } ) ;
840+ // The per-package leg of `os build` (`packageBodyAsStack` in the cli).
841+ const perPackage = ( extra : Rec , objectExtensions : unknown [ ] = extensions ) : Rec => {
842+ const body = { id : 'app' , objects : [ deal , account ] , ...extra } ;
843+ return { ...body , manifest : body , packages : [ { manifest : body } , { manifest : { id : 'mod' , objectExtensions } } ] } ;
844+ } ;
845+ const ids = ( run : Run , stack : Rec ) => run ( stack ) . map ( ( f ) => f . rule ) ;
846+ // The expression passes carry no rule id: every issue they raise on these
847+ // stacks is about the one expression each names, read as `expression`.
848+ const expressions : Run = ( stack ) => validateStackExpressions ( stack ) . map ( ( issue ) => ( { ...issue , rule : 'expression' } ) ) ;
849+ const validationOn = ( condition : string ) : Rec => ( {
850+ objects : [ { ...deal , validations : [ { name : 'v' , type : 'script' , condition, message : 'M' } ] } , account ] ,
851+ } ) ;
852+
853+ // rule · its unknown-field id · the stack naming FIELD
854+ const existence : Array < [ string , string , Run , ( field : string ) => Rec ] > = [
855+ [ 'validate-expressions (a validation condition)' , 'expression' , expressions , ( f ) => validationOn ( `record.${ f } == 'x'` ) ] ,
856+ [ 'validate-expressions (a sharing-rule condition)' , 'expression' , expressions , ( f ) => ( {
857+ sharingRules : [ { name : 'sr' , object : 'xs_deal' , condition : `record.${ f } == 'x'` , sharedWith : { type : 'role' , value : 'r' } , accessLevel : 'read' } ] ,
858+ } ) ] ,
859+ [ 'validate-flow-node-writes' , FLOW_NODE_WRITE_UNKNOWN_FIELD , validateFlowNodeWrites , ( f ) => ( {
860+ flows : [ { name : 'fl' , nodes : [ { id : 'n' , type : 'update_record' , config : { objectName : 'xs_deal' , fields : { [ f ] : 'x' } } } ] } ] ,
861+ } ) ] ,
862+ [ 'validate-action-body-writes' , ACTION_BODY_WRITE_UNKNOWN_FIELD , validateActionBodyWrites , ( f ) => ( {
863+ actions : [ { name : 'act' , label : 'Act' , body : { language : 'js' , source : `await ctx.api.object('xs_deal').updateById(id, { ${ f } : 'x' });` } } ] ,
864+ } ) ] ,
865+ [ 'validate-hook-body-writes' , HOOK_BODY_WRITE_UNKNOWN_FIELD , ( stack ) => validateHookBodyWrites ( stack ) , ( f ) => ( {
866+ hooks : [ { name : 'h' , object : 'xs_deal' , events : [ 'beforeInsert' ] , body : { language : 'js' , source : `ctx.input.${ f } = 'x';` } } ] ,
867+ } ) ] ,
868+ [ 'validate-react-page-props' , PAGE_FIELD_UNKNOWN , validateReactPageProps , ( f ) => ( {
869+ pages : [ {
870+ name : 'p' , kind : 'react' ,
871+ source : `function Page(){ return <ListView data={{ provider: 'object', object: 'xs_deal' }} columns={['name', '${ f } ']} />; }` ,
872+ } ] ,
873+ } ) ] ,
874+ [ 'validate-translation-references' , TRANSLATION_TARGET_UNKNOWN , validateTranslationReferences , ( f ) => ( {
875+ translations : [ { 'zh-CN' : { objects : { xs_deal : { fields : { [ f ] : { label : 'L' } } } } } } ] ,
876+ } ) ] ,
877+ ] ;
878+
879+ it . each ( existence ) ( '%s: an extension-added field passes; a field nothing adds is still refused' , ( _rule , id , run , naming ) => {
880+ expect ( ids ( run , artifact ( naming ( 'region' ) ) ) ) . not . toContain ( id ) ;
881+ expect ( ids ( run , artifact ( naming ( 'region' ) , [ ] ) ) ) . toContain ( id ) ;
882+ expect ( ids ( run , artifact ( naming ( 'nowhere' ) ) ) ) . toContain ( id ) ;
883+ } ) ;
884+
885+ it . each ( existence ) ( '%s, on the per-package leg: the same verdicts' , ( _rule , id , run , naming ) => {
886+ expect ( ids ( run , perPackage ( naming ( 'region' ) ) ) ) . not . toContain ( id ) ;
887+ expect ( ids ( run , perPackage ( naming ( 'region' ) , [ ] ) ) ) . toContain ( id ) ;
888+ expect ( ids ( run , perPackage ( naming ( 'nowhere' ) ) ) ) . toContain ( id ) ;
889+ } ) ;
890+
891+ it . each ( existence ) ( '%s: a field nothing adds is refused byte-for-byte as on the declared objects alone' , ( _rule , id , run , naming ) => {
892+ const refusal = ( stack : Rec ) => run ( stack )
893+ . filter ( ( f ) => f . rule === id )
894+ . map ( ( { severity, where, path, message } ) => ( { severity, where, path, message } ) ) ;
895+ const served = refusal ( artifact ( naming ( 'zq_absent' ) ) ) ;
896+ expect ( served ) . toHaveLength ( 1 ) ;
897+ expect ( served ) . toEqual ( refusal ( artifact ( naming ( 'zq_absent' ) , [ ] ) ) ) ;
898+ } ) ;
899+
900+ it ( 'validate-translation-references: a `_validations` key for a rule an extension adds resolves' , ( ) => {
901+ const bundle = ( rule : string ) => ( { translations : [ { 'zh-CN' : { objects : { xs_deal : { _validations : { [ rule ] : { message : 'M' } } } } } } ] } ) ;
902+ expect ( ids ( validateTranslationReferences , artifact ( bundle ( 'region_set' ) ) ) ) . toEqual ( [ ] ) ;
903+ expect ( ids ( validateTranslationReferences , artifact ( bundle ( 'region_set' ) , [ ] ) ) ) . toEqual ( [ TRANSLATION_TARGET_UNKNOWN ] ) ;
904+ expect ( ids ( validateTranslationReferences , artifact ( bundle ( 'nowhere' ) ) ) ) . toEqual ( [ TRANSLATION_TARGET_UNKNOWN ] ) ;
905+ } ) ;
906+
907+ // The retired private reader kept every entry; the fold keeps one per
908+ // package per target, so a later entry of the same package replaces an
909+ // earlier one whole, its rules with its fields.
910+ it ( 'validate-translation-references: a key only a replaced entry adds is an orphan, as the registry serves it' , ( ) => {
911+ const replaced = [ ...extensions , { extend : 'xs_deal' , fields : { mood : { type : 'text' } } } ] ;
912+ const keys = ( fields : Rec , rules : Rec = { } ) => ( { translations : [ { 'zh-CN' : { objects : { xs_deal : { fields, _validations : rules } } } } ] } ) ;
913+ expect ( ids ( validateTranslationReferences , artifact ( keys ( { mood : { label : 'M' } } ) , replaced ) ) ) . toEqual ( [ ] ) ;
914+ expect ( ids ( validateTranslationReferences , artifact ( keys ( { region : { label : 'R' } } ) , replaced ) ) ) . toEqual ( [ TRANSLATION_TARGET_UNKNOWN ] ) ;
915+ expect ( ids ( validateTranslationReferences , artifact ( keys ( { } , { region_set : { message : 'M' } } ) , replaced ) ) ) . toEqual ( [ TRANSLATION_TARGET_UNKNOWN ] ) ;
916+ } ) ;
917+
918+ // The write rules leave an object that declares no field unjudged (an
919+ // external, introspected schema): an extension adding to it does not make
920+ // its other fields knowable.
921+ it ( 'the write rules keep an object that declares no field unjudged, extended or not' , ( ) => {
922+ const bare = { name : 'xs_deal' , label : 'Deal' , fields : { } } ;
923+ const write = { flows : [ { name : 'fl' , nodes : [ { id : 'n' , type : 'update_record' , config : { objectName : 'xs_deal' , fields : { remote_col : 'x' } } } ] } ] } ;
924+ expect ( ids ( validateFlowNodeWrites , artifact ( write , extensions , [ bare , account ] ) ) ) . toEqual ( [ ] ) ;
925+ expect ( ids ( validateFlowNodeWrites , artifact ( write , [ ] , [ bare , account ] ) ) ) . toEqual ( [ ] ) ;
926+ } ) ;
927+
928+ // A field two packages write at the deciding priority: its name is served,
929+ // its definition is registration order. The expression type check leaves it
930+ // untyped; each pair lists last the definition the check would refuse.
931+ it ( 'validate-expressions: a field two packages tie on is a served name with no type' , ( ) => {
932+ const tie = ( a : Rec , b : Rec , extra : Rec ) : Rec => ( {
933+ objects : [ deal , account ] ,
934+ packages : [
935+ { manifest : { id : 'app' , objects : [ deal , account ] } } ,
936+ { manifest : { id : 'mod' , objectExtensions : [ { extend : 'xs_deal' , fields : a } ] } } ,
937+ { manifest : { id : 'mod2' , objectExtensions : [ { extend : 'xs_deal' , fields : b } ] } } ,
938+ ] ,
939+ ...extra ,
940+ } ) ;
941+ const sum = validationOn ( 'record.score + 1 > 2' ) ;
942+ expect ( ids ( expressions , tie ( { score : { type : 'number' } } , { score : { type : 'text' } } , sum ) ) ) . toEqual ( [ ] ) ;
943+ expect ( ids ( expressions , tie ( { score : { type : 'text' } } , { } , sum ) ) ) . toEqual ( [ 'expression' ] ) ;
944+ expect ( ids ( expressions , tie ( { score : { type : 'number' } } , { } , sum ) ) ) . toEqual ( [ ] ) ;
945+ } ) ;
946+ } ) ;
0 commit comments