kandi background
Explore Kits

validator-spring | Bean Validator utilizing Spring Expression Language | Validation library

 by   jirutka Java Version: Current License: No License

 by   jirutka Java Version: Current License: No License

Download this library from

kandi X-RAY | validator-spring Summary

validator-spring is a Java library typically used in Utilities, Validation, Spring Boot, Spring, Swagger, JPA applications. validator-spring has no bugs, it has no vulnerabilities, it has build file available and it has low support. You can download it from GitHub, Maven.
This library provides Bean Validation (JSR 303/349) constraint that allows to use powerful Spring Expression Language (SpEL) for non-trivial validations. It’s especially very useful for cross-field validations that are very complicated with a plain Bean Validation.
Support
Support
Quality
Quality
Security
Security
License
License
Reuse
Reuse

kandi-support Support

  • validator-spring has a low active ecosystem.
  • It has 75 star(s) with 21 fork(s). There are 8 watchers for this library.
  • It had no major release in the last 12 months.
  • There are 2 open issues and 5 have been closed. On average issues are closed in 200 days. There are 1 open pull requests and 0 closed requests.
  • It has a neutral sentiment in the developer community.
  • The latest version of validator-spring is current.
validator-spring Support
Best in #Validation
Average in #Validation
validator-spring Support
Best in #Validation
Average in #Validation

quality kandi Quality

  • validator-spring has 0 bugs and 1 code smells.
validator-spring Quality
Best in #Validation
Average in #Validation
validator-spring Quality
Best in #Validation
Average in #Validation

securitySecurity

  • validator-spring has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.
  • validator-spring code analysis shows 0 unresolved vulnerabilities.
  • There are 0 security hotspots that need review.
validator-spring Security
Best in #Validation
Average in #Validation
validator-spring Security
Best in #Validation
Average in #Validation

license License

  • validator-spring does not have a standard license declared.
  • Check the repository for any license declaration and review the terms closely.
  • Without a license, all rights are reserved, and you cannot use the library in your applications.
validator-spring License
Best in #Validation
Average in #Validation
validator-spring License
Best in #Validation
Average in #Validation

buildReuse

  • validator-spring releases are not available. You will need to build from source code and install.
  • Deployable package is available in Maven.
  • Build file is available. You can build the component from source.
  • Installation instructions are not available. Examples and code snippets are available.
  • validator-spring saves you 114 person hours of effort in developing the same functionality from scratch.
  • It has 288 lines of code, 19 functions and 5 files.
  • It has medium code complexity. Code complexity directly impacts maintainability of the code.
validator-spring Reuse
Best in #Validation
Average in #Validation
validator-spring Reuse
Best in #Validation
Average in #Validation
Top functions reviewed by kandi - BETA

kandi has reviewed validator-spring and discovered the below as its top functions. This is intended to give you an instant insight into validator-spring implemented functionality, and help decide if they suit your requirements.

  • Create a new evaluation context .
    • Recursively inspect all registered functions .
      • Converts the given value to the target type .
        • Initialize this SpELAssert .
          • Evaluates if the given object is valid .
            • Extract static methods from a class .
              • Determines if the source type can convert the source type to the target type .

                Get all kandi verified functions for this library.

                Get all kandi verified functions for this library.

                validator-spring Key Features

                Bean Validator utilizing Spring Expression Language (SpEL)

                Cross-field validation

                copy iconCopydownload iconDownload
                @SpELAssert(value = "hasRedirectUris()", applyIf = "grantTypes.contains('auth_code')",
                            message = "{validator.missing_redirect_uri}")
                public class ClientDTO {
                
                	private Collection<String> grantTypes;
                	private Collection<String> redirectUris;
                
                    public boolean hasRedirectUris() {
                        return !redirectUris.isEmpty();
                    }
                }

                Using helper functions

                copy iconCopydownload iconDownload
                @SpELAssert(value = "#isEven(count) && count > 42", applyIf = "enabled",
                            helpers = Helpers.class)
                public class Sample {
                
                    private int count;
                    private boolean enabled;
                }

                Using Spring beans

                copy iconCopydownload iconDownload
                public class Sample {
                
                    @SpELAssert("@myService.calculate(#this) > 42")
                    private int value;
                }

                Maven

                copy iconCopydownload iconDownload
                <dependency>
                    <groupId>cz.jirutka.validator</groupId>
                    <artifactId>validator-spring</artifactId>
                    <version>1.1.0</version>
                </dependency>

                Community Discussions

                Trending Discussions on validator-spring
                • Empty messages with Validation errors in Spring Data REST
                Trending Discussions on validator-spring

                QUESTION

                Empty messages with Validation errors in Spring Data REST

                Asked 2017-Oct-15 at 11:09

                I'm creating an application using Spring Boot, Spring Data REST, Spring HATEOAS, Hibernate, Spring Validation.

                I created my own validation to support SpEL following this guide.

                So I've my Validator:

                  public class SpELClassValidator implements ConstraintValidator<ValidateClassExpression, Object> {
                    private Logger log = LogManager.getLogger();
                
                    private ValidateClassExpression annotation;
                    private ExpressionParser parser = new SpelExpressionParser();
                
                    public void initialize(ValidateClassExpression constraintAnnotation) {
                        annotation = constraintAnnotation;
                        parser.parseExpression(constraintAnnotation.value());
                    }
                
                    public boolean isValid(Object value, ConstraintValidatorContext context) {
                        try {           
                            StandardEvaluationContext spelContext = new StandardEvaluationContext(value);
                            return (Boolean) parser.parseExpression(annotation.value()).getValue(spelContext);
                        } catch (Exception e) {
                            log.error("", e);
                            return false;
                        }
                
                    }
                }
                

                and my annotation:

                @Target({ java.lang.annotation.ElementType.TYPE, java.lang.annotation.ElementType.ANNOTATION_TYPE })
                @Retention(RetentionPolicy.RUNTIME)
                @Constraint(validatedBy = { SpELClassValidator.class })
                @Documented
                @Repeatable(ValidateClassExpressions.class)
                public @interface ValidateClassExpression {
                
                    String message() default "{expression.validation.message}";
                
                    Class<?>[] groups() default {};
                
                    Class<? extends Payload>[] payload() default {};
                
                    String value();
                
                }
                

                Configuration of validator:

                @Bean
                public MessageSource messageSource() {
                    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
                    messageSource.setBasenames("classpath:/i18n/messages");
                    // messageSource.setDefaultEncoding("UTF-8");
                    // set to true only for debugging
                    messageSource.setUseCodeAsDefaultMessage(false);
                    messageSource.setCacheSeconds((int) TimeUnit.HOURS.toSeconds(1));
                    messageSource.setFallbackToSystemLocale(false);
                    return messageSource;
                }
                
                /**
                 * Enable Spring bean validation
                 * https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#validation
                 * 
                 * @return
                 */
                @Bean   
                public LocalValidatorFactoryBean validator() {
                    LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean();
                    factoryBean.setValidationMessageSource(messageSource());
                    return factoryBean;
                }
                
                @Bean
                public MethodValidationPostProcessor methodValidationPostProcessor() {
                    MethodValidationPostProcessor methodValidationPostProcessor = new MethodValidationPostProcessor();
                    methodValidationPostProcessor.setValidator(validator());
                    return methodValidationPostProcessor;
                }
                

                ..and defined validator for REST repositories:

                    @Configuration
                public class RestConfig extends RepositoryRestConfigurerAdapter {
                    @Autowired
                    private Validator validator;
                
                    public static final DateTimeFormatter ISO_FIXED_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'")
                            .withZone(ZoneId.of("Z"));
                
                    @Bean
                    public RootResourceProcessor rootResourceProcessor() {
                        return new RootResourceProcessor();
                    }
                
                    @Override
                    public void configureExceptionHandlerExceptionResolver(ExceptionHandlerExceptionResolver exceptionResolver) {
                
                    }
                
                    @Override
                    public void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener validatingListener) {
                        validatingListener.addValidator("beforeCreate", validator);
                        validatingListener.addValidator("beforeSave", validator);
                        super.configureValidatingRepositoryEventListener(validatingListener);
                    }
                }
                

                this is my bean:

                    @Entity
                // Validate the number of seats if the bus is a minibus
                @ValidateClassExpression(value = "#this.isMiniBus() == true ? #this.getSeats()<=17 : true", message = "{Expression.licenseplate.validminibus}")
                public class LicensePlate extends AbstractEntity {
                    private static final long serialVersionUID = -6871697166535810224L;
                
                    @NotEmpty
                    @ColumnTransformer(read = "UPPER(licensePlate)", write = "UPPER(?)")
                    @Column(nullable = false, unique = true)
                    private String licensePlate;
                
                    // The engine euro level (3,4,5,6)
                    @Range(min = 0, max = 6)
                    @NotNull
                    @Column(nullable = false, columnDefinition = "INTEGER default 0")
                    private int engineEuroLevel = 0;
                
                    @NotNull(message = "{NotNull.licenseplate.enginetype}")
                    @Enumerated(EnumType.STRING)
                    @Column(nullable = false)
                    private EngineType engineType = EngineType.DIESEL;
                
                    // If the bus has the particulate filter
                    @NotNull(message = "{NotNull.licenseplate.particulatefilter}")
                    @Column(nullable = false, columnDefinition = "BOOLEAN default false")
                    private boolean particulateFilter = false;
                
                    // Number of seats
                    @NotNull
                    @Range(min = 1, max = 99)
                    @Column(nullable = false, columnDefinition = "INTEGER default 50")
                    private int seats = 50;
                
                    // If the vehicle is a minibus
                    @NotNull
                    @Column(nullable = false, columnDefinition = "BOOLEAN default false")
                    private boolean miniBus = false;
                
                    @NotNull(message = "{NotNull.licenseplate.country}")
                    // The country of the vehicle
                    @ManyToOne(fetch = FetchType.LAZY, optional = false)
                    private Country country;
                
                    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
                    private List<Note> notes = new ArrayList<>();
                
                    public LicensePlate() {
                    }
                
                    public String getLicensePlate() {
                        return licensePlate;
                    }
                
                    public void setLicensePlate(String licensePlate) {
                        this.licensePlate = licensePlate;
                    }
                
                    public int getEngineEuroLevel() {
                        return engineEuroLevel;
                    }
                
                    public void setEngineEuroLevel(int engineEuroLevel) {
                        this.engineEuroLevel = engineEuroLevel;
                    }
                
                    public int getSeats() {
                        return seats;
                    }
                
                    public void setSeats(int seats) {
                        this.seats = seats;
                    }
                
                    public boolean isMiniBus() {
                        return miniBus;
                    }
                
                    public void setMiniBus(boolean miniBus) {
                        this.miniBus = miniBus;
                    }
                
                    public EngineType getEngineType() {
                        return engineType;
                    }
                
                    public void setEngineType(EngineType engineType) {
                        this.engineType = engineType;
                    }
                
                    public boolean isParticulateFilter() {
                        return particulateFilter;
                    }
                
                    public void setParticulateFilter(boolean particulateFilter) {
                        this.particulateFilter = particulateFilter;
                    }
                
                    public Country getCountry() {
                        return country;
                    }
                
                    public void setCountry(Country country) {
                        this.country = country;
                    }
                
                    @Override
                    public String toString() {
                        return "LicensePlate [licensePlate=" + licensePlate + ", engineEuroLevel=" + engineEuroLevel + ", engineType="
                                + engineType + ", particulateFilter=" + particulateFilter + ", seats=" + seats + ", miniBus=" + miniBus
                                + "]";
                    }
                
                    public List<Note> getNotes() {
                        return notes;
                    }
                
                    public void setNotes(List<Note> notes) {
                        this.notes = notes;
                    }
                
                }
                

                On configuration I've also this class:

                @RestControllerAdvice
                public class ApplicationExceptionHandler extends ResponseEntityExceptionHandler {
                
                    @Override
                    protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex,
                            HttpHeaders headers, HttpStatus status, WebRequest request) {
                        throw new RuntimeException(ex);
                    }
                
                    @Override
                    protected ResponseEntity<Object> handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex,
                            HttpHeaders headers, HttpStatus status, WebRequest request) {
                        throw new RuntimeException(ex);
                    }
                
                    @Override
                    protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex,
                            HttpHeaders headers, HttpStatus status, WebRequest request) {
                        throw new RuntimeException(ex);
                    }
                
                }
                

                Using my repository:

                @Transactional
                @RepositoryRestResource(excerptProjection = LicensePlateProjection.class)
                @PreAuthorize("isAuthenticated()")
                public interface LicensePlateRepository
                        extends PagingAndSortingRepository<LicensePlate, Long>, RevisionRepository<LicensePlate, Long, Integer> {
                
                    public LicensePlate findByLicensePlate(String licencePlate);
                

                Using Swagger I'm doing a POST of this json:

                {"licensePlate":"asdfg","engineEuroLevel":"4","particulateFilter":true,"seats":18,"miniBus":true,"country":"http://localhost:8080/api/v1/countries/1"}
                

                Because I've the validation rule that check a minibus has less than 17 seats, I should see a validation error, instad I see this:

                 {
                  "errors": []
                }
                

                with an HTTP 400 error (this return code is right).

                I've to point out that I created Junit test cases and I see the right message:

                @Test
                @WithMockUser(roles = "ADMIN")
                public void validateMinibusWithMoreThan17SeatsFails() {
                    assertEquals(1, countryRepository.count());
                
                    LicensePlate plate = new LicensePlate();
                    plate.setLicensePlate("AA123BB");
                    plate.setEngineEuroLevel(3);
                    plate.setMiniBus(true);
                    plate.setSeats(18);
                    plate.setCountry(countryRepository.findFirstByOrderByIdAsc());
                
                    Set<ConstraintViolation<LicensePlate>> constraintViolations = validator.validate(plate);
                    assertEquals(1, constraintViolations.size());
                    ConstraintViolation<LicensePlate> constraintViolation = constraintViolations.iterator().next();
                    assertEquals("I veicoli di tipo minibus possono avere al massimo 17 posti (16 passeggeri più il conducente).",
                            constraintViolation.getMessage());
                }
                

                So I guess the problem is on the REST/MVC part. I debugged the request and I checked the class org.springframework.data.rest.core.RepositoryConstraintViolationException; in the constructor i see my errors are right and I can see the error message and the right structure:

                org.springframework.data.rest.core.ValidationErrors: 1 errors
                Error in object 'LicensePlate': codes [ValidateClassExpression.LicensePlate,ValidateClassExpression]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [LicensePlate.,]; arguments []; default message [],org.springframework.validation.beanvalidation.SpringValidatorAdapter$ResolvableAttribute@520b6a25]; default message [I veicoli di tipo minibus possono avere al massimo 17 posti (16 passeggeri più il conducente).]
                

                I can't see where I am making the mistake. With other (also) custom validators I see the right message. I someone also to put me in the right direction to solve the problem?

                ANSWER

                Answered 2017-Oct-15 at 11:09

                I reckon that Spring MVC doesn't know where to show the error message as the constraint violation of the class-level constraint doesn't indicate any specific property.

                HV's @ScriptAssert provides the reportOn() attribute for specifying a property to report the error on.

                For your custom constraint you could do the same by creating a customized constraint violation and property path using the API exposed via ConstraintValidatorContext.

                Source https://stackoverflow.com/questions/46670224

                Community Discussions, Code Snippets contain sources that include Stack Exchange Network

                Vulnerabilities

                No vulnerabilities reported

                Install validator-spring

                You can download it from GitHub, Maven.
                You can use validator-spring like any standard Java library. Please include the the jar files in your classpath. You can also use any IDE and you can run and debug the validator-spring component as you would do with any other Java program. Best practice is to use a build tool that supports dependency management such as Maven or Gradle. For Maven installation, please refer maven.apache.org. For Gradle installation, please refer gradle.org .

                Support

                For any new features, suggestions and bugs create an issue on GitHub. If you have any questions check and ask questions on community page Stack Overflow .

                DOWNLOAD this Library from

                Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from
                over 430 million Knowledge Items
                Find more libraries
                Reuse Solution Kits and Libraries Curated by Popular Use Cases
                Explore Kits

                Save this library and start creating your kit

                Share this Page

                share link
                Consider Popular Validation Libraries
                Try Top Libraries by jirutka
                Compare Validation Libraries with Highest Support
                Compare Validation Libraries with Highest Quality
                Compare Validation Libraries with Highest Security
                Compare Validation Libraries with Permissive License
                Compare Validation Libraries with Highest Reuse
                Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from
                over 430 million Knowledge Items
                Find more libraries
                Reuse Solution Kits and Libraries Curated by Popular Use Cases
                Explore Kits

                Save this library and start creating your kit

                • © 2022 Open Weaver Inc.