Skip to content

pwall567/json-validation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json-validation

Build Status License: MIT Maven Central

Validation functions for JSON Schema validation

Background

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.

Quick Start

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).

date-time validation

        boolean valid = JSONValidation.isDateTime(str);

Test for conformity to the date-time format type. A string is valid if it conforms to the date-time production in RFC 3339, section 5.6.

local-date-time validation

        boolean valid = JSONValidation.isLocalDateTime(str);

Test for conformity to an unofficial 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.

date validation

        boolean valid = JSONValidation.isDate(str);

Test for conformity to the date format type. A string is valid if it conforms to the full-date production in RFC 3339, section 5.6.

time validation

        boolean valid = JSONValidation.isTime(str);

Test for conformity to the time format type. A string is valid if it conforms to the full-time production in RFC 3339, section 5.6.

local-time validation

        boolean valid = JSONValidation.isLocalTime(str);

Test for conformity to an unofficial 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.

duration validation

        boolean valid = JSONValidation.isDuration(str);

Test for conformity to the duration format type. A string is valid if it conforms to the duration production in RFC 3339, Appendix A.

uri validation

        boolean valid = JSONValidation.isURI(str);

Test for conformity to the uri format type. A string is valid if it conforms to RFC 3986.

uri-reference validation

        boolean valid = JSONValidation.isURIReference(str);

Test for conformity to the uri-reference format type. A string is valid if it conforms to RFC 3986 (either a URI or a relative-reference).

uri-template validation

        boolean valid = JSONValidation.isURI(str);

Test for conformity to the uri-template format type. A string is valid if it conforms to RFC 6570.

uuid validation

        boolean valid = JSONValidation.isUUID(str);

Test for conformity to the uuid format type. A string is valid if it conforms to RFC 4122.

hostname validation

        boolean valid = JSONValidation.isHostname(str);

Test for conformity to the hostname format type. A string is valid if it conforms to RFC 1123, section 2.1.

email validation

        boolean valid = JSONValidation.isEmail(str);

Test for conformity to the 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”.

ipv4 validation

        boolean valid = JSONValidation.isIPV4(str);

Test for conformity to the ipv4 format type. A string is valid if it conforms to RFC 2673, section 3.2.

ipv6 validation

        boolean valid = JSONValidation.isIPV6(str);

Test for conformity to the 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.

json-pointer validation

        boolean valid = JSONValidation.isJSONPointer(str);

Test for conformity to the json-pointer format type. A string is valid if it conforms to RFC 6901, section 5.

relative-json-pointer validation

        boolean valid = JSONValidation.isRelativeJSONPointer(str);

Test for conformity to the relative-json-pointer format type. A string is valid if it conforms to Relative JSON Pointers.

regex validation

        boolean valid = JSONValidation.isRegex(str);

Test for conformity to the 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.

Additional Functions

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.

isLeapYear

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);

monthLength

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);

No Transitive Dependencies

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.

Dependency Specification

The latest version of the library is 4.0, and it may be obtained from the Maven Central repository.

Maven

    <dependency>
      <groupId>io.jstuff</groupId>
      <artifactId>json-validation</artifactId>
      <version>4.0</version>
    </dependency>

Gradle

    implementation 'io.jstuff:json-validation:4.0'

Gradle (kts)

    implementation("io.jstuff:json-validation:4.0")

Peter Wall

2025-02-03

About

Validation functions for JSON Schema validation

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages