Validation functions for JSON Schema validation
This library performs the validations required for some of the JSON Schema format
validations as defined in
JSON Schema Validation
Section 7.3.
The functions are provided in the form of a standalone library to simplify their use in code generated from JSON Schema specification files.
Most but not all of the functions defined in the specification are included in the initial version; the remaining few may be added in due course.
The validations are static functions, and they all take a single parameter, the string value to be tested.
They return true
if the value matches the requirements of the specification (they all return false
if the string is
null
).
boolean valid = JSONValidation.isDateTime(str);
boolean valid = JSONValidation.isDate(str);
boolean valid = JSONValidation.isTime(str);
boolean valid = JSONValidation.isDuration(str);
boolean valid = JSONValidation.isEmail(str);
boolean valid = JSONValidation.isHostname(str);
boolean valid = JSONValidation.isURI(str);
boolean valid = JSONValidation.isURIReference(str);
boolean valid = JSONValidation.isUUID(str);
boolean valid = JSONValidation.isIPV4(str);
boolean valid = JSONValidation.isIPV6(str);
boolean valid = JSONValidation.isJSONPointer(str);
boolean valid = JSONValidation.isRelativeJSONPointer(str);
boolean valid = JSONValidation.isRegex(str);
In order to perform date validations, the library contains additional static functions related to dates. They are made public because there is little reason not to do so, and they may be useful in other contexts.
This function returns true
if the given year is a leap year.
It takes a single integer parameter, the year to be checked.
The function uses the rules of the Gregorian calendar, and while there is no limit on the input year value, the result
will be meaningful only in cases where the year is subject to that calendar.
boolean leap = JSONValidation.isLeapYear(year);
This function returns the length of a specified month.
It takes two integer parameters, the year and the month (in the range 1 to 12).
As with isLeapYear
, the function uses the rules of the Gregorian calendar, and the same caveat applies.
int length = JSONValidation.isLeapYear(year, month);
Importantly, the library is entirely self-contained, and the inclusion of this library in a project will not bring in a long list of transitive dependencies.
The latest version of the library is 1.5, and it may be obtained from the Maven Central repository.
<dependency>
<groupId>net.pwall.json</groupId>
<artifactId>json-validation</artifactId>
<version>1.5</version>
</dependency>
implementation 'net.pwall.json:json-validation:1.5'
implementation("net.pwall.json:json-validation:1.5")
Peter Wall
2022-11-06