public class JSONValidation extends Object
The original requirements are taken from the JSON Schema Validation specification.
There are also additional validations for two unofficial types local-date-time and local-time;
these allow for validation of date-time or time strings that do not include time zone information.
Also included are isLeapYear(int) and monthLength(int, int) functions; these are required for
date checking and it costs nothing to make them available for general use.
| Modifier and Type | Field and Description |
|---|---|
static Map<String,Predicate<String>> |
validations
Table of implemented validations.
|
| Constructor and Description |
|---|
JSONValidation() |
| Modifier and Type | Method and Description |
|---|---|
static boolean |
isDate(CharSequence string)
Test for conformity to the
date format type. |
static boolean |
isDateTime(CharSequence string)
Test for conformity to the
date-time format type. |
static boolean |
isDuration(CharSequence string)
Test for conformity to the
duration format type. |
static boolean |
isEmail(CharSequence string)
Test for conformity to the
email format type. |
static boolean |
isHostname(CharSequence string)
Test for conformity to the
hostname format type. |
static boolean |
isIPV4(CharSequence string)
Test for conformity to the
ipv4 format type. |
static boolean |
isIPV6(CharSequence string)
Test for conformity to the
ipv6 format type. |
static boolean |
isJSONPointer(CharSequence string)
Test for conformity to the
json-pointer format type. |
static boolean |
isLeapYear(int year)
Determine whether a given year is a leap year (only meaningful for years covered by the Gregorian calendar).
|
static boolean |
isLocalDateTime(CharSequence string)
Test for conformity to an unofficial
local-date-time type (not part of the JSON Schema Validation
Specification). |
static boolean |
isLocalTime(CharSequence string)
Test for conformity to an unofficial
local-time type (not part of the JSON Schema Validation
Specification). |
static boolean |
isRegex(CharSequence string)
Test for conformity to the
regex format type. |
static boolean |
isRelativeJSONPointer(CharSequence string)
Test for conformity to the
relative-json-pointer format type. |
static boolean |
isTime(CharSequence string)
Test for conformity to the
time format type. |
static boolean |
isURI(CharSequence string)
Test for conformity to the
uri format type. |
static boolean |
isURIReference(CharSequence string)
Test for conformity to the
uri-reference format type. |
static boolean |
isURITemplate(CharSequence string)
Test for conformity to the
uri-template format type. |
static boolean |
isUUID(CharSequence string)
Test for conformity to the
uuid format type. |
static int |
monthLength(int year,
int month)
Calculate the length of a given month (only meaningful for years covered by the Gregorian calendar).
|
public static boolean isLeapYear(int year)
year - the yeartrue if the year is a leap yearpublic static int monthLength(int year,
int month)
year - the yearmonth - the month (must be in the range 1 to 12)IllegalArgumentException - if the month is not in the range 1 to 12public static boolean isDateTime(CharSequence string)
date-time format type. A string is valid if it conforms to the
date-time production in
RFC 3339, section 5.6.string - the string to be testedtrue if the string is correctpublic static boolean isLocalDateTime(CharSequence string)
local-date-time type (not part of the JSON Schema Validation
Specification). A string is valid if it conforms to a new production [full-date "T" partial-time] based
on RFC 3339, section 5.6.string - the string to be testedtrue if the string is correctpublic static boolean isDate(CharSequence string)
date format type. A string is valid if it conforms to the full-date
production in RFC 3339, section 5.6.string - the string to be testedtrue if the string is correctpublic static boolean isTime(CharSequence string)
time format type. A string is valid if it conforms to the full-time
production in RFC 3339, section 5.6.string - the string to be testedtrue if the string is correctpublic static boolean isLocalTime(CharSequence string)
local-time type (not part of the JSON Schema Validation
Specification). A string is valid if it conforms to the partial-time production in
RFC 3339, section 5.6.string - the string to be testedtrue if the string is correctpublic static boolean isDuration(CharSequence string)
duration format type. A string is valid if it conforms to the
duration production in RFC 3339, Appendix A.string - the string to be testedtrue if the string is correctpublic static boolean isURI(CharSequence string)
uri format type. A string is valid if it conforms to
RFC 3986.string - the string to be testedtrue if the string is correctpublic static boolean isURIReference(CharSequence string)
uri-reference format type. A string is valid if it conforms to
RFC 3986 (either a URI or a relative-reference).string - the string to be testedtrue if the string is correctpublic static boolean isURITemplate(CharSequence string)
uri-template format type. A string is valid if it conforms to
RFC 6570.string - the string to be testedtrue if the string is correctpublic static boolean isUUID(CharSequence string)
uuid format type. A string is valid if it conforms to
RFC 4122.string - the string to be testedtrue if the string is correctpublic static boolean isHostname(CharSequence string)
hostname format type. A string is valid if it conforms to
RFC 1123, section 2.1.string - the string to be testedtrue if the string is correctpublic static boolean isEmail(CharSequence string)
email format type.
Validation of email addresses is difficult, largely because the specification in
RFC 5322 makes reference to earlier “obsolete”
forms of email addresses that are expected to be accepted as valid. This function does not attempt to cover the
entire range of obsolete addresses; instead, it implements a form of validation derived from the regular
expression at the web site emailregex.com for the
“local-part” (the addressee or mailbox name), and it uses the hostname validation from
RFC 1123 for the “domain”.
string - the string to be testedtrue if the string is correctpublic static boolean isIPV4(CharSequence string)
ipv4 format type. A string is valid if it conforms to
RFC 2673, section 3.2.string - the string to be testedtrue if the string is correctpublic static boolean isIPV6(CharSequence string)
ipv6 format type. A string is valid if it conforms to
RFC 4291, section 2.2.
NOTE: The
JSON Schema Validation
specification says (§ 7.3.4) that a string conforming to the ipv6 format must be an “IPv6
address as defined in RFC 4291, section 2.2”. Subsequent
to RFC 4291, RFC 5952 recommended tighter restrictions on the
representation of IPV6 addresses, including mandating the use of lower case for all alpha characters, and
prohibiting the use of “::” to compress a single zero 16-bit field. Because the JSON Schema
Validation specification refers only to RFC 4291, not RFC 5952, this function does not implement the tighter
restrictions of the later document.
string - the string to be testedtrue if the string is correctpublic static boolean isJSONPointer(CharSequence string)
json-pointer format type. A string is valid if it conforms to
RFC 6901, section 5.string - the string to be testedtrue if the string is correctpublic static boolean isRelativeJSONPointer(CharSequence string)
relative-json-pointer format type. A string is valid if it conforms to
Relative JSON Pointers.string - the string to be testedtrue if the string is correctpublic static boolean isRegex(CharSequence string)
regex format type. A string is valid if it conforms to the
ECMA 262 regular expression dialect.
Since the Java Pattern class used here implements a dialect very close to, but not identical to the
ECMA 262 variant, it is possible that in rare cases there may be subtle inconsistencies in the results of this
function.
string - the string to be testedtrue if the string is correctCopyright © 2025. All rights reserved.