|
| 1 | +package org.javawebstack.abstractdata.schema; |
| 2 | + |
| 3 | +import org.javawebstack.abstractdata.AbstractArray; |
| 4 | +import org.javawebstack.abstractdata.AbstractElement; |
| 5 | +import org.javawebstack.abstractdata.AbstractPath; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Locale; |
| 10 | + |
| 11 | +public class AbstractArraySchema implements AbstractSchema { |
| 12 | + |
| 13 | + private AbstractSchema itemSchema; |
| 14 | + private Integer min; |
| 15 | + private Integer max; |
| 16 | + private boolean allowNull = false; |
| 17 | + private final List<CustomValidation<AbstractArray>> customValidations = new ArrayList<>(); |
| 18 | + |
| 19 | + public AbstractArraySchema itemSchema(AbstractSchema schema) { |
| 20 | + this.itemSchema = schema; |
| 21 | + return this; |
| 22 | + } |
| 23 | + |
| 24 | + public AbstractArraySchema min(int min) { |
| 25 | + this.min = min; |
| 26 | + return this; |
| 27 | + } |
| 28 | + |
| 29 | + public AbstractArraySchema max(int max) { |
| 30 | + this.max = max; |
| 31 | + return this; |
| 32 | + } |
| 33 | + |
| 34 | + public AbstractArraySchema allowNull() { |
| 35 | + this.allowNull = true; |
| 36 | + return this; |
| 37 | + } |
| 38 | + |
| 39 | + public AbstractArraySchema customValidation(CustomValidation<AbstractArray> validation) { |
| 40 | + customValidations.add(validation); |
| 41 | + return this; |
| 42 | + } |
| 43 | + |
| 44 | + public AbstractSchema getItemSchema() { |
| 45 | + return itemSchema; |
| 46 | + } |
| 47 | + |
| 48 | + public Integer getMin() { |
| 49 | + return min; |
| 50 | + } |
| 51 | + |
| 52 | + public Integer getMax() { |
| 53 | + return max; |
| 54 | + } |
| 55 | + |
| 56 | + public List<CustomValidation<AbstractArray>> getCustomValidations() { |
| 57 | + return customValidations; |
| 58 | + } |
| 59 | + |
| 60 | + public List<SchemaValidationError> validate(AbstractPath path, AbstractElement value) { |
| 61 | + List<SchemaValidationError> errors = new ArrayList<>(); |
| 62 | + if(value.getType() != AbstractElement.Type.ARRAY) { |
| 63 | + errors.add(new SchemaValidationError(path, "invalid_type").meta("expected", "array").meta("actual", value.getType().name().toLowerCase(Locale.ROOT))); |
| 64 | + return errors; |
| 65 | + } |
| 66 | + AbstractArray array = value.array(); |
| 67 | + if(min != null && array.size() < min) { |
| 68 | + errors.add(new SchemaValidationError(path, "not_enough_items").meta("min", String.valueOf(min)).meta("actual", String.valueOf(array.size()))); |
| 69 | + } |
| 70 | + if(max != null && array.size() > max) { |
| 71 | + errors.add(new SchemaValidationError(path, "too_many_items").meta("max", String.valueOf(max)).meta("actual", String.valueOf(array.size()))); |
| 72 | + } |
| 73 | + if(itemSchema != null) { |
| 74 | + for(int i=0; i<array.size(); i++) { |
| 75 | + AbstractElement item = array.get(i); |
| 76 | + AbstractPath itemPath = path.subPath(String.valueOf(i)); |
| 77 | + if(item.isNull()) { |
| 78 | + if(!allowNull) { |
| 79 | + errors.add(new SchemaValidationError(itemPath, "null_not_allowed")); |
| 80 | + } |
| 81 | + continue; |
| 82 | + } |
| 83 | + errors.addAll(itemSchema.validate(itemPath, array.get(i))); |
| 84 | + } |
| 85 | + } |
| 86 | + for(CustomValidation<AbstractArray> validation : customValidations) { |
| 87 | + errors.addAll(validation.validate(path, array)); |
| 88 | + } |
| 89 | + return errors; |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments