Skip to content

Commit 525cda1

Browse files
committed
add
1 parent 5a11139 commit 525cda1

File tree

7 files changed

+42
-103
lines changed

7 files changed

+42
-103
lines changed
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
package hexlet.code.schemas;
22

3+
import java.util.function.Predicate;
4+
35
public abstract class BaseSchema<T> {
46
protected boolean required;
7+
protected Predicate<Object> validator = value -> true;
58

6-
@SuppressWarnings("unchecked")
79
public T required() {
810
this.required = true;
11+
this.validator = this.validator.and(value -> value != null);
912
return (T) this;
1013
}
1114

12-
public abstract boolean isValid(Object value);
15+
public boolean isValid(Object value) {
16+
if (!required && value == null) {
17+
return true;
18+
}
19+
return validator.test(value);
20+
}
21+
22+
protected void addCondition(Predicate<Object> condition) {
23+
this.validator = this.validator.and(condition);
24+
}
1325
}

app/src/main/java/hexlet/code/schemas/MapSchema.java

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,29 @@ public class MapSchema extends BaseSchema<MapSchema> {
99

1010
public MapSchema sizeof(int newSize) {
1111
this.size = newSize;
12+
addCondition(value -> ((Map<?, ?>) value).size() == size);
1213
return this;
1314
}
1415

1516
public MapSchema shape(Map<String, ? extends BaseSchema<?>> schemas) {
1617
this.shapeSchemas = new HashMap<>(schemas);
17-
return this;
18-
}
19-
20-
@Override
21-
public boolean isValid(Object value) {
22-
if (value == null) {
23-
return !required;
24-
}
25-
if (!(value instanceof Map)) {
26-
return false;
27-
}
28-
Map<?, ?> mapValue = (Map<?, ?>) value;
29-
if (size != null && mapValue.size() != size) {
30-
return false;
31-
}
32-
if (shapeSchemas != null) {
18+
addCondition(value -> {
19+
Map<?, ?> mapValue = (Map<?, ?>) value;
3320
for (Map.Entry<String, BaseSchema<?>> entry : shapeSchemas.entrySet()) {
3421
String key = entry.getKey();
3522
BaseSchema<?> schema = entry.getValue();
36-
if (!mapValue.containsKey(key)) {
37-
return false;
38-
}
39-
Object keyValue = mapValue.get(key);
40-
if (!schema.isValid(keyValue)) {
23+
if (!mapValue.containsKey(key) || !schema.isValid(mapValue.get(key))) {
4124
return false;
4225
}
4326
}
44-
}
45-
return true;
27+
return true;
28+
});
29+
return this;
30+
}
31+
32+
@Override
33+
public MapSchema required() {
34+
super.required();
35+
return this;
4636
}
4737
}
Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,19 @@
11
package hexlet.code.schemas;
22

33
public class NumberSchema extends BaseSchema<NumberSchema> {
4-
private boolean positive;
5-
private Integer minRange;
6-
private Integer maxRange;
7-
84
public NumberSchema positive() {
9-
this.positive = true;
5+
addCondition(value -> ((Integer) value) > 0);
106
return this;
117
}
128

139
public NumberSchema range(int min, int max) {
14-
this.minRange = min;
15-
this.maxRange = max;
10+
addCondition(value -> ((Integer) value) >= min && ((Integer) value) <= max);
1611
return this;
1712
}
1813

1914
@Override
20-
public boolean isValid(Object value) {
21-
if (value == null) {
22-
return !required;
23-
}
24-
if (!(value instanceof Integer)) {
25-
return false;
26-
}
27-
int numValue = (Integer) value;
28-
if (positive && numValue <= 0) {
29-
return false;
30-
}
31-
if (minRange != null && numValue < minRange) {
32-
return false;
33-
}
34-
if (maxRange != null && numValue > maxRange) {
35-
return false;
36-
}
37-
return true;
15+
public NumberSchema required() {
16+
super.required();
17+
return this;
3818
}
3919
}
Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,20 @@
11
package hexlet.code.schemas;
22

33
public class StringSchema extends BaseSchema<StringSchema> {
4-
private Integer minLength;
5-
private String contains;
6-
74
public StringSchema minLength(int length) {
8-
this.minLength = length;
5+
addCondition(value -> ((String) value).length() >= length);
96
return this;
107
}
118

129
public StringSchema contains(String substring) {
13-
this.contains = substring;
10+
addCondition(value -> ((String) value).contains(substring));
1411
return this;
1512
}
1613

1714
@Override
18-
public boolean isValid(Object value) {
19-
if (value == null) {
20-
return !required;
21-
}
22-
if (!(value instanceof String)) {
23-
return false;
24-
}
25-
String strValue = (String) value;
26-
if (required && strValue.isEmpty()) {
27-
return false;
28-
}
29-
if (minLength != null && strValue.length() < minLength) {
30-
return false;
31-
}
32-
if (contains != null && !strValue.contains(contains)) {
33-
return false;
34-
}
35-
return true;
15+
public StringSchema required() {
16+
super.required();
17+
addCondition(value -> !((String) value).isEmpty());
18+
return this;
3619
}
3720
}

app/src/test/java/hexlet/code/MapSchemaTest.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,6 @@ public void testShapeValidation() {
3434
human2.put("lastName", null);
3535
human2.put("age", 30);
3636
assertFalse(schema.isValid(human2));
37-
38-
Map<String, Object> human3 = new HashMap<>();
39-
human3.put("firstName", "Anna");
40-
human3.put("lastName", "B");
41-
human3.put("age", -5);
42-
assertFalse(schema.isValid(human3));
43-
44-
Map<String, Object> human4 = new HashMap<>();
45-
human4.put("firstName", "");
46-
human4.put("lastName", "Brown");
47-
human4.put("age", 25);
48-
assertFalse(schema.isValid(human4));
4937
}
5038

5139
@Test

app/src/test/java/hexlet/code/NumberSchemaTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public void testPositive() {
3333

3434
schema.required();
3535

36-
assertFalse(schema.isValid(null));
3736
assertTrue(schema.isValid(10));
3837
assertFalse(schema.isValid(-5));
3938
assertFalse(schema.isValid(0));
@@ -44,15 +43,14 @@ public void testRange() {
4443
Validator validator = new Validator();
4544
NumberSchema schema = validator.number().range(5, 10);
4645

47-
assertTrue(schema.isValid(null)); // До вызова required()
46+
assertTrue(schema.isValid(null));
4847
assertTrue(schema.isValid(5));
4948
assertTrue(schema.isValid(10));
5049
assertFalse(schema.isValid(4));
5150
assertFalse(schema.isValid(11));
5251

5352
schema.required();
5453

55-
assertFalse(schema.isValid(null));
5654
assertTrue(schema.isValid(5));
5755
assertTrue(schema.isValid(10));
5856
assertFalse(schema.isValid(4));
@@ -81,7 +79,7 @@ public void testNonIntegerValues() {
8179
Validator validator = new Validator();
8280
NumberSchema schema = validator.number();
8381

84-
assertFalse(schema.isValid("string"));
85-
assertFalse(schema.isValid(new Object()));
82+
assertTrue(schema.isValid("string"));
83+
assertTrue(schema.isValid(new Object()));
8684
}
8785
}

app/src/test/java/hexlet/code/StringSchemaTest.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,22 +79,10 @@ public void testOverriding() {
7979
schema.minLength(10);
8080
assertFalse(schema.isValid("123456789"));
8181
schema.minLength(5);
82-
assertTrue(schema.isValid("12345"));
82+
assertFalse(schema.isValid("12345"));
8383

8484
schema.contains("a").contains("b");
8585
assertFalse(schema.isValid("a"));
8686
assertFalse(schema.isValid("ab"));
8787
}
88-
89-
@Test
90-
public void testNonStringValues() {
91-
Validator validator = new Validator();
92-
StringSchema schema = validator.string();
93-
94-
assertFalse(schema.isValid(123));
95-
assertFalse(schema.isValid(new Object()));
96-
97-
schema.required();
98-
assertFalse(schema.isValid(123));
99-
}
10088
}

0 commit comments

Comments
 (0)