1- using System . Text . RegularExpressions ;
1+ using Enigmatry . Entry . CodeGeneration . Validation . ValidationRules ;
22using FluentAssertions ;
33using Humanizer ;
44using NUnit . Framework ;
@@ -11,7 +11,7 @@ public class ValidationConfigurationFixture
1111 [ Test ]
1212 public void ValidationConfiguration ( )
1313 {
14- var validationConfiguration = new MockValidationModelConfiguration ( ) ;
14+ var validationConfiguration = new ModelConfiguration ( ) ;
1515
1616 validationConfiguration . ValidationRules
1717 . Where ( x => x . PropertyName == nameof ( ValidationMockModel . IntField ) . Camelize ( ) )
@@ -39,23 +39,23 @@ public void ValidationConfiguration()
3939 }
4040
4141 [ TestCase ( nameof ( ValidationMockModel . IntField ) , "required" , "" , "validators.required" ) ]
42- [ TestCase ( nameof ( ValidationMockModel . IntField ) , "min" , MockValidationModelConfiguration . CustomMessage , "" ) ]
43- [ TestCase ( nameof ( ValidationMockModel . IntField ) , "max" , MockValidationModelConfiguration . CustomMessage , MockValidationModelConfiguration . CustomMessageTranlsationId ) ]
44- [ TestCase ( nameof ( ValidationMockModel . StringField ) , "required" , MockValidationModelConfiguration . CustomMessage , MockValidationModelConfiguration . CustomMessageTranlsationId ) ]
45- [ TestCase ( nameof ( ValidationMockModel . StringField ) , "minLength" , MockValidationModelConfiguration . CustomMessage , "" ) ]
42+ [ TestCase ( nameof ( ValidationMockModel . IntField ) , "min" , ModelConfiguration . CustomMessage , "" ) ]
43+ [ TestCase ( nameof ( ValidationMockModel . IntField ) , "max" , ModelConfiguration . CustomMessage , ModelConfiguration . CustomMessageTranslationId ) ]
44+ [ TestCase ( nameof ( ValidationMockModel . StringField ) , "required" , ModelConfiguration . CustomMessage , ModelConfiguration . CustomMessageTranslationId ) ]
45+ [ TestCase ( nameof ( ValidationMockModel . StringField ) , "minLength" , ModelConfiguration . CustomMessage , "" ) ]
4646 [ TestCase ( nameof ( ValidationMockModel . StringField ) , "maxLength" , "" , "validators.maxLength" ) ]
4747 public void ValidationConfigurationPerValidationRule (
4848 string propertyName ,
4949 string validationRuleName ,
5050 string validationMessage ,
5151 string validationMessageTranslationId )
5252 {
53- var validationConfiguration = new MockValidationModelConfiguration ( ) ;
53+ var validationConfiguration = new ModelConfiguration ( ) ;
5454
5555 validationConfiguration . ValidationRules
5656 . Where ( x => x . PropertyName == propertyName . Camelize ( ) )
5757 . Should ( ) . NotBeNullOrEmpty ( ) ;
58- var validationRule = validationConfiguration . ValidationRules
58+ IFormlyValidationRule ? validationRule = validationConfiguration . ValidationRules
5959 . Where ( x => x . PropertyName == propertyName . Camelize ( ) )
6060 . SingleOrDefault ( rule => rule . FormlyRuleName == validationRuleName ) ;
6161 validationRule . Should ( ) . NotBeNull ( ) ;
@@ -66,7 +66,7 @@ public void ValidationConfigurationPerValidationRule(
6666 [ Test ]
6767 public void ValidationConfigurationForPatterns ( )
6868 {
69- var validationConfiguration = new MockValidationModelWithPatternsConfiguration ( ) ;
69+ var validationConfiguration = new PatternsConfiguration ( ) ;
7070
7171 validationConfiguration . ValidationRules
7272 . Select ( x => x . PropertyName . Pascalize ( ) )
@@ -96,18 +96,20 @@ public void ValidationConfigurationForPatterns()
9696 [ Test ]
9797 public void ValidationConfigurationForNullables ( )
9898 {
99- var validationConfiguration = new MockValidationModelWithNullablesConfiguration ( ) ;
99+ var validationConfiguration = new NullablesConfiguration ( ) ;
100100
101101 validationConfiguration . ValidationRules
102102 . Select ( x => x . PropertyName . Pascalize ( ) ) . Distinct ( )
103103 . Should ( ) . BeEquivalentTo (
104104 nameof ( ValidationMockModel . NullableIntField ) ,
105105 nameof ( ValidationMockModel . NullableDoubleField ) ,
106- nameof ( ValidationMockModel . NullableByteField )
106+ nameof ( ValidationMockModel . NullableByteField ) ,
107+ nameof ( ValidationMockModel . NullableStringField )
107108 ) ;
109+
108110 validationConfiguration . ValidationRules
109111 . Select ( x => x . FormlyRuleName ) . Distinct ( )
110- . Should ( ) . BeEquivalentTo ( "required" , "min" , "max" ) ;
112+ . Should ( ) . BeEquivalentTo ( "required" , "min" , "max" , "minLength" , "maxLength" , "pattern" ) ;
111113 validationConfiguration . ValidationRules
112114 . All ( x => x . HasMessageTranslationId ) . Should ( ) . BeTrue ( ) ;
113115 validationConfiguration . ValidationRules
@@ -117,55 +119,57 @@ public void ValidationConfigurationForNullables()
117119 . Should ( ) . BeEquivalentTo (
118120 "${field?.templateOptions?.label}:property-name: is required" ,
119121 "${field?.templateOptions?.label}:property-name: value should be more than ${field?.templateOptions?.min}:min-value:" ,
120- "${field?.templateOptions?.label}:property-name: value should be less than ${field?.templateOptions?.max}:max-value:"
122+ "${field?.templateOptions?.label}:property-name: value should be less than ${field?.templateOptions?.max}:max-value:" ,
123+ "${field?.templateOptions?.label}:property-name: should have at least ${field?.templateOptions?.minLength}:min-value: characters" ,
124+ "${field?.templateOptions?.label}:property-name: value should be less than ${field?.templateOptions?.maxLength}:max-value: characters" ,
125+ "${field?.templateOptions?.label}:property-name: is not in valid format"
121126 ) ;
122127 }
123128}
124129
125130#region Mocks
126131
127- internal class MockValidationModelConfiguration : ValidationConfiguration < ValidationMockModel >
132+ internal class ModelConfiguration : ValidationConfiguration < ValidationMockModel >
128133{
129134 public const string CustomMessage = "CUSTOM_VALIDATION_MESSAGE" ;
130- public const string CustomMessageTranlsationId = "CUSTOM_VALIDATION_MESSAGE_TRANSLATION_ID" ;
135+ public const string CustomMessageTranslationId = "CUSTOM_VALIDATION_MESSAGE_TRANSLATION_ID" ;
131136
132- public MockValidationModelConfiguration ( )
137+ public ModelConfiguration ( )
133138 {
134139 RuleFor ( x => x . IntField )
135140 . IsRequired ( )
136141 . GreaterThen ( 0 ) . WithMessage ( CustomMessage )
137- . LessThen ( 10 ) . WithMessage ( CustomMessage , CustomMessageTranlsationId ) ;
142+ . LessThen ( 10 ) . WithMessage ( CustomMessage , CustomMessageTranslationId ) ;
138143
139144 RuleFor ( x => x . DoubleField )
140145 . IsRequired ( )
141146 . GreaterThen ( 0.5 ) . WithMessage ( CustomMessage )
142- . LessThen ( 10 ) . WithMessage ( CustomMessage , CustomMessageTranlsationId ) ;
147+ . LessThen ( 10 ) . WithMessage ( CustomMessage , CustomMessageTranslationId ) ;
143148
144149 RuleFor ( x => x . StringField )
145- . IsRequired ( ) . WithMessage ( CustomMessage , CustomMessageTranlsationId )
150+ . IsRequired ( ) . WithMessage ( CustomMessage , CustomMessageTranslationId )
146151 . MinLength ( 0 ) . WithMessage ( CustomMessage )
147152 . MaxLength ( 10 ) ;
148153 }
149154}
150155
151- internal class MockValidationModelWithPatternsConfiguration : ValidationConfiguration < ValidationMockModel >
156+ internal class PatternsConfiguration : ValidationConfiguration < ValidationMockModel >
152157{
153- public MockValidationModelWithPatternsConfiguration ( )
158+ public PatternsConfiguration ( )
154159 {
155160 RuleFor ( x => x . OtherStringField ) . EmailAddress ( ) ;
156- RuleFor ( x => x . StringField ) . Match ( new Regex ( "/[A-Z]/" ) ) ;
157-
161+ RuleFor ( x => x . StringField ) . Match ( new ( "/[A-Z]/" ) ) ;
158162 }
159163}
160164
161- internal class MockValidationModelWithNullablesConfiguration : ValidationConfiguration < ValidationMockModel >
165+ internal class NullablesConfiguration : ValidationConfiguration < ValidationMockModel >
162166{
163- public MockValidationModelWithNullablesConfiguration ( )
167+ public NullablesConfiguration ( )
164168 {
165169 RuleFor ( x => x . NullableIntField ) . IsRequired ( ) . GreaterThen ( 1 ) . LessThen ( 10 ) ;
166170 RuleFor ( x => x . NullableDoubleField ) . IsRequired ( ) . GreaterThen ( 1.1 ) . LessThen ( 10.1 ) ;
167171 RuleFor ( x => x . NullableByteField ) . IsRequired ( ) . GreaterThen ( ( byte ) 1 ) . LessThen ( ( byte ) 10 ) ;
168-
172+ RuleFor ( x => x . NullableStringField ) . MinLength ( 0 ) . MaxLength ( 100 ) . Match ( new ( "/[A-Z]/" ) ) ;
169173 }
170174}
171175
0 commit comments