@@ -654,11 +654,8 @@ private void iterate(FieldMap map, String msgType, DataDictionary dd) throws Inc
654654 checkValue (field );
655655 }
656656
657- if (beginString != null && shouldCheckTag (field )) {
658- dd .checkValidTagNumber (field );
659- if (map instanceof Message ) {
660- checkIsInMessage (field , msgType );
661- }
657+ if (beginString != null ) {
658+ dd .checkField (field , msgType , map instanceof Message );
662659 dd .checkGroupCount (field , map , msgType );
663660 }
664661 }
@@ -680,17 +677,33 @@ private void checkMsgType(String msgType) {
680677 }
681678 }
682679
683- // / If we need to check for the tag in the dictionary
684- private boolean shouldCheckTag (Field <?> field ) {
685- return checkUserDefinedFields || field .getField () < USER_DEFINED_TAG_MIN ;
686- }
687-
688680 // / Check if field tag number is defined in spec.
689681 void checkValidTagNumber (Field <?> field ) {
690682 if (!fields .contains (Integer .valueOf (field .getTag ()))) {
691683 throw new FieldException (SessionRejectReason .INVALID_TAG_NUMBER , field .getField ());
692684 }
693685 }
686+
687+ // / Check if field tag is defined for message or group
688+ void checkField (Field <?> field , String msgType , boolean message ) {
689+ // use different validation for groups and messages
690+ boolean messageField = message ? isMsgField (msgType , field .getField ()) : fields .contains (field .getField ());
691+ boolean fail ;
692+
693+ if (field .getField () < USER_DEFINED_TAG_MIN ) {
694+ fail = !messageField && !allowUnknownMessageFields ;
695+ } else {
696+ fail = !messageField && checkUserDefinedFields ;
697+ }
698+
699+ if (fail ) {
700+ if (fields .contains (Integer .valueOf (field .getTag ()))) {
701+ throw new FieldException (SessionRejectReason .TAG_NOT_DEFINED_FOR_THIS_MESSAGE_TYPE , field .getField ());
702+ } else {
703+ throw new FieldException (SessionRejectReason .INVALID_TAG_NUMBER , field .getField ());
704+ }
705+ }
706+ }
694707
695708 private void checkValidFormat (StringField field ) throws IncorrectDataFormat {
696709 try {
@@ -773,14 +786,6 @@ private void checkHasValue(StringField field) {
773786 }
774787 }
775788
776- // / Check if a field is in this message type.
777- private void checkIsInMessage (Field <?> field , String msgType ) {
778- if (!isMsgField (msgType , field .getField ()) && !allowUnknownMessageFields ) {
779- throw new FieldException (SessionRejectReason .TAG_NOT_DEFINED_FOR_THIS_MESSAGE_TYPE ,
780- field .getField ());
781- }
782- }
783-
784789 // / Check if group count matches number of groups in
785790 private void checkGroupCount (StringField field , FieldMap fieldMap , String msgType ) {
786791 final int fieldNum = field .getField ();
0 commit comments