@@ -390,7 +390,7 @@ func validateRelationTypeCondition(ctx context.Context, cfg *cond.CondCfg, prefi
390390}
391391
392392// validateScopeBindingMappingRules 校验 scope_binding 类型的 mapping_rules
393- func validateScopeBindingMappingRules (ctx context.Context , mappingRules any , _ bool ) (* interfaces.ScopeBindingMapping , error ) {
393+ func validateScopeBindingMappingRules (ctx context.Context , mappingRules any , strictMode bool ) (* interfaces.ScopeBindingMapping , error ) {
394394 var mapping interfaces.ScopeBindingMapping
395395 if err := mapstructure .Decode (mappingRules , & mapping ); err != nil {
396396 return nil , rest .NewHTTPError (ctx , http .StatusBadRequest , berrors .BknBackend_RelationType_InvalidParameter ).
@@ -399,44 +399,50 @@ func validateScopeBindingMappingRules(ctx context.Context, mappingRules any, _ b
399399
400400 // 校验 source 不能为空
401401 if mapping .Source == nil {
402- return nil , rest .NewHTTPError (ctx , http .StatusBadRequest , berrors .BknBackend_RelationType_InvalidParameter ).
403- WithErrorDetails ("scope_binding 的 source 不能为空" )
404- }
405-
406- // 校验 source.object_type_id 不能为空
407- if mapping .Source .ObjectTypeID == "" {
408- return nil , rest .NewHTTPError (ctx , http .StatusBadRequest , berrors .BknBackend_RelationType_InvalidParameter ).
409- WithErrorDetails ("scope_binding 的 source.object_type_id 不能为空" )
410- }
402+ if strictMode {
403+ return nil , rest .NewHTTPError (ctx , http .StatusBadRequest , berrors .BknBackend_RelationType_InvalidParameter ).
404+ WithErrorDetails ("scope_binding 的 source 不能为空" )
405+ }
406+ } else {
407+ // 校验 source.object_type_id 不能为空
408+ if mapping .Source .ObjectTypeID == "" {
409+ if strictMode {
410+ return nil , rest .NewHTTPError (ctx , http .StatusBadRequest , berrors .BknBackend_RelationType_InvalidParameter ).
411+ WithErrorDetails ("scope_binding 的 source.object_type_id 不能为空" )
412+ }
413+ }
411414
412- // 校验 source.filter(如果存在)
413- if mapping .Source .Condition != nil {
414- if err := validateRelationTypeCondition (ctx , mapping .Source .Condition , "source.filter" ); err != nil {
415- return nil , err
415+ // 校验 source.filter(如果存在)
416+ if mapping .Source .Condition != nil {
417+ if err := validateRelationTypeCondition (ctx , mapping .Source .Condition , "source.filter" ); err != nil {
418+ return nil , err
419+ }
416420 }
417421 }
418422
419423 // 校验 target_rules 不能为空
420424 if len (mapping .TargetRules ) == 0 {
421- return nil , rest .NewHTTPError (ctx , http .StatusBadRequest , berrors .BknBackend_RelationType_InvalidParameter ).
422- WithErrorDetails ("scope_binding 的 target_rules 不能为空" )
423- }
424-
425- // 校验每个 target_rule
426- for i , targetRule := range mapping .TargetRules {
427- if err := validateScopeBindingTargetRule (ctx , targetRule , i ); err != nil {
428- return nil , err
425+ if strictMode {
426+ return nil , rest .NewHTTPError (ctx , http .StatusBadRequest , berrors .BknBackend_RelationType_InvalidParameter ).
427+ WithErrorDetails ("scope_binding 的 target_rules 不能为空" )
429428 }
430-
431- // 校验 condition(如果存在)
432- if targetRule .Condition != nil {
433- if targetRule .Scope != interfaces .SCOPE_BINDING_SCOPE_OBJECT_TYPE {
434- return nil , rest .NewHTTPError (ctx , http .StatusBadRequest , berrors .BknBackend_RelationType_InvalidParameter ).
435- WithErrorDetails (fmt .Sprintf ("scope_binding 的 target_rules[%d].condition 仅支持 scope 为 object_type" , i ))
436- }
437- if err := validateRelationTypeCondition (ctx , targetRule .Condition , fmt .Sprintf ("target_rules[%d].condition" , i )); err != nil {
429+ } else {
430+ // 校验每个 target_rule
431+ for i , targetRule := range mapping .TargetRules {
432+ if err := validateScopeBindingTargetRule (ctx , targetRule , i ); err != nil {
438433 return nil , err
439434 }
435+
436+ // 校验 condition(如果存在)
437+ if targetRule .Condition != nil {
438+ if targetRule .Scope != interfaces .SCOPE_BINDING_SCOPE_OBJECT_TYPE {
439+ return nil , rest .NewHTTPError (ctx , http .StatusBadRequest , berrors .BknBackend_RelationType_InvalidParameter ).
440+ WithErrorDetails (fmt .Sprintf ("scope_binding 的 target_rules[%d].condition 仅支持 scope 为 object_type" , i ))
441+ }
442+ if err := validateRelationTypeCondition (ctx , targetRule .Condition , fmt .Sprintf ("target_rules[%d].condition" , i )); err != nil {
443+ return nil , err
444+ }
445+ }
440446 }
441447 }
442448
0 commit comments