Skip to content

Commit 1a47d1d

Browse files
committed
add
1 parent 9e43b93 commit 1a47d1d

File tree

13 files changed

+271
-479
lines changed

13 files changed

+271
-479
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"java.compile.nullAnalysis.mode": "automatic"
3+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package hexlet.code;
2+
3+
public abstract class BaseSchema<T> {
4+
protected boolean required;
5+
6+
@SuppressWarnings("unchecked")
7+
public T required() {
8+
this.required = true;
9+
return (T) this;
10+
}
11+
12+
public abstract boolean isValid(Object value);
13+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package hexlet.code;
2+
3+
import java.util.Map;
4+
5+
public class MapSchema<K> extends BaseSchema<Map<K, ?>> {
6+
private Integer size;
7+
private Map<K, BaseSchema<?>> shapeSchemas;
8+
9+
public MapSchema<K> sizeof(int newSize) {
10+
this.size = newSize;
11+
return this;
12+
}
13+
14+
public MapSchema<K> shape(Map<K, ? extends BaseSchema<?>> schemas) {
15+
this.shapeSchemas = (Map<K, BaseSchema<?>>) schemas;
16+
return this;
17+
}
18+
19+
@Override
20+
public boolean isValid(Object value) {
21+
22+
if (value == null) {
23+
return !required;
24+
}
25+
26+
if (!(value instanceof Map)) {
27+
return false;
28+
}
29+
30+
Map<?, ?> mapValue = (Map<?, ?>) value;
31+
32+
if (size != null && mapValue.size() != size) {
33+
return false;
34+
}
35+
36+
if (shapeSchemas != null) {
37+
for (Map.Entry<K, BaseSchema<?>> entry : shapeSchemas.entrySet()) {
38+
K key = entry.getKey();
39+
BaseSchema<?> schema = entry.getValue();
40+
41+
if (!mapValue.containsKey(key)) {
42+
return false;
43+
}
44+
45+
Object keyValue = mapValue.get(key);
46+
if (!schema.isValid(keyValue)) {
47+
return false;
48+
}
49+
}
50+
}
51+
52+
return true;
53+
}
54+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package hexlet.code;
2+
3+
public class NumberSchema extends BaseSchema<NumberSchema> {
4+
private boolean positive;
5+
private Integer minRange;
6+
private Integer maxRange;
7+
8+
public NumberSchema positive() {
9+
this.positive = true;
10+
return this;
11+
}
12+
13+
public NumberSchema range(int min, int max) {
14+
this.minRange = min;
15+
this.maxRange = max;
16+
return this;
17+
}
18+
19+
@Override
20+
public boolean isValid(Object value) {
21+
22+
if (value == null) {
23+
return !required;
24+
}
25+
26+
if (!(value instanceof Integer)) {
27+
return false;
28+
}
29+
30+
int numValue = (Integer) value;
31+
32+
if (positive && numValue <= 0) {
33+
return false;
34+
}
35+
36+
if (minRange != null && numValue < minRange) {
37+
return false;
38+
}
39+
40+
if (maxRange != null && numValue > maxRange) {
41+
return false;
42+
}
43+
44+
return true;
45+
}
46+
}
Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,8 @@
1-
package hexlet.code.schemas;
1+
package hexlet.code;
22

33
public class StringSchema extends BaseSchema<StringSchema> {
4-
private boolean isRequired = false;
5-
private int minLength = 0;
6-
private String contains = null;
7-
8-
@Override
9-
protected StringSchema getThis() {
10-
return this;
11-
}
12-
13-
@Override
14-
public StringSchema required() {
15-
this.isRequired = true;
16-
return super.required();
17-
}
18-
19-
@Override
20-
public boolean isRequired() {
21-
return isRequired;
22-
}
4+
private Integer minLength;
5+
private String contains;
236

247
public StringSchema minLength(int length) {
258
this.minLength = length;
@@ -33,22 +16,29 @@ public StringSchema contains(String substring) {
3316

3417
@Override
3518
public boolean isValid(Object value) {
19+
3620
if (value == null) {
37-
return !isRequired;
21+
return false;
3822
}
23+
3924
if (!(value instanceof String)) {
4025
return false;
4126
}
27+
4228
String strValue = (String) value;
43-
if (isRequired && strValue.isEmpty()) {
29+
30+
if (required && strValue.isEmpty()) {
4431
return false;
4532
}
46-
if (strValue.length() < minLength) {
33+
34+
if (minLength != null && strValue.length() < minLength) {
4735
return false;
4836
}
37+
4938
if (contains != null && !strValue.contains(contains)) {
5039
return false;
5140
}
41+
5242
return true;
5343
}
5444
}

app/src/main/java/hexlet/code/Validator.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package hexlet.code;
22

3-
import hexlet.code.schemas.MapSchema;
4-
import hexlet.code.schemas.NumberSchema;
5-
import hexlet.code.schemas.StringSchema;
6-
73
public class Validator {
84
public StringSchema string() {
95
return new StringSchema();

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

Lines changed: 0 additions & 10 deletions
This file was deleted.

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

Lines changed: 0 additions & 72 deletions
This file was deleted.

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

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)