@SuppressWarnings({"unchecked", "rawtypes"}) @Override public boolean isValid(Object value, ConstraintValidatorContext context) { final ViolationCollector collector = new ViolationCollector(context); context.disableDefaultConstraintViolation(); for (ValidationCaller caller : methodMap.computeIfAbsent(value.getClass(), this::findMethods)) { caller.setValidationObject(value); caller.call(collector); } return !collector.hasViolationOccurred(); }
@Override public boolean isValid(MultipleJGitEnvironmentProperties sshUriProperties, ConstraintValidatorContext context) { String knownHostsFile = sshUriProperties.getKnownHostsFile(); if (knownHostsFile != null && !new File(knownHostsFile).exists()) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate( format("File '%s' specified in property 'spring.cloud.config.server.git.knownHostsFile' could not be located", knownHostsFile)) .addConstraintViolation(); return false; } return true; } }
@Override public boolean isValid(Double aDouble, ConstraintValidatorContext context) { if (aDouble == 0) { context.disableDefaultConstraintViolation(); // disable violation message context.buildConstraintViolationWithTemplate("may not be zero").addConstraintViolation(); // add message return false; } return true; } }
@Override public boolean isValid(Map<String, String> map, ConstraintValidatorContext context) { for (String key : map.keySet()) { if (key == null || key.isEmpty()) { context.disableDefaultConstraintViolation(); // disable violation message context.buildConstraintViolationWithTemplate("key value cannot be null or empty").addConstraintViolation(); // add message return false; } if (map.get(key) == null || map.get(key).isEmpty()) { context.disableDefaultConstraintViolation(); // disable violation message context.buildConstraintViolationWithTemplate("value cannot be null or empty for key: " + key).addConstraintViolation(); // add message return false; } } return true; } }
private boolean isHostKeySpecifiedWhenAlgorithmSet(JGitEnvironmentProperties sshUriProperties, ConstraintValidatorContext context) { if (hasText(sshUriProperties.getHostKeyAlgorithm()) && !hasText(sshUriProperties.getHostKey())) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate( format("Property '%shostKey' must be set when '%shostKeyAlgorithm' is specified", GIT_PROPERTY_PREFIX, GIT_PROPERTY_PREFIX)) .addConstraintViolation(); return false; } return true; }
private boolean isAlgorithmSpecifiedWhenHostKeySet(JGitEnvironmentProperties sshUriProperties, ConstraintValidatorContext context) { if (hasText(sshUriProperties.getHostKey()) && !hasText(sshUriProperties.getHostKeyAlgorithm())) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate( format("Property '%shostKeyAlgorithm' must be set when '%shostKey' is specified", GIT_PROPERTY_PREFIX, GIT_PROPERTY_PREFIX)) .addConstraintViolation(); return false; } return true; } }
@Override public boolean isValid(MultipleJGitEnvironmentProperties sshUriProperties, ConstraintValidatorContext context) { context.disableDefaultConstraintViolation(); Set<Boolean> validationResults = new HashSet<>(); List<JGitEnvironmentProperties> extractedProperties = sshPropertyValidator.extractRepoProperties(sshUriProperties); for (JGitEnvironmentProperties extractedProperty : extractedProperties) { if (sshUriProperties.isIgnoreLocalSshSettings() && isSshUri(extractedProperty.getUri())) { validationResults.add(isHostKeySpecifiedWhenAlgorithmSet(extractedProperty, context)); } } return !validationResults.contains(false); }
@Override public boolean isValid(MultipleJGitEnvironmentProperties sshUriProperties, ConstraintValidatorContext context) { context.disableDefaultConstraintViolation(); Set<Boolean> validationResults = new HashSet<>(); List<JGitEnvironmentProperties> extractedProperties = sshPropertyValidator.extractRepoProperties(sshUriProperties); for (JGitEnvironmentProperties extractedProperty : extractedProperties) { if (extractedProperty.isIgnoreLocalSshSettings() && isSshUri(extractedProperty.getUri())) { validationResults.add( isPrivateKeyPresent(extractedProperty, context) && isPrivateKeyFormatCorrect(extractedProperty, context)); } } return !validationResults.contains(false); }
@Override public boolean isValid(List<String> list, ConstraintValidatorContext context) { context.disableDefaultConstraintViolation(); boolean valid = true; for (int i = 0; i < list.size(); i++) { if ("X".equals(list.get(i))) { context.buildConstraintViolationWithTemplate(context.getDefaultConstraintMessageTemplate()).addBeanNode().inIterable().atIndex(i).addConstraintViolation(); valid = false; } } return valid; } }
@Override public boolean isValid(ValidPerson value, ConstraintValidatorContext context) { if (value.expectsAutowiredValidator) { assertNotNull(this.environment); } boolean valid = (value.name == null || !value.address.street.contains(value.name)); if (!valid && "Phil".equals(value.name)) { context.buildConstraintViolationWithTemplate( context.getDefaultConstraintMessageTemplate()).addPropertyNode("address").addConstraintViolation().disableDefaultConstraintViolation(); } return valid; } }
@Override public boolean isValid(ValidPerson value, ConstraintValidatorContext context) { if (value.expectsAutowiredValidator) { assertNotNull(this.environment); } boolean valid = (value.name == null || !value.address.street.contains(value.name)); if (!valid && "Phil".equals(value.name)) { context.buildConstraintViolationWithTemplate( context.getDefaultConstraintMessageTemplate()).addPropertyNode("address").addConstraintViolation().disableDefaultConstraintViolation(); } return valid; } }
@Override public boolean isValid(List<String> list, ConstraintValidatorContext context) { context.disableDefaultConstraintViolation(); boolean valid = true; for (int i = 0; i < list.size(); i++) { if ("X".equals(list.get(i))) { context.buildConstraintViolationWithTemplate(context.getDefaultConstraintMessageTemplate()).addBeanNode().inIterable().atIndex(i).addConstraintViolation(); valid = false; } } return valid; } }
public boolean isValid(String value, ConstraintValidatorContext context) { if (!TimeUnit.contains(value)) { context.disableDefaultConstraintViolation(); // disable violation message context.buildConstraintViolationWithTemplate("must be one of " + TimeUnit.toValueNames()).addConstraintViolation(); // add message return false; } return true; } }
@Override public boolean isValid(WorkflowDef workflowDef, ConstraintValidatorContext context) { context.disableDefaultConstraintViolation(); boolean valid = true; //check if taskReferenceNames are unique across tasks or not HashMap<String, Integer> taskReferenceMap = new HashMap<>(); for (WorkflowTask workflowTask : workflowDef.getTasks()) { if (taskReferenceMap.containsKey(workflowTask.getTaskReferenceName())) { String message = String.format("taskReferenceName: %s should be unique across tasks for a given workflowDefinition: %s", workflowTask.getTaskReferenceName(), workflowDef.getName()); context.buildConstraintViolationWithTemplate(message).addConstraintViolation(); valid = false; } else { taskReferenceMap.put(workflowTask.getTaskReferenceName(), 1); } } //check inputParameters points to valid taskDef return valid & verifyTaskInputParameters(context, workflowDef); }
public boolean isValid(Object value, ConstraintValidatorContext context) { BeanWrapper beanWrapper = new BeanWrapperImpl(value); Object fieldValue = beanWrapper.getPropertyValue(field); Object comparingFieldValue = beanWrapper.getPropertyValue(comparingField); boolean matched = ObjectUtils.nullSafeEquals(fieldValue, comparingFieldValue); if (matched) { return true; } else { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate(message) .addPropertyNode(field) .addConstraintViolation(); return false; } } }
public boolean isValid(Object value, ConstraintValidatorContext context) { BeanWrapper beanWrapper = new BeanWrapperImpl(value); Object fieldValue = beanWrapper.getPropertyValue(field); Object comparingFieldValue = beanWrapper.getPropertyValue(comparingField); boolean matched = ObjectUtils.nullSafeEquals(fieldValue, comparingFieldValue); if (matched) { return true; } else { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate(message) .addPropertyNode(field) .addConstraintViolation(); return false; } } }
@Override public boolean isValid(InnerBean bean, ConstraintValidatorContext context) { context.disableDefaultConstraintViolation(); if (bean.getValue() == null) { context.buildConstraintViolationWithTemplate("NULL").addPropertyNode("value").addConstraintViolation(); return false; } return true; } }
@Override public boolean isValid(InnerBean bean, ConstraintValidatorContext context) { context.disableDefaultConstraintViolation(); if (bean.getValue() == null) { context.buildConstraintViolationWithTemplate("NULL").addPropertyNode("value").addConstraintViolation(); return false; } return true; } }
@Override public boolean isValid(TaskDef taskDef, ConstraintValidatorContext context) { context.disableDefaultConstraintViolation(); boolean valid = true; if (taskDef.getTimeoutSeconds() > 0) { if (taskDef.getResponseTimeoutSeconds() > taskDef.getTimeoutSeconds()) { valid = false; String message = String.format("TaskDef: %s responseTimeoutSeconds: %d must be less than timeoutSeconds: %d", taskDef.getName(), taskDef.getResponseTimeoutSeconds(), taskDef.getTimeoutSeconds()); context.buildConstraintViolationWithTemplate(message).addConstraintViolation(); } } return valid; } }
@Override public boolean isValid(WorkflowTask workflowTask, ConstraintValidatorContext context) { context.disableDefaultConstraintViolation(); if (workflowTask == null) { return true; } if (workflowTask.getName() == null) { String message = "WorkflowTask name cannot be empty or null"; context.buildConstraintViolationWithTemplate(message).addConstraintViolation(); } boolean valid = true; // avoid task type definition check incase of non simple task if (!workflowTask.getType().equals(TASK_TYPE_SIMPLE)) { return valid; } if (ValidationContext.getMetadataDAO().getTaskDef(workflowTask.getName()) == null) { //check if task type is ephemeral TaskDef task = workflowTask.getTaskDefinition(); if (task == null) { valid = false; String message = String.format("workflowTask: %s task definition is not defined", workflowTask.getName()); context.buildConstraintViolationWithTemplate(message).addConstraintViolation(); } } return valid; } }