File tree Expand file tree Collapse file tree 4 files changed +25
-18
lines changed
app/src/main/java/hexlet/code/schemas Expand file tree Collapse file tree 4 files changed +25
-18
lines changed Original file line number Diff line number Diff line change 22
33public abstract class BaseSchema <T extends BaseSchema <T >> {
44 protected abstract T getThis ();
5-
5+ public abstract boolean isRequired ();
66 public T required () {
77 return getThis ();
88 }
9-
109 public abstract boolean isValid (Object value );
1110}
Original file line number Diff line number Diff line change @@ -16,7 +16,12 @@ protected MapSchema getThis() {
1616 @ Override
1717 public MapSchema required () {
1818 this .isRequired = true ;
19- return this ;
19+ return super .required ();
20+ }
21+
22+ @ Override
23+ public boolean isRequired () {
24+ return isRequired ;
2025 }
2126
2227 public MapSchema sizeof (int size ) {
@@ -38,38 +43,30 @@ public boolean isValid(Object value) {
3843 return false ;
3944 }
4045 Map <?, ?> map = (Map <?, ?>) value ;
46+
4147 if (isRequired && map .isEmpty ()) {
4248 return false ;
4349 }
4450 if (mapSize != null && map .size () != mapSize ) {
4551 return false ;
4652 }
53+
4754 for (Map .Entry <String , BaseSchema <?>> entry : shapeSchemas .entrySet ()) {
4855 String key = entry .getKey ();
4956 BaseSchema <?> schema = entry .getValue ();
57+
5058 if (!map .containsKey (key )) {
51- if (schema instanceof RequiredSchema ) {
59+ if (schema . isRequired () ) {
5260 return false ;
5361 }
5462 continue ;
5563 }
64+
5665 Object val = map .get (key );
5766 if (!schema .isValid (val )) {
5867 return false ;
5968 }
6069 }
6170 return true ;
6271 }
63-
64- private static class RequiredSchema extends BaseSchema <RequiredSchema > {
65- @ Override
66- protected RequiredSchema getThis () {
67- return this ;
68- }
69-
70- @ Override
71- public boolean isValid (Object value ) {
72- return value != null ;
73- }
74- }
7572}
Original file line number Diff line number Diff line change @@ -11,9 +11,15 @@ protected NumberSchema getThis() {
1111 return this ;
1212 }
1313
14+ @ Override
1415 public NumberSchema required () {
1516 this .isRequired = true ;
16- return this ;
17+ return super .required ();
18+ }
19+
20+ @ Override
21+ public boolean isRequired () {
22+ return isRequired ;
1723 }
1824
1925 public NumberSchema positive () {
Original file line number Diff line number Diff line change @@ -13,7 +13,12 @@ protected StringSchema getThis() {
1313 @ Override
1414 public StringSchema required () {
1515 this .isRequired = true ;
16- return this ;
16+ return super .required ();
17+ }
18+
19+ @ Override
20+ public boolean isRequired () {
21+ return isRequired ;
1722 }
1823
1924 public StringSchema minLength (int length ) {
You can’t perform that action at this time.
0 commit comments