@Test public void testRequiredValue() { ChoiceSchema choice = new ChoiceSchema(1, 1); choice.addField("aString", new StringSchema()); choice.addField("aLong", new LongSchema()); choice.addField("aDouble", new DoubleSchema()); Map<String, Object> data = new HashMap<String, Object>(); data.put("aString", "abc"); assertTrue(choice.validate(data)); data.put("aLong", 1L); assertFalse(choice.validate(data)); data.remove("aString"); assertTrue(choice.validate(data)); data.put("aLong", "abc"); assertFalse(choice.validate(data)); }
@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; }