File tree Expand file tree Collapse file tree 4 files changed +8
-29
lines changed
main/java/hexlet/code/schemas Expand file tree Collapse file tree 4 files changed +8
-29
lines changed Original file line number Diff line number Diff line change 11package hexlet .code .schemas ;
22
3+ import java .util .HashMap ;
34import java .util .Map ;
45
5- public class MapSchema extends BaseSchema <Map < String , ?> > {
6+ public class MapSchema extends BaseSchema <MapSchema > {
67 private Integer size ;
78 private Map <String , BaseSchema <?>> shapeSchemas ;
89
@@ -11,44 +12,36 @@ public MapSchema sizeof(int newSize) {
1112 return this ;
1213 }
1314
14- public MapSchema shape (Map <String , BaseSchema <?>> schemas ) {
15- this .shapeSchemas = schemas ;
15+ public MapSchema shape (Map <String , ? extends BaseSchema <?>> schemas ) {
16+ this .shapeSchemas = new HashMap <>( schemas ) ;
1617 return this ;
1718 }
1819
1920 @ Override
2021 public boolean isValid (Object value ) {
21-
2222 if (value == null ) {
2323 return !required ;
2424 }
25-
2625 if (!(value instanceof Map )) {
2726 return false ;
2827 }
29-
3028 Map <?, ?> mapValue = (Map <?, ?>) value ;
31-
3229 if (size != null && mapValue .size () != size ) {
3330 return false ;
3431 }
35-
3632 if (shapeSchemas != null ) {
3733 for (Map .Entry <String , BaseSchema <?>> entry : shapeSchemas .entrySet ()) {
3834 String key = entry .getKey ();
3935 BaseSchema <?> schema = entry .getValue ();
40-
4136 if (!mapValue .containsKey (key )) {
4237 return false ;
4338 }
44-
4539 Object keyValue = mapValue .get (key );
4640 if (!schema .isValid (keyValue )) {
4741 return false ;
4842 }
4943 }
5044 }
51-
5245 return true ;
5346 }
5447}
Original file line number Diff line number Diff line change @@ -18,29 +18,22 @@ public NumberSchema range(int min, int max) {
1818
1919 @ Override
2020 public boolean isValid (Object value ) {
21-
2221 if (value == null ) {
2322 return !required ;
2423 }
25-
2624 if (!(value instanceof Integer )) {
2725 return false ;
2826 }
29-
3027 int numValue = (Integer ) value ;
31-
3228 if (positive && numValue <= 0 ) {
3329 return false ;
3430 }
35-
3631 if (minRange != null && numValue < minRange ) {
3732 return false ;
3833 }
39-
4034 if (maxRange != null && numValue > maxRange ) {
4135 return false ;
4236 }
43-
4437 return true ;
4538 }
4639}
Original file line number Diff line number Diff line change @@ -16,29 +16,22 @@ public StringSchema contains(String substring) {
1616
1717 @ Override
1818 public boolean isValid (Object value ) {
19-
2019 if (value == null ) {
21- return false ;
20+ return ! required ;
2221 }
23-
2422 if (!(value instanceof String )) {
2523 return false ;
2624 }
27-
2825 String strValue = (String ) value ;
29-
3026 if (required && strValue .isEmpty ()) {
3127 return false ;
3228 }
33-
3429 if (minLength != null && strValue .length () < minLength ) {
3530 return false ;
3631 }
37-
3832 if (contains != null && !strValue .contains (contains )) {
3933 return false ;
4034 }
41-
4235 return true ;
4336 }
4437}
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ public void testRequired() {
1212 Validator validator = new Validator ();
1313 StringSchema schema = validator .string ();
1414
15- assertFalse (schema .isValid (null ));
15+ assertTrue (schema .isValid (null ));
1616 assertTrue (schema .isValid ("" ));
1717 assertTrue (schema .isValid ("Hello" ));
1818
@@ -27,7 +27,7 @@ public void testMinLength() {
2727 Validator validator = new Validator ();
2828 StringSchema schema = validator .string ().minLength (3 );
2929
30- assertFalse (schema .isValid (null ));
30+ assertTrue (schema .isValid (null ));
3131 assertFalse (schema .isValid ("" ));
3232 assertFalse (schema .isValid ("ab" ));
3333 assertTrue (schema .isValid ("abc" ));
@@ -43,7 +43,7 @@ public void testContains() {
4343 Validator validator = new Validator ();
4444 StringSchema schema = validator .string ().contains ("test" );
4545
46- assertFalse (schema .isValid (null ));
46+ assertTrue (schema .isValid (null ));
4747 assertFalse (schema .isValid ("" ));
4848 assertFalse (schema .isValid ("tes" ));
4949 assertTrue (schema .isValid ("test" ));
You can’t perform that action at this time.
0 commit comments