@Test public void testComplexSchemas() { ComplexSchema c1 = new ComplexSchema(); c1.addField("s1", new StringSchema()); c1.addField("l1", new LongSchema()); ComplexSchema c2 = new ComplexSchema(); c2.addField("s2", new StringSchema()); c2.addField("l2", new LongSchema()); ChoiceSchema choice = new ChoiceSchema(0, 1); choice.addField("c1", c1); choice.addField("c2", c2); choice.addField("s3", new StringSchema()); Map<String, Object> c1data = new HashMap<String, Object>(); c1data.put("s1", "abc"); c1data.put("l1", 1L); Map<String, Object> c2data = new HashMap<String, Object>(); c2data.put("s2", "efg"); c2data.put("l2", 1L); assertTrue(choice.validate(createMap("s3", "abc"))); assertTrue(choice.validate(createMap("c1", c1data))); assertTrue(choice.validate(createMap("c2", c2data))); assertFalse(choice.validate(createMap("c2", "abc"))); assertFalse(choice.validate(createMap("s3", c1data))); assertFalse(choice.validate(createMap("c1", c2data))); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Override protected boolean validate(String fieldName, Object entity_in, List<ValidationError> errors, Repository<Entity> repo) { Object entity = entity_in; if (entity instanceof Set) { entity = new ArrayList<Object>((Set) entity); } if (getMaxOccurs() == 1) { if (getMinOccurs() == 0 && entity == null) { return true; } if (!(entity instanceof Map)) { return addError(false, fieldName, entity, "Map", ErrorType.INVALID_DATATYPE, errors); } Map<?, ?> datum = (Map<?, ?>) entity; if (datum.size() != 1) { return addError(false, fieldName, entity, "Single Entry", ErrorType.INVALID_DATATYPE, errors); } Entry<?, ?> datumValue = datum.entrySet().iterator().next(); if (!(datumValue.getKey() instanceof String)) { return addError(false, fieldName, entity, this.getFields().keySet().toArray(new String[0]), ErrorType.INVALID_CHOICE_TYPE, errors); } String key = (String) datumValue.getKey(); Object value = datumValue.getValue(); NeutralSchema fieldSchema = this.getFields().get(key); if (fieldSchema == null) { return addError(false, fieldName, entity, this.getFields().keySet().toArray(new String[0]), ErrorType.INVALID_CHOICE_TYPE, errors); } if (fieldSchema.validate(key, value, errors, repo)) { return true; } } else if (getMaxOccurs() > 1) { if (!(entity instanceof List)) { return addError(false, fieldName, entity, "List", ErrorType.INVALID_DATATYPE, errors); } if (((List) entity).size() < getMinOccurs()) { return addError(false, fieldName, entity, "min-length=" + this.getMinOccurs(), ErrorType.INVALID_VALUE, errors); } else if (((List) entity).size() > getMaxOccurs()) { return addError(false, fieldName, entity, "max-length=" + this.getMinOccurs(), ErrorType.INVALID_VALUE, errors); } List<Object> data = (List<Object>) entity; for (Object uncastedDatum : data) { if (!(uncastedDatum instanceof Map)) { return addError(false, fieldName, entity, "Map", ErrorType.INVALID_DATATYPE, errors); } Map<?, ?> datum = (Map<?, ?>) uncastedDatum; if (datum.size() != 1) { return addError(false, fieldName, entity, "Single Entry", ErrorType.INVALID_DATATYPE, errors); } Entry<?, ?> datumValue = datum.entrySet().iterator().next(); if (!(datumValue.getKey() instanceof String)) { return addError(false, fieldName, entity, this.getFields().keySet().toArray(new String[0]), ErrorType.INVALID_CHOICE_TYPE, errors); } String key = (String) datumValue.getKey(); Object value = datumValue.getValue(); NeutralSchema fieldSchema = this.getFields().get(key); if (fieldSchema == null) { return addError(false, fieldName, entity, this.getFields().keySet().toArray(new String[0]), ErrorType.INVALID_CHOICE_TYPE, errors); } if (!fieldSchema.validate(key, value, errors, repo)) { return false; } } return true; } return false; }
ChoiceSchema extends NeutralSchema { @SuppressWarnings({ "unchecked", "rawtypes" }) @Override protected boolean validate(String fieldName, Object entity_in, List<ValidationError> errors, Repository<Entity> repo) { Object entity = entity_in; if (entity instanceof Set) { entity = new ArrayList<Object>((Set) entity); } if (getMaxOccurs() == 1) { if (getMinOccurs() == 0 && entity == null) { return true; } if (!(entity instanceof Map)) { return addError(false, fieldName, entity, "Map", ErrorType.INVALID_DATATYPE, errors); } Map<?, ?> datum = (Map<?, ?>) entity; if (datum.size() != 1) { return addError(false, fieldName, entity, "Single Entry", ErrorType.INVALID_DATATYPE, errors); } Entry<?, ?> datumValue = datum.entrySet().iterator().next(); if (!(datumValue.getKey() instanceof String)) { return addError(false, fieldName, entity, this.getFields().keySet().toArray(new String[0]), ErrorType.INVALID_CHOICE_TYPE, errors); } String key = (String) datumValue.getKey(); Object value = datumValue.getValue(); NeutralSchema fieldSchema = this.getFields().get(key); if (fieldSchema == null) { return addError(false, fieldName, entity, this.getFields().keySet().toArray(new String[0]), ErrorType.INVALID_CHOICE_TYPE, errors); } if (fieldSchema.validate(key, value, errors, repo)) { return true; } } else if (getMaxOccurs() > 1) { if (!(entity instanceof List)) { return addError(false, fieldName, entity, "List", ErrorType.INVALID_DATATYPE, errors); } if (((List) entity).size() < getMinOccurs()) { return addError(false, fieldName, entity, "min-length=" + this.getMinOccurs(), ErrorType.INVALID_VALUE, errors); } else if (((List) entity).size() > getMaxOccurs()) { return addError(false, fieldName, entity, "max-length=" + this.getMinOccurs(), ErrorType.INVALID_VALUE, errors); } List<Object> data = (List<Object>) entity; for (Object uncastedDatum : data) { if (!(uncastedDatum instanceof Map)) { return addError(false, fieldName, entity, "Map", ErrorType.INVALID_DATATYPE, errors); } Map<?, ?> datum = (Map<?, ?>) uncastedDatum; if (datum.size() != 1) { return addError(false, fieldName, entity, "Single Entry", ErrorType.INVALID_DATATYPE, errors); } Entry<?, ?> datumValue = datum.entrySet().iterator().next(); if (!(datumValue.getKey() instanceof String)) { return addError(false, fieldName, entity, this.getFields().keySet().toArray(new String[0]), ErrorType.INVALID_CHOICE_TYPE, errors); } String key = (String) datumValue.getKey(); Object value = datumValue.getValue(); NeutralSchema fieldSchema = this.getFields().get(key); if (fieldSchema == null) { return addError(false, fieldName, entity, this.getFields().keySet().toArray(new String[0]), ErrorType.INVALID_CHOICE_TYPE, errors); } if (!fieldSchema.validate(key, value, errors, repo)) { return false; } } return true; } return false; } }
ChoiceSchema extends NeutralSchema { @SuppressWarnings({ "unchecked", "rawtypes" }) @Override protected boolean validate(String fieldName, Object entity_in, List<ValidationError> errors, Repository<Entity> repo) { Object entity = entity_in; if (entity instanceof Set) { entity = new ArrayList<Object>((Set) entity); } if (getMaxOccurs() == 1) { if (getMinOccurs() == 0 && entity == null) { return true; } if (!(entity instanceof Map)) { return addError(false, fieldName, entity, "Map", ErrorType.INVALID_DATATYPE, errors); } Map<?, ?> datum = (Map<?, ?>) entity; if (datum.size() != 1) { return addError(false, fieldName, entity, "Single Entry", ErrorType.INVALID_DATATYPE, errors); } Entry<?, ?> datumValue = datum.entrySet().iterator().next(); if (!(datumValue.getKey() instanceof String)) { return addError(false, fieldName, entity, this.getFields().keySet().toArray(new String[0]), ErrorType.INVALID_CHOICE_TYPE, errors); } String key = (String) datumValue.getKey(); Object value = datumValue.getValue(); NeutralSchema fieldSchema = this.getFields().get(key); if (fieldSchema == null) { return addError(false, fieldName, entity, this.getFields().keySet().toArray(new String[0]), ErrorType.INVALID_CHOICE_TYPE, errors); } if (fieldSchema.validate(key, value, errors, repo)) { return true; } } else if (getMaxOccurs() > 1) { if (!(entity instanceof List)) { return addError(false, fieldName, entity, "List", ErrorType.INVALID_DATATYPE, errors); } if (((List) entity).size() < getMinOccurs()) { return addError(false, fieldName, entity, "min-length=" + this.getMinOccurs(), ErrorType.INVALID_VALUE, errors); } else if (((List) entity).size() > getMaxOccurs()) { return addError(false, fieldName, entity, "max-length=" + this.getMinOccurs(), ErrorType.INVALID_VALUE, errors); } List<Object> data = (List<Object>) entity; for (Object uncastedDatum : data) { if (!(uncastedDatum instanceof Map)) { return addError(false, fieldName, entity, "Map", ErrorType.INVALID_DATATYPE, errors); } Map<?, ?> datum = (Map<?, ?>) uncastedDatum; if (datum.size() != 1) { return addError(false, fieldName, entity, "Single Entry", ErrorType.INVALID_DATATYPE, errors); } Entry<?, ?> datumValue = datum.entrySet().iterator().next(); if (!(datumValue.getKey() instanceof String)) { return addError(false, fieldName, entity, this.getFields().keySet().toArray(new String[0]), ErrorType.INVALID_CHOICE_TYPE, errors); } String key = (String) datumValue.getKey(); Object value = datumValue.getValue(); NeutralSchema fieldSchema = this.getFields().get(key); if (fieldSchema == null) { return addError(false, fieldName, entity, this.getFields().keySet().toArray(new String[0]), ErrorType.INVALID_CHOICE_TYPE, errors); } if (!fieldSchema.validate(key, value, errors, repo)) { return false; } } return true; } return false; } ChoiceSchema(long minOccurs, long maxOccurs);  ChoiceSchema(String name); }
ChoiceSchema extends NeutralSchema { @SuppressWarnings({ "unchecked", "rawtypes" }) @Override protected boolean validate(String fieldName, Object entity_in, List<ValidationError> errors, Repository<Entity> repo) { Object entity = entity_in; if (entity instanceof Set) { entity = new ArrayList<Object>((Set) entity); } if (getMaxOccurs() == 1) { if (getMinOccurs() == 0 && entity == null) { return true; } if (!(entity instanceof Map)) { return addError(false, fieldName, entity, "Map", ErrorType.INVALID_DATATYPE, errors); } Map<?, ?> datum = (Map<?, ?>) entity; if (datum.size() != 1) { return addError(false, fieldName, entity, "Single Entry", ErrorType.INVALID_DATATYPE, errors); } Entry<?, ?> datumValue = datum.entrySet().iterator().next(); if (!(datumValue.getKey() instanceof String)) { return addError(false, fieldName, entity, this.getFields().keySet().toArray(new String[0]), ErrorType.INVALID_CHOICE_TYPE, errors); } String key = (String) datumValue.getKey(); Object value = datumValue.getValue(); NeutralSchema fieldSchema = this.getFields().get(key); if (fieldSchema == null) { return addError(false, fieldName, entity, this.getFields().keySet().toArray(new String[0]), ErrorType.INVALID_CHOICE_TYPE, errors); } if (fieldSchema.validate(key, value, errors, repo)) { return true; } } else if (getMaxOccurs() > 1) { if (!(entity instanceof List)) { return addError(false, fieldName, entity, "List", ErrorType.INVALID_DATATYPE, errors); } if (((List) entity).size() < getMinOccurs()) { return addError(false, fieldName, entity, "min-length=" + this.getMinOccurs(), ErrorType.INVALID_VALUE, errors); } else if (((List) entity).size() > getMaxOccurs()) { return addError(false, fieldName, entity, "max-length=" + this.getMinOccurs(), ErrorType.INVALID_VALUE, errors); } List<Object> data = (List<Object>) entity; for (Object uncastedDatum : data) { if (!(uncastedDatum instanceof Map)) { return addError(false, fieldName, entity, "Map", ErrorType.INVALID_DATATYPE, errors); } Map<?, ?> datum = (Map<?, ?>) uncastedDatum; if (datum.size() != 1) { return addError(false, fieldName, entity, "Single Entry", ErrorType.INVALID_DATATYPE, errors); } Entry<?, ?> datumValue = datum.entrySet().iterator().next(); if (!(datumValue.getKey() instanceof String)) { return addError(false, fieldName, entity, this.getFields().keySet().toArray(new String[0]), ErrorType.INVALID_CHOICE_TYPE, errors); } String key = (String) datumValue.getKey(); Object value = datumValue.getValue(); NeutralSchema fieldSchema = this.getFields().get(key); if (fieldSchema == null) { return addError(false, fieldName, entity, this.getFields().keySet().toArray(new String[0]), ErrorType.INVALID_CHOICE_TYPE, errors); } if (!fieldSchema.validate(key, value, errors, repo)) { return false; } } return true; } return false; } ChoiceSchema(long minOccurs, long maxOccurs);  ChoiceSchema(String name); void setMinOccurs(long i); void setMaxOccurs(long i); long getMinOccurs(); long getMaxOccurs(); @Override NeutralSchemaType getSchemaType(); @Override boolean isPrimitive(); @Override boolean isSimple(); }
ChoiceSchema extends NeutralSchema { @SuppressWarnings({ "unchecked", "rawtypes" }) @Override protected boolean validate(String fieldName, Object entity_in, List<ValidationError> errors, Repository<Entity> repo) { Object entity = entity_in; if (entity instanceof Set) { entity = new ArrayList<Object>((Set) entity); } if (getMaxOccurs() == 1) { if (getMinOccurs() == 0 && entity == null) { return true; } if (!(entity instanceof Map)) { return addError(false, fieldName, entity, "Map", ErrorType.INVALID_DATATYPE, errors); } Map<?, ?> datum = (Map<?, ?>) entity; if (datum.size() != 1) { return addError(false, fieldName, entity, "Single Entry", ErrorType.INVALID_DATATYPE, errors); } Entry<?, ?> datumValue = datum.entrySet().iterator().next(); if (!(datumValue.getKey() instanceof String)) { return addError(false, fieldName, entity, this.getFields().keySet().toArray(new String[0]), ErrorType.INVALID_CHOICE_TYPE, errors); } String key = (String) datumValue.getKey(); Object value = datumValue.getValue(); NeutralSchema fieldSchema = this.getFields().get(key); if (fieldSchema == null) { return addError(false, fieldName, entity, this.getFields().keySet().toArray(new String[0]), ErrorType.INVALID_CHOICE_TYPE, errors); } if (fieldSchema.validate(key, value, errors, repo)) { return true; } } else if (getMaxOccurs() > 1) { if (!(entity instanceof List)) { return addError(false, fieldName, entity, "List", ErrorType.INVALID_DATATYPE, errors); } if (((List) entity).size() < getMinOccurs()) { return addError(false, fieldName, entity, "min-length=" + this.getMinOccurs(), ErrorType.INVALID_VALUE, errors); } else if (((List) entity).size() > getMaxOccurs()) { return addError(false, fieldName, entity, "max-length=" + this.getMinOccurs(), ErrorType.INVALID_VALUE, errors); } List<Object> data = (List<Object>) entity; for (Object uncastedDatum : data) { if (!(uncastedDatum instanceof Map)) { return addError(false, fieldName, entity, "Map", ErrorType.INVALID_DATATYPE, errors); } Map<?, ?> datum = (Map<?, ?>) uncastedDatum; if (datum.size() != 1) { return addError(false, fieldName, entity, "Single Entry", ErrorType.INVALID_DATATYPE, errors); } Entry<?, ?> datumValue = datum.entrySet().iterator().next(); if (!(datumValue.getKey() instanceof String)) { return addError(false, fieldName, entity, this.getFields().keySet().toArray(new String[0]), ErrorType.INVALID_CHOICE_TYPE, errors); } String key = (String) datumValue.getKey(); Object value = datumValue.getValue(); NeutralSchema fieldSchema = this.getFields().get(key); if (fieldSchema == null) { return addError(false, fieldName, entity, this.getFields().keySet().toArray(new String[0]), ErrorType.INVALID_CHOICE_TYPE, errors); } if (!fieldSchema.validate(key, value, errors, repo)) { return false; } } return true; } return false; } ChoiceSchema(long minOccurs, long maxOccurs);  ChoiceSchema(String name); void setMinOccurs(long i); void setMaxOccurs(long i); long getMinOccurs(); long getMaxOccurs(); @Override NeutralSchemaType getSchemaType(); @Override boolean isPrimitive(); @Override boolean isSimple(); static final long UNBOUNDED; }