File tree Expand file tree Collapse file tree
NJsonSchema.Tests/Validation
NJsonSchema/Validation/FormatValidators Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -38,5 +38,56 @@ public void When_format_ipv4_correct_then_validation_succeeds()
3838 // Assert
3939 Assert . Empty ( errors ) ;
4040 }
41+
42+ [ Fact ]
43+ public void When_format_ipv4_has_colons_instead_of_dots_then_validation_fails ( )
44+ {
45+ // Arrange
46+ var schema = new JsonSchema ( ) ;
47+ schema . Type = JsonObjectType . String ;
48+ schema . Format = JsonFormatStrings . IpV4 ;
49+
50+ var token = new JValue ( "00:45:00.0" ) ;
51+
52+ // Act
53+ var errors = schema . Validate ( token ) ;
54+
55+ // Assert
56+ Assert . Equal ( ValidationErrorKind . IpV4Expected , errors . First ( ) . Kind ) ;
57+ }
58+
59+ [ Fact ]
60+ public void When_format_ipv4_has_colons_as_separators_then_validation_fails ( )
61+ {
62+ // Arrange
63+ var schema = new JsonSchema ( ) ;
64+ schema . Type = JsonObjectType . String ;
65+ schema . Format = JsonFormatStrings . IpV4 ;
66+
67+ var token = new JValue ( "1:2:3:4" ) ;
68+
69+ // Act
70+ var errors = schema . Validate ( token ) ;
71+
72+ // Assert
73+ Assert . Equal ( ValidationErrorKind . IpV4Expected , errors . First ( ) . Kind ) ;
74+ }
75+
76+ [ Fact ]
77+ public void When_format_ipv4_octet_exceeds_255_then_validation_fails ( )
78+ {
79+ // Arrange
80+ var schema = new JsonSchema ( ) ;
81+ schema . Type = JsonObjectType . String ;
82+ schema . Format = JsonFormatStrings . IpV4 ;
83+
84+ var token = new JValue ( "256.1.1.1" ) ;
85+
86+ // Act
87+ var errors = schema . Validate ( token ) ;
88+
89+ // Assert
90+ Assert . Equal ( ValidationErrorKind . IpV4Expected , errors . First ( ) . Kind ) ;
91+ }
4192 }
4293}
Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ namespace NJsonSchema.Validation.FormatValidators
1414 /// <summary>Validator for "IpV4" format.</summary>
1515 public class IpV4FormatValidator : IFormatValidator
1616 {
17- private const string IpV4RegexExpression = @"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$" ;
17+ private const string IpV4RegexExpression = @"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\ .){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$" ;
1818
1919 /// <summary>Gets the format attribute's value.</summary>
2020 public string Format { get ; } = JsonFormatStrings . IpV4 ;
You can’t perform that action at this time.
0 commit comments