@Test public void testRestrictions() { schema.getProperties().put(Restriction.MIN_LENGTH.getValue(), "2"); schema.getProperties().put(Restriction.MAX_LENGTH.getValue(), "4"); assertTrue(schema.validate("12")); assertTrue(schema.validate("1234")); assertFalse(schema.validate("1")); assertFalse(schema.validate("12345")); schema.getProperties().put(Restriction.LENGTH.getValue(), "4"); assertTrue(schema.validate("1234")); assertFalse(schema.validate("123")); assertFalse(schema.validate("12345")); }
@Override protected boolean validate(String fieldName, Object entity, List<ValidationError> errors, Repository<Entity> repo) { if (!addError(String.class.isInstance(entity), fieldName, entity, "String", ErrorType.INVALID_DATATYPE, errors)) { return false; } String data = (String) entity; if (this.getProperties() != null) { for (Entry<String, Object> entry : this.getProperties().entrySet()) { if (Restriction.isRestriction(entry.getKey())) { String restrictionValue = (String) entry.getValue(); switch (Restriction.fromValue(entry.getKey())) { case PATTERN: if (!addError(data.matches(restrictionValue), fieldName, entity, "pattern=" + restrictionValue, ErrorType.INVALID_VALUE, errors)) { return false; } break; case LENGTH: if (!addError(data.length() == Integer.parseInt(restrictionValue), fieldName, entity, "length=" + restrictionValue, ErrorType.INVALID_VALUE, errors)) { return false; } break; case MIN_LENGTH: if (!addError(data.length() >= Integer.parseInt(restrictionValue), fieldName, entity, "min-length=" + restrictionValue, ErrorType.INVALID_VALUE, errors)) { return false; } break; case MAX_LENGTH: if (!addError(data.length() <= Integer.parseInt(restrictionValue), fieldName, entity, "max-length=" + restrictionValue, ErrorType.INVALID_VALUE, errors)) { return false; } break; } } } } if (isRelaxedBlacklisted()) { for (AbstractBlacklistStrategy validationRule : relaxedValidationRuleList) { boolean isValid = validationRule.isValid("StringSchemaContext", data); if (!addError(isValid, fieldName, entity, "Invalid value caught by relaxed blacklisting strategy: " + validationRule.getIdentifier(), ErrorType.INVALID_VALUE, errors)) { return false; } } } else { for (AbstractBlacklistStrategy validationRule : validationRuleList) { boolean isValid = validationRule.isValid("StringSchemaContext", data); if (!addError(isValid, fieldName, entity, "Invalid value caught by strict blacklisting strategy: " + validationRule.getIdentifier(), ErrorType.INVALID_VALUE, errors)) { return false; } } } return true; }
StringSchema extends NeutralSchema { @Override protected boolean validate(String fieldName, Object entity, List<ValidationError> errors, Repository<Entity> repo) { if (!addError(String.class.isInstance(entity), fieldName, entity, "String", ErrorType.INVALID_DATATYPE, errors)) { return false; } String data = (String) entity; if (this.getProperties() != null) { for (Entry<String, Object> entry : this.getProperties().entrySet()) { if (Restriction.isRestriction(entry.getKey())) { String restrictionValue = (String) entry.getValue(); switch (Restriction.fromValue(entry.getKey())) { case PATTERN: if (!addError(data.matches(restrictionValue), fieldName, entity, "pattern=" + restrictionValue, ErrorType.INVALID_VALUE, errors)) { return false; } break; case LENGTH: if (!addError(data.length() == Integer.parseInt(restrictionValue), fieldName, entity, "length=" + restrictionValue, ErrorType.INVALID_VALUE, errors)) { return false; } break; case MIN_LENGTH: if (!addError(data.length() >= Integer.parseInt(restrictionValue), fieldName, entity, "min-length=" + restrictionValue, ErrorType.INVALID_VALUE, errors)) { return false; } break; case MAX_LENGTH: if (!addError(data.length() <= Integer.parseInt(restrictionValue), fieldName, entity, "max-length=" + restrictionValue, ErrorType.INVALID_VALUE, errors)) { return false; } break; } } } } if (isRelaxedBlacklisted()) { for (AbstractBlacklistStrategy validationRule : relaxedValidationRuleList) { boolean isValid = validationRule.isValid("StringSchemaContext", data); if (!addError(isValid, fieldName, entity, "Invalid value caught by relaxed blacklisting strategy: " + validationRule.getIdentifier(), ErrorType.INVALID_VALUE, errors)) { return false; } } } else { for (AbstractBlacklistStrategy validationRule : validationRuleList) { boolean isValid = validationRule.isValid("StringSchemaContext", data); if (!addError(isValid, fieldName, entity, "Invalid value caught by strict blacklisting strategy: " + validationRule.getIdentifier(), ErrorType.INVALID_VALUE, errors)) { return false; } } } return true; } }
StringSchema extends NeutralSchema { @Override protected boolean validate(String fieldName, Object entity, List<ValidationError> errors, Repository<Entity> repo) { if (!addError(String.class.isInstance(entity), fieldName, entity, "String", ErrorType.INVALID_DATATYPE, errors)) { return false; } String data = (String) entity; if (this.getProperties() != null) { for (Entry<String, Object> entry : this.getProperties().entrySet()) { if (Restriction.isRestriction(entry.getKey())) { String restrictionValue = (String) entry.getValue(); switch (Restriction.fromValue(entry.getKey())) { case PATTERN: if (!addError(data.matches(restrictionValue), fieldName, entity, "pattern=" + restrictionValue, ErrorType.INVALID_VALUE, errors)) { return false; } break; case LENGTH: if (!addError(data.length() == Integer.parseInt(restrictionValue), fieldName, entity, "length=" + restrictionValue, ErrorType.INVALID_VALUE, errors)) { return false; } break; case MIN_LENGTH: if (!addError(data.length() >= Integer.parseInt(restrictionValue), fieldName, entity, "min-length=" + restrictionValue, ErrorType.INVALID_VALUE, errors)) { return false; } break; case MAX_LENGTH: if (!addError(data.length() <= Integer.parseInt(restrictionValue), fieldName, entity, "max-length=" + restrictionValue, ErrorType.INVALID_VALUE, errors)) { return false; } break; } } } } if (isRelaxedBlacklisted()) { for (AbstractBlacklistStrategy validationRule : relaxedValidationRuleList) { boolean isValid = validationRule.isValid("StringSchemaContext", data); if (!addError(isValid, fieldName, entity, "Invalid value caught by relaxed blacklisting strategy: " + validationRule.getIdentifier(), ErrorType.INVALID_VALUE, errors)) { return false; } } } else { for (AbstractBlacklistStrategy validationRule : validationRuleList) { boolean isValid = validationRule.isValid("StringSchemaContext", data); if (!addError(isValid, fieldName, entity, "Invalid value caught by strict blacklisting strategy: " + validationRule.getIdentifier(), ErrorType.INVALID_VALUE, errors)) { return false; } } } return true; } StringSchema();  StringSchema(String xsdType);  StringSchema(List<AbstractBlacklistStrategy> validationRuleList, List<AbstractBlacklistStrategy> relaxedValidationRuleList);  StringSchema(String xsdType, List<AbstractBlacklistStrategy> validationRuleList, List<AbstractBlacklistStrategy> relaxedValidationRuleList); }
StringSchema extends NeutralSchema { @Override protected boolean validate(String fieldName, Object entity, List<ValidationError> errors, Repository<Entity> repo) { if (!addError(String.class.isInstance(entity), fieldName, entity, "String", ErrorType.INVALID_DATATYPE, errors)) { return false; } String data = (String) entity; if (this.getProperties() != null) { for (Entry<String, Object> entry : this.getProperties().entrySet()) { if (Restriction.isRestriction(entry.getKey())) { String restrictionValue = (String) entry.getValue(); switch (Restriction.fromValue(entry.getKey())) { case PATTERN: if (!addError(data.matches(restrictionValue), fieldName, entity, "pattern=" + restrictionValue, ErrorType.INVALID_VALUE, errors)) { return false; } break; case LENGTH: if (!addError(data.length() == Integer.parseInt(restrictionValue), fieldName, entity, "length=" + restrictionValue, ErrorType.INVALID_VALUE, errors)) { return false; } break; case MIN_LENGTH: if (!addError(data.length() >= Integer.parseInt(restrictionValue), fieldName, entity, "min-length=" + restrictionValue, ErrorType.INVALID_VALUE, errors)) { return false; } break; case MAX_LENGTH: if (!addError(data.length() <= Integer.parseInt(restrictionValue), fieldName, entity, "max-length=" + restrictionValue, ErrorType.INVALID_VALUE, errors)) { return false; } break; } } } } if (isRelaxedBlacklisted()) { for (AbstractBlacklistStrategy validationRule : relaxedValidationRuleList) { boolean isValid = validationRule.isValid("StringSchemaContext", data); if (!addError(isValid, fieldName, entity, "Invalid value caught by relaxed blacklisting strategy: " + validationRule.getIdentifier(), ErrorType.INVALID_VALUE, errors)) { return false; } } } else { for (AbstractBlacklistStrategy validationRule : validationRuleList) { boolean isValid = validationRule.isValid("StringSchemaContext", data); if (!addError(isValid, fieldName, entity, "Invalid value caught by strict blacklisting strategy: " + validationRule.getIdentifier(), ErrorType.INVALID_VALUE, errors)) { return false; } } } return true; } StringSchema();  StringSchema(String xsdType);  StringSchema(List<AbstractBlacklistStrategy> validationRuleList, List<AbstractBlacklistStrategy> relaxedValidationRuleList);  StringSchema(String xsdType, List<AbstractBlacklistStrategy> validationRuleList, List<AbstractBlacklistStrategy> relaxedValidationRuleList); @Override NeutralSchemaType getSchemaType(); @Override Object convert(Object value); }
StringSchema extends NeutralSchema { @Override protected boolean validate(String fieldName, Object entity, List<ValidationError> errors, Repository<Entity> repo) { if (!addError(String.class.isInstance(entity), fieldName, entity, "String", ErrorType.INVALID_DATATYPE, errors)) { return false; } String data = (String) entity; if (this.getProperties() != null) { for (Entry<String, Object> entry : this.getProperties().entrySet()) { if (Restriction.isRestriction(entry.getKey())) { String restrictionValue = (String) entry.getValue(); switch (Restriction.fromValue(entry.getKey())) { case PATTERN: if (!addError(data.matches(restrictionValue), fieldName, entity, "pattern=" + restrictionValue, ErrorType.INVALID_VALUE, errors)) { return false; } break; case LENGTH: if (!addError(data.length() == Integer.parseInt(restrictionValue), fieldName, entity, "length=" + restrictionValue, ErrorType.INVALID_VALUE, errors)) { return false; } break; case MIN_LENGTH: if (!addError(data.length() >= Integer.parseInt(restrictionValue), fieldName, entity, "min-length=" + restrictionValue, ErrorType.INVALID_VALUE, errors)) { return false; } break; case MAX_LENGTH: if (!addError(data.length() <= Integer.parseInt(restrictionValue), fieldName, entity, "max-length=" + restrictionValue, ErrorType.INVALID_VALUE, errors)) { return false; } break; } } } } if (isRelaxedBlacklisted()) { for (AbstractBlacklistStrategy validationRule : relaxedValidationRuleList) { boolean isValid = validationRule.isValid("StringSchemaContext", data); if (!addError(isValid, fieldName, entity, "Invalid value caught by relaxed blacklisting strategy: " + validationRule.getIdentifier(), ErrorType.INVALID_VALUE, errors)) { return false; } } } else { for (AbstractBlacklistStrategy validationRule : validationRuleList) { boolean isValid = validationRule.isValid("StringSchemaContext", data); if (!addError(isValid, fieldName, entity, "Invalid value caught by strict blacklisting strategy: " + validationRule.getIdentifier(), ErrorType.INVALID_VALUE, errors)) { return false; } } } return true; } StringSchema();  StringSchema(String xsdType);  StringSchema(List<AbstractBlacklistStrategy> validationRuleList, List<AbstractBlacklistStrategy> relaxedValidationRuleList);  StringSchema(String xsdType, List<AbstractBlacklistStrategy> validationRuleList, List<AbstractBlacklistStrategy> relaxedValidationRuleList); @Override NeutralSchemaType getSchemaType(); @Override Object convert(Object value);  }