All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yodlee.api.model.validator.Validator Maven / Gradle / Ivy

There is a newer version: 1.0.29.beta1
Show newest version
/**
 * Copyright (c) 2019 Yodlee, Inc. All Rights Reserved.
 *
 * Licensed under the MIT License. See LICENSE file in the project root for license information.
 */
package com.yodlee.api.model.validator;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.ValidatorFactory;

public class Validator {

	public static List validate(final Object modelComponent) {
		final List problems = new ArrayList<>();
		if (modelComponent != null) {
			final Set> constraintViolations =
					APIModelValidatorFactory.SINGLE_INSTANCE.getValidator().validate(modelComponent);
			if (!constraintViolations.isEmpty()) {
				for (ConstraintViolation constraintViolation : constraintViolations) {
					Problem problem = new Problem(constraintViolation.getMessage(),
							constraintViolation.getPropertyPath().toString());
					problems.add(problem);
				}
			}
		}
		return problems;
	}

	public enum APIModelValidatorFactory {
		SINGLE_INSTANCE {

			private final transient ValidatorFactory avf = Validation.buildDefaultValidatorFactory();

			@Override
			public javax.validation.Validator getValidator() {
				return avf.getValidator();
			}
		};

		public abstract javax.validation.Validator getValidator();
	}
}