-
Notifications
You must be signed in to change notification settings - Fork 0
/
data-validation-overview.html
18 lines (17 loc) · 13.5 KB
/
data-validation-overview.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!DOCTYPE html><html lang="de-ch"><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Data Validation Overview - Finecloud</title><meta name="description" content="What is Validation? Validation is a process of making assertions against data to ensure data integrity Is a value required? How long is a phone number? Is it a good date? What is the maximum length of a string? Some refer to data validation as…"><meta name="generator" content="Publii Open-Source CMS for Static Site"><link rel="stylesheet" href="https://www.finecloud.ch/media/plugins/syntaxHighlighter/prism-black.css"><link rel="canonical" href="https://www.finecloud.ch/data-validation-overview.html"><link rel="alternate" type="application/atom+xml" href="https://www.finecloud.ch/feed.xml"><link rel="alternate" type="application/json" href="https://www.finecloud.ch/feed.json"><meta property="og:title" content="Data Validation Overview"><meta property="og:site_name" content="Finecloud"><meta property="og:description" content="What is Validation? Validation is a process of making assertions against data to ensure data integrity Is a value required? How long is a phone number? Is it a good date? What is the maximum length of a string? Some refer to data validation as…"><meta property="og:url" content="https://www.finecloud.ch/data-validation-overview.html"><meta property="og:type" content="article"><link rel="shortcut icon" href="https://www.finecloud.ch/media/website/finecloud.png" type="image/png"><link rel="stylesheet" href="https://www.finecloud.ch/assets/css/style.css?v=39da73365516a098a9b73b721fc970e2"><script type="application/ld+json">{"@context":"http://schema.org","@type":"Article","mainEntityOfPage":{"@type":"WebPage","@id":"https://www.finecloud.ch/data-validation-overview.html"},"headline":"Data Validation Overview","datePublished":"2023-05-02T20:07","dateModified":"2023-05-02T21:45","description":"What is Validation? Validation is a process of making assertions against data to ensure data integrity Is a value required? How long is a phone number? Is it a good date? What is the maximum length of a string? Some refer to data validation as…","author":{"@type":"Person","name":"Finecloud","url":"https://www.finecloud.ch/authors/finecloud/"},"publisher":{"@type":"Organization","name":"Finecloud"}}</script><meta name="google-site-verification" content="seFY9U12uiEq5U3_MyZiX6XWzk0AVFl9zITr2ZKsytY"></head><body><div class="site-container"><header class="top" id="js-header"><a class="logo" href="https://www.finecloud.ch/">Finecloud</a><nav class="navbar js-navbar"><button class="navbar__toggle js-toggle" aria-label="Menu" aria-haspopup="true" aria-expanded="false"><span class="navbar__toggle-box"><span class="navbar__toggle-inner">Menu</span></span></button><ul class="navbar__menu"><li><a href="https://www.finecloud.ch/" target="_self">Blog</a></li><li><a href="https://www.finecloud.ch/tags/" target="_self">Tags</a></li></ul></nav><div class="search"><div class="search__overlay js-search-overlay"><div class="search__overlay-inner"><form action="https://www.finecloud.ch/search.html" class="search__form"><input class="search__input js-search-input" type="search" name="q" placeholder="search..." aria-label="search..." autofocus="autofocus"></form><button class="search__close js-search-close" aria-label="Close">Close</button></div></div><button class="search__btn js-search-btn" aria-label="Search"><svg role="presentation" focusable="false"><use xlink:href="https://www.finecloud.ch/assets/svg/svg-map.svg#search"/></svg></button></div></header><main><article class="post"><div class="hero"><figure class="hero__image hero__image--overlay"><img src="https://www.finecloud.ch/media/website/download.jpg" srcset="https://www.finecloud.ch/media/website/responsive/download-xs.jpg 300w, https://www.finecloud.ch/media/website/responsive/download-sm.jpg 480w, https://www.finecloud.ch/media/website/responsive/download-md.jpg 768w, https://www.finecloud.ch/media/website/responsive/download-lg.jpg 1024w, https://www.finecloud.ch/media/website/responsive/download-xl.jpg 1360w, https://www.finecloud.ch/media/website/responsive/download-2xl.jpg 1600w" sizes="100vw" loading="eager" alt=""></figure><header class="hero__content"><div class="wrapper"><div class="post__meta"><time datetime="2023-05-02T20:07">Mai 2, 2023</time></div><h1>Data Validation Overview</h1></div></header></div><div class="wrapper post__entry"><div class="post__toc"><h3>Table of Contents</h3><ul><li><a href="#mcetoc_1gvevuu5v9p">What is Validation?</a></li><li><a href="#mcetoc_1gvevuu5v9q">When to Validate?</a></li><li><a href="#mcetoc_1gvevuu5v9r">Java Bean Validation</a></li><li><a href="#mcetoc_1gvevuu5v9s">Jakarta Bean Validation 3.0</a></li><li><a href="#mcetoc_1gvevuu5v9t">Built In Constraint Definitions</a></li><li><a href="#mcetoc_1gvevuu5v9u">Hiberate Validator Constraints</a></li><li><a href="#mcetoc_1gvevuu5v9v">Validation and Spring Framework</a></li><li><a href="#mcetoc_1gvevuu5va0">Spring Boot and Validation</a></li></ul></div><h2 id="mcetoc_1gvevuu5v9p">What is Validation?</h2><ul><li>Validation is a process of making assertions against data to ensure data integrity</li><li>Is a value required? How long is a phone number?</li><li>Is it a good date? What is the maximum length of a string?</li><li>Some refer to data validation as defensive programming</li><li>Or a process of trust but verify</li><li>Validation is an important step, but easily overlooked</li></ul><h2 id="mcetoc_1gvevuu5v9q">When to Validate?</h2><ul><li>Validate data early and often!</li><li>Validation should occur with every exchange</li><li>User input data should be validated in the UI with rich user feedback</li><li>RESTful API data should be validated early in the controller, before the service layer</li><li>Data should be validated before persistence to the database</li><li>Database constraints will also enforce data validation</li><li>Best to validate early - Handling persistence errors is ugly</li></ul><h2 id="mcetoc_1gvevuu5v9r">Java Bean Validation</h2><ul><li>Java Bean Validation is a Java API standard</li><li>Provides for a standard way of performing validation and handling errors</li><li>Much more graceful than custom code blocks of if… then… throw Exception</li><li>Bean Validation is an API, like JPA or JDBC you need an implementation</li><li>Fun Fact - Gunnar Morling, founder of MapStruct is the spec lead for the Bean Validation API and contributor of the Hibernate Implementation of the Bean Validation API</li></ul><h2 id="mcetoc_1gvevuu5v9s">Jakarta Bean Validation 3.0</h2><ul><li>Released July of 2020</li><li>Name changed from Bean Validation to Jakarta Bean Validation</li><li>Only change from 2.0 to 3.0 is the API package changes</li><li>2.0 - javax.validation</li><li>3.0 - jakarta.validaton</li><li>Used in Spring Framework 6.x+</li><li>Hibernate Validator 7.x+ is the implementation</li></ul><h2 id="mcetoc_1gvevuu5v9t">Built In Constraint Definitions</h2><ul><li>@Null - Checks value is null</li><li>@NotNull - Checks value is not null</li><li>@AssertTrue - Value is true</li><li>@AssertFalse - Value is false</li><li>@Min - Number is equal or higher</li><li>@Max - Number is equal or less</li><li>@DecimalMin - Value is larger</li><li>@DecimalMax - Value is less than</li><li>@Negative - Value is less than zero. Zero invalid.</li><li>@NegativeOrZero - Value is zero or less than zero</li><li>@Positive - Value is greater than zero. Zero invalid.</li><li>@PositiveOrZero - Value is zero or greater than zero.</li><li>@Size - checks if string or collection is between a min and max</li><li>@Digits - check for integer digits and fraction digits</li><li>@Past - Checks if date is in past</li><li>@PastOrPresent - Checks if date is in past or present</li><li>@Future - Checks if date is in future</li><li>@FutureOrPresent - Checks if date is present or in future</li><li>@Pattern - checks against RegEx pattern</li><li>@NotEmpty - Checks if value is not null nor empty (whitespace characters or empty collection)</li><li>@NonBlank - Checks string is not null or not whitespace characters</li><li>@Email - Checks if string value is an email address</li></ul><h2 id="mcetoc_1gvevuu5v9u">Hiberate Validator Constraints</h2><ul><li>@ScriptAssert - Class level annotation, checks class against script</li><li>@CreditCardNumber - Verifies value is a credit card number</li><li>@Currency - Valid currency amount</li><li>@DurationMax - Duration less than given value</li><li>@DurationMin - Duration greater than given value</li><li>@EAN - Valid EAN barcode</li><li>@ISBN - Valid ISBN value</li><li>@Length - String length between given min and max</li><li>@CodePointLength - Validates that code point length of the annotated character sequence is between min and max included.</li><li>@LuhnCheck - Luhn check sum</li><li>@Mod10Check - Mod 10 check sum</li><li>@Mod11Check - Mod 11 check sum</li><li>@Range - checks if number is between given min and max (inclusive)</li><li>@SafeHtml - Checks for safe HTML</li><li>@UniqueElements - Checks if collection has unique elements</li><li>@Url - checks for valid URL</li><li>@CNPJ - Brazilian Corporate Tax Payer Registry Number</li><li>@CPF - Brazilian Individual Taxpayer Registry Number</li><li>@TituloEleitoral - Brazilian voter ID</li><li>@NIP - Polish VAR ID</li><li>@PESEL - Polish National Validation Number</li><li>@REGON - Polish Taxpayer ID</li></ul><h2 id="mcetoc_1gvevuu5v9v">Validation and Spring Framework</h2><ul><li>Spring Framework has robust support for bean validation</li><li>Validation support can be used in controllers, and services, and other Spring managed components</li><li>Spring MVC will return a 400 Bad Request Error for validation failures</li><li>Spring Data JPA with throw an exception for JPA constraint violations</li></ul><h2 id="mcetoc_1gvevuu5va0">Spring Boot and Validation</h2><ul><li>Spring Boot will auto-configure validation when the validation implementation is found on classpath</li><li>If API is only on classpath (with no implementation) you can use the annotations, BUT validation will NOT occur</li><li>Prior to Spring Boot 2.3, validation was included in starter dependencies</li><li>After Spring Boot 2.3, you must include the Spring Boot validation starter</li></ul></div><footer class="wrapper post__footer"><p class="post__last-updated">This article was updated on Mai 2, 2023</p><ul class="post__tag"><li><a href="https://www.finecloud.ch/tags/java/">java</a></li><li><a href="https://www.finecloud.ch/tags/softwareentwicklung/">software development</a></li><li><a href="https://www.finecloud.ch/tags/spring/">spring</a></li><li><a href="https://www.finecloud.ch/tags/spring-framework/">spring-framework</a></li></ul><div class="post__share"></div></footer></article><nav class="post__nav"><div class="post__nav-inner"><div class="post__nav-prev"><svg width="1.041em" height="0.416em" aria-hidden="true"><use xlink:href="https://www.finecloud.ch/assets/svg/svg-map.svg#arrow-prev"/></svg> <a href="https://www.finecloud.ch/data-transfer-objects.html" class="post__nav-link" rel="prev"><span>Previous</span> Data Transfer Objects</a></div><div class="post__nav-next"><a href="https://www.finecloud.ch/building-spring-boot-docker-images.html" class="post__nav-link" rel="next"><span>Next</span> Building Spring Boot Docker Images </a><svg width="1.041em" height="0.416em" aria-hidden="true"><use xlink:href="https://www.finecloud.ch/assets/svg/svg-map.svg#arrow-next"/></svg></div></div></nav><div class="post__related related"><div class="wrapper"><h2 class="h5 related__title">You should also read:</h2><article class="related__item"><div class="feed__meta"><time datetime="2023-04-10T08:46" class="feed__date">April 10, 2023</time></div><h3 class="h1"><a href="https://www.finecloud.ch/data-transfer-objects.html">Data Transfer Objects</a></h3></article><article class="related__item"><div class="feed__meta"><time datetime="2022-09-13T11:41" class="feed__date">September 13, 2022</time></div><h3 class="h1"><a href="https://www.finecloud.ch/java-and-databases-use-sql-joins.html">Java and databases use SQL joins</a></h3></article><article class="related__item"><div class="feed__meta"><time datetime="2022-09-06T14:20" class="feed__date">September 6, 2022</time></div><h3 class="h1"><a href="https://www.finecloud.ch/java-working-with-a-database.html">Java working with databases</a></h3></article></div></div></main><footer class="footer"><div class="footer__copyright"><p>Powered by Publii</p></div><button onclick="backToTopFunction()" id="backToTop" class="footer__bttop" aria-label="Back to top" title="Back to top"><svg><use xlink:href="https://www.finecloud.ch/assets/svg/svg-map.svg#toparrow"/></svg></button></footer></div><script>window.publiiThemeMenuConfig = {
mobileMenuMode: 'sidebar',
animationSpeed: 300,
submenuWidth: 'auto',
doubleClickTime: 500,
mobileMenuExpandableSubmenus: true,
relatedContainerForOverlayMenuSelector: '.top',
};</script><script defer="defer" src="https://www.finecloud.ch/assets/js/scripts.min.js?v=6ca8b60e6534a3888de1205e82df8528"></script><script>var images = document.querySelectorAll('img[loading]');
for (var i = 0; i < images.length; i++) {
if (images[i].complete) {
images[i].classList.add('is-loaded');
} else {
images[i].addEventListener('load', function () {
this.classList.add('is-loaded');
}, false);
}
}</script><script defer="defer" src="https://www.finecloud.ch/media/plugins/syntaxHighlighter/prism.js"></script></body></html>