-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement standalone Validator, re #44
- Loading branch information
Showing
14 changed files
with
457 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2014 JSONx | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# JSONx Binding Validator | ||
|
||
> **JSON Schema for the enterprise** | ||
[![Build Status](https://github.com/jsonx-org/java/actions/workflows/build.yml/badge.svg)](https://github.com/jsonx-org/java/actions/workflows/build.yml) | ||
[![Coverage Status](https://coveralls.io/repos/github/jsonx-org/java/badge.svg)](https://coveralls.io/github/jsonx-org/java) | ||
[![Javadocs](https://www.javadoc.io/badge/org.jsonx/validator.svg)](https://www.javadoc.io/doc/org.jsonx/validator) | ||
[![Released Version](https://img.shields.io/maven-central/v/org.jsonx/validator.svg)](https://mvnrepository.com/artifact/org.jsonx/validator) | ||
![Snapshot Version](https://img.shields.io/nexus/s/org.jsonx/validator?label=maven-snapshot&server=https%3A%2F%2Foss.sonatype.org) | ||
|
||
## Abstract | ||
|
||
This document specifies the <ins>JSONx Binding Validator</ins>, which offers facilities for validating JSON documents agains a [JSD schema][schema]. | ||
|
||
## Table of Contents | ||
|
||
<samp> </samp>1 [<ins>Introduction</ins>](#1-introduction)<br> | ||
<samp> </samp>1.1 [Conventions Used in This Document](#11-conventions-used-in-this-document)<br> | ||
<samp> </samp>2 [<ins>Purpose</ins>](#2-purpose)<br> | ||
<samp> </samp>3 [<ins>Requirements</ins>](#3-requirements)<br> | ||
<samp> </samp>4 [<ins>Getting Started</ins>](#4-getting-started)<br> | ||
<samp> </samp>4.1 [Valid Case](#41-valid-case)<br> | ||
<samp> </samp>4.2 [Invalid Case](#42-invalid-case)<br> | ||
<samp> </samp>5 [<ins>Specification</ins>](#5-specification)<br> | ||
<samp> </samp>6 [<ins>Contributing</ins>](#6-contributing)<br> | ||
<samp> </samp>7 [<ins>Special Thanks</ins>](#7-special-thanks)<br> | ||
<samp> </samp>8 [<ins>License</ins>](#8-license) | ||
|
||
## <b>1</b> <ins>Introduction</ins> | ||
|
||
This document presents the functionality of the <ins>JSONx Validator</ins>. It also contains a directory of links to these related resources. | ||
|
||
The <ins>JSONx Validator</ins> consumes a JSD schema and a JSON document, and returns successfully if the specified documents are valid against the provided schema, or fails with an exception specifying the validation error if the specified document is invalid against the provided schema. The <ins>JSONx Validator</ins> utilizes the <ins>JSONx Binding Generator</ins> to perform its validation. | ||
|
||
### <b>1.1</b> Conventions Used in This Document | ||
|
||
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in [RFC2119](https://www.ietf.org/rfc/rfc2119.txt). | ||
|
||
## <b>2</b> <ins>Purpose</ins> | ||
|
||
Provide a <ins>validator</ins> utility for automatic validation of JSON documents against a <ins>schema document</ins>. | ||
|
||
## <b>3</b> <ins>Requirements</ins> | ||
|
||
1. The <ins>validator</ins> MUST be able to consume a <ins>JSON document</ins>, and validate its contents agains a provided <ins>schema document</ins>. | ||
|
||
1. The <ins>validator</ins> MUST return successfully if a <ins>JSON document</ins> conforms to the provided <ins>schema document</ins>. | ||
|
||
1. The <ins>validator</ins> MUST throw an exception specifying the validation error if a <ins>JSON document</ins> does not conform to the provided <ins>schema document</ins>. | ||
|
||
## <b>4</b> <ins>Getting Started</ins> | ||
|
||
The <ins>JSONx Validator</ins> provides convenience utilities for validating a <ins>JSON document</ins> agains a <ins>schema document</ins>. The following illustrates example usage of the `Validator`. | ||
|
||
### <b>4.1</b> Valid Case | ||
|
||
The following example uses the `Validator` to validate a <i>valid</i> <ins>JSON document</ins> against a <ins>schema document</ins>. | ||
|
||
```bash | ||
$ java -cp ... org.jsonx.Validator src/main/resources/example.jsd src/main/resources/valid.json | ||
$ echo $? | ||
0 | ||
``` | ||
|
||
### <b>4.2</b> Invalid Case | ||
|
||
The following example uses the `Validator` to validate an <i>invalid</i> <ins>JSON document</ins> against a <ins>schema document</ins>. | ||
|
||
```bash | ||
$ java -cp ... org.jsonx.Validator src/main/resources/example.jsd src/main/resources/invalid.json | ||
Invalid content was found in empty array: @org.jsonx.StringElement(id=0, pattern="[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"): Content is not complete | ||
$ echo $? | ||
1 | ||
``` | ||
|
||
## <b>5</b> <ins>Specification</ins> | ||
|
||
The `Validator` is a utility class that can be used on the CLI to validate a <ins>JSON document</ins> against a <ins>schema document</ins>. The `Validator` class has the following usage specification: | ||
|
||
``` | ||
Usage: Usage: Validator <SCHEMA> <JSON...> | ||
Supported SCHEMA formats: | ||
<JSD|JSDx> | ||
``` | ||
|
||
## <b>6</b> <ins>Contributing</ins> | ||
|
||
Pull requests are welcome. For major changes, please [open an issue](../../../issues) first to discuss what you would like to change. | ||
|
||
Please make sure to update tests as appropriate. | ||
|
||
## <b>7</b> <ins>Special Thanks</ins> | ||
|
||
[![Java Profiler](https://www.ej-technologies.com/images/product_banners/jprofiler_small.png)](https://www.ej-technologies.com/products/jprofiler/overview.html) | ||
<br><sub>_Special thanks to [EJ Technologies](https://www.ej-technologies.com/) for providing their award winning Java Profiler ([JProfiler](https://www.ej-technologies.com/products/jprofiler/overview.html)) for development of the JSONx Framework._</sub> | ||
|
||
## <b>8</b> <ins>License</ins> | ||
|
||
This project is licensed under the MIT License - see the [LICENSE.txt](LICENSE.txt) file for details. | ||
|
||
[schema]: ../../../../schema |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<!-- | ||
Copyright (c) 2017 JSONx | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
You should have received a copy of The MIT License (MIT) along with this | ||
program. If not, see <http://opensource.org/licenses/MIT/>. | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.jsonx</groupId> | ||
<artifactId>jsonx</artifactId> | ||
<version>0.4.0-SNAPSHOT</version> | ||
</parent> | ||
<properties> | ||
<powermock.version>2.0.2</powermock.version> | ||
</properties> | ||
<artifactId>validator</artifactId> | ||
<name>JSONx Validator</name> | ||
<description> | ||
The JSD Binding Validator validates JSON documents against JSD or JSDx schemas. | ||
</description> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<configuration> | ||
<reuseForks>false</reuseForks> | ||
<forkCount>1</forkCount> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-shade-plugin</artifactId> | ||
<version>3.2.4</version> | ||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>shade</goal> | ||
</goals> | ||
<configuration> | ||
<shadedArtifactAttached>true</shadedArtifactAttached> | ||
<shadedClassifierName>bundle</shadedClassifierName> | ||
<transformers> | ||
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> | ||
<manifestEntries> | ||
<Implementation-Title>${project.artifactId}</Implementation-Title> | ||
<Implementation-Version>${project.version}</Implementation-Version> | ||
<Implementation-Vendor-Id>${project.groupId}</Implementation-Vendor-Id> | ||
</manifestEntries> | ||
</transformer> | ||
</transformers> | ||
<finalName>${project.artifactId}-${project.version}</finalName> | ||
<filters> | ||
<filter> | ||
<artifact>*:*</artifact> | ||
<excludes> | ||
<exclude>META-INF/*.SF</exclude> | ||
<exclude>META-INF/*.DSA</exclude> | ||
<exclude>META-INF/*.RSA</exclude> | ||
</excludes> | ||
</filter> | ||
</filters> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.jsonx</groupId> | ||
<artifactId>generator</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jsonx</groupId> | ||
<artifactId>generator</artifactId> | ||
<version>${project.version}</version> | ||
<type>test-jar</type> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* Copyright (c) 2018 JSONx | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* You should have received a copy of The MIT License (MIT) along with this | ||
* program. If not, see <http://opensource.org/licenses/MIT/>. | ||
*/ | ||
|
||
package org.jsonx; | ||
|
||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Modifier; | ||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import org.libj.jci.CompilationException; | ||
import org.libj.jci.InMemoryCompiler; | ||
import org.libj.lang.Numbers; | ||
import org.libj.lang.PackageLoader; | ||
import org.libj.lang.PackageNotFoundException; | ||
import org.libj.net.URLs; | ||
import org.openjax.json.JsonReader; | ||
|
||
/** | ||
* Utility for validating JSON documents to a JSD or JSDx schema. | ||
*/ | ||
public class Validator { | ||
private static void trapPrintUsage() { | ||
System.err.println("Usage: Validator <SCHEMA> <JSON...>"); | ||
System.err.println("Supported SCHEMA formats:"); | ||
System.err.println(" <JSD|JSDx>"); | ||
System.exit(1); | ||
} | ||
|
||
private static void validate(final JsonReader reader, final Set<Class<? extends JxObject>> objectClasses, final Set<Class<? extends Annotation>> arrayClasses, final ArrayList<String> errors) throws IOException { | ||
final long i = reader.readToken(); | ||
final int off = Numbers.Composite.decodeInt(i, 0); | ||
final char token = reader.bufToChar(off); | ||
try { | ||
if (token == '{') { | ||
if (objectClasses.size() > 0) | ||
JxDecoder.VALIDATING.parseObject(reader, objectClasses); | ||
else | ||
errors.add("No object definition present in schema"); | ||
} | ||
else if (token == '[') { | ||
if (arrayClasses.size() > 0) | ||
JxDecoder.VALIDATING.parseArray(reader, arrayClasses); | ||
else | ||
errors.add("No array definition present in schema"); | ||
} | ||
else { | ||
throw new IllegalArgumentException("Unable to parse JSON document"); | ||
} | ||
} | ||
catch (final DecodeException e) { | ||
errors.add(e.getMessage()); | ||
} | ||
} | ||
|
||
public static void main(final String[] args) throws CompilationException, IOException, PackageNotFoundException { | ||
if (args.length < 2) | ||
trapPrintUsage(); | ||
|
||
if (validate(args) == 1) | ||
System.exit(1); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
static int validate(final String[] args) throws CompilationException, IOException, PackageNotFoundException { | ||
final String prefix = "jsonx"; | ||
final Map<String,String> source = SchemaElement.parse(URLs.fromStringPath(args[0]), new Settings.Builder().withPrefix(prefix + ".").build()).toSource(); | ||
final InMemoryCompiler compiler = new InMemoryCompiler(); | ||
for (final Map.Entry<String,String> entry : source.entrySet()) | ||
compiler.addSource(entry.getValue()); | ||
|
||
final ClassLoader classLoader = compiler.compile(); | ||
|
||
final HashSet<Class<? extends JxObject>> objectClasses = new HashSet<>(); | ||
final HashSet<Class<? extends Annotation>> arrayClasses = new HashSet<>(); | ||
PackageLoader.getPackageLoader(classLoader).loadPackage(prefix, cls -> { | ||
if (Annotation.class.isAssignableFrom(cls)) | ||
arrayClasses.add((Class<? extends Annotation>)cls); | ||
else if (!Modifier.isAbstract(cls.getModifiers()) && JxObject.class.isAssignableFrom(cls)) | ||
objectClasses.add((Class<? extends JxObject>)cls); | ||
|
||
return true; | ||
}); | ||
|
||
final ArrayList<String> errors = new ArrayList<>(); | ||
for (int i = 1, i$ = args.length; i < i$; ++i) {// [A] | ||
try (final JsonReader reader = new JsonReader(new FileReader(args[i]))) { | ||
validate(reader, objectClasses, arrayClasses, errors); | ||
} | ||
} | ||
|
||
for (int i = 0, i$ = errors.size(); i < i$; ++i) // [RA] | ||
System.err.println(errors.get(i)); | ||
|
||
return errors.size() == 0 ? 0 : 1; | ||
} | ||
} |
Oops, something went wrong.