Skip to content

Commit

Permalink
Add shape function for nested maps
Browse files Browse the repository at this point in the history
  • Loading branch information
niyatanya committed Apr 11, 2024
1 parent 7512808 commit 9205bf8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
16 changes: 16 additions & 0 deletions app/src/main/java/hexlet/code/schemas/MapSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.NoArgsConstructor;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Predicate;

@NoArgsConstructor
Expand All @@ -19,4 +20,19 @@ public MapSchema<K, L> sizeof(int size) {
checkers.put("sizeof", sizeofFunction);
return this;
}

public MapSchema<K, L> shape(Map<String, BaseSchema<L>> schemas) {
Predicate<Map<K, L>> shapeFunction = ((value) -> {
for (Entry<K, L> entry : value.entrySet()) {
K key = entry.getKey();
BaseSchema<L> schema = schemas.get(key);
if (!schema.isValid((entry.getValue()))) {
return false;
}
}
return true;
});
checkers.put("shape", shapeFunction);
return this;
}
}
36 changes: 31 additions & 5 deletions app/src/test/java/hexlet/code/ValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import hexlet.code.schemas.MapSchema;
import hexlet.code.schemas.NumberSchema;
import hexlet.code.schemas.StringSchema;
import hexlet.code.schemas.BaseSchema;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -16,14 +17,16 @@ public class ValidatorTest {
private static StringSchema stringSchema;
private static NumberSchema numberSchema;
private static MapSchema<String, String> mapSchema;

private static MapSchema<String, String> mapSchema2;
private static Validator val = new Validator();

@BeforeEach
public void preparation() {
Validator val = new Validator();
stringSchema = val.string();
Validator val1 = new Validator();
numberSchema = val1.number();
Validator val2 = new Validator();
mapSchema = val2.map();
numberSchema = val.number();
mapSchema = val.map();
mapSchema2 = val.map();
}

@Test
Expand Down Expand Up @@ -107,4 +110,27 @@ public void mapSchemaTestSizeof() {
map.put("Kate", "singer");
assertTrue(mapSchema.isValid(map));
}

@Test
public void mapShapedSchemaTest() {
Map<String, BaseSchema<String>> schemas = new HashMap<>();
schemas.put("firstName", val.string().required());
schemas.put("lastName", val.string().required().minLength(2));
mapSchema2.shape(schemas);

Map<String, String> human1 = new HashMap<>();
human1.put("firstName", "John");
human1.put("lastName", "Smith");
assertTrue(mapSchema2.isValid(human1));

Map<String, String> human2 = new HashMap<>();
human2.put("firstName", "John");
human2.put("lastName", null);
assertFalse(mapSchema2.isValid(human2));

Map<String, String> human3 = new HashMap<>();
human3.put("firstName", "Anna");
human3.put("lastName", "B");
assertFalse(mapSchema2.isValid(human3));
}
}

0 comments on commit 9205bf8

Please sign in to comment.