Skip to content

Commit

Permalink
added ValidationResult to remove external dependency; updated license…
Browse files Browse the repository at this point in the history
… text; added examples
  • Loading branch information
nickwhalen committed Feb 23, 2015
1 parent 7e898b7 commit da1454b
Show file tree
Hide file tree
Showing 22 changed files with 603 additions and 478 deletions.
8 changes: 4 additions & 4 deletions openrtb-validator/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2013, Nexage, Inc.
Copyright (c) 2015, Millennial Media, Inc.
All rights reserved.
Provided under BSD License as follows:

Expand All @@ -10,9 +10,9 @@ modification, are permitted provided that the following conditions are met:
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of Nexage, Inc. nor the names of its contributors may
be used to endorse or promote products derived from this software
without specific prior written permission.
3. Neither the name of Millennial Media, Inc. nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Expand Down
28 changes: 10 additions & 18 deletions openrtb-validator/README → openrtb-validator/README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,30 @@
openrtb-validator
=================
# openrtb-validator

This project provides a simple API that can be used to validate JSON bid requests and responses according to OpenRTB specifications. OpenRTB versions 1.0, 2.0, 2.1, 2.2, and 2.3 are fully supported.

Provided under the New BSD License. Refer to the file LICENSE file in the root of this project for more information.

Usage
-----
## Usage

To add validator dependencies to a Maven pom.xml:
Add the OpenRTB-validator dependency to your Maven pom.xml:

<dependency>
<groupId>org.openrtb</groupId>
<artifactId>openrtb-validator</artifactId>
<version>2.2.0</version>
<version>2.3.1</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.github.fge</groupId>
<artifactId>json-schema-validator</artifactId>
<version>2.1.3</version>
</dependency>

To ascertain whether a given JSON String, File, Reader, or Resource is a valid bid request according to OpenRTB v2.2 specifications:
To ascertain whether a given JSON String, File, Reader, or Resource is a valid bid request according to OpenRTB v2.3 specifications:

OpenRtbValidator validator = OpenRtbValidatorFactory.getValidator(OpenRtbInputType.BID_REQUEST, OpenRtbVersion.V2_2);
OpenRtbValidator validator = OpenRtbValidatorFactory.getValidator(OpenRtbInputType.BID_REQUEST, OpenRtbVersion.V2_3);
boolean valid = validator.isValid(json);

To get a detailed validation report including reasons why the JSON is invalid:

ProcessingReport report = validator.validate(json);
System.out.println("validation report: " + report);
ValidationResult validationResult = validator.validate(json);
System.out.println("valid: " + validationResult.isValid() + ", result: " + validationResult.getResult());

Specification Documents
-----------------------
## Specification Documents

The specification documents used to create these OpenRTB validation schemas can be found under src/main/resources/specification.
The specification documents used to create these OpenRTB validation schemas can be found under src/main/resources/specification and at http://www.iab.net/guidelines/rtbproject.
161 changes: 92 additions & 69 deletions openrtb-validator/pom.xml
Original file line number Diff line number Diff line change
@@ -1,77 +1,100 @@
<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>
<modelVersion>4.0.0</modelVersion>

<groupId>org.openrtb</groupId>
<artifactId>openrtb-validator</artifactId>
<version>2.3.0</version>
<packaging>jar</packaging>
<groupId>org.openrtb</groupId>
<artifactId>openrtb-validator</artifactId>
<version>2.3.1</version>
<packaging>jar</packaging>

<name>openrtb-validator</name>
<url>http://maven.apache.org</url>
<name>openrtb-validator</name>
<url>http://maven.apache.org</url>
<description>Provides a simple API that can be used to validate JSON bid requests and responses according to OpenRTB specifications. OpenRTB versions 1.0, 2.0, 2.1, 2.2, and 2.3 are fully supported.</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<component.name>openrtb-validator</component.name>
</properties>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<component.name>openrtb-validator</component.name>
</properties>

<licenses>
<license>
<name>New BSD License</name>
<url>http://opensource.org/licenses/BSD-3-Clause</url>
<distribution>repo</distribution>
</license>
</licenses>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.fge</groupId>
<artifactId>json-schema-validator</artifactId>
<version>2.1.3</version>
</dependency>
</dependencies>
<licenses>
<license>
<name>New BSD License</name>
<url>http://opensource.org/licenses/BSD-3-Clause</url>
<distribution>repo</distribution>
</license>
</licenses>

<developers>
<developer>
<name>Nick Whalen</name>
<email>[email protected]</email>
<organization>OpenRTB</organization>
<organizationUrl>https://github.com/openrtb/openrtb2x</organizationUrl>
</developer>
</developers>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.fge</groupId>
<artifactId>json-schema-validator</artifactId>
<version>2.2.6</version>
</dependency>
</dependencies>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>specifications/</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9</version>
</plugin>
</plugins>
</build>

<distributionManagement>
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
</repository>
</distributionManagement>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>specifications/</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9</version>
</plugin>
</plugins>
</build>
<scm>
<url>https://github.com/openrtb/openrtb2x/tree/2.0/openrtb-validator</url>
<connection>scm:git:https://github.com/openrtb/openrtb2x.git</connection>
<developerConnection>scm:git:[email protected]:openrtb/openrtb2x.git</developerConnection>
</scm>

</project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* ============================================================================
* Copyright (c) 2013, Nexage, Inc.
* Copyright (c) 2015, Millennial Media, Inc.
* All rights reserved.
* Provided under BSD License as follows:
*
Expand All @@ -12,9 +12,9 @@
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Nexage, Inc. nor the names of its contributors may
* be used to endorse or promote products derived from this software
* without specific prior written permission.
* 3. Neither the name of Millennial Media, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Expand All @@ -38,10 +38,10 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jsonschema.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import com.github.fge.jsonschema.report.ProcessingReport;

/**
* This class provides basic JSON validation against a schema.
Expand All @@ -50,68 +50,92 @@ public class GenericOpenRtbValidator implements OpenRtbValidator {

private final JsonSchema schema;

/**
* Constructs a JSON validator using the given schema read as a resource.
*
* @param schemaResource the schema resource
*/
public GenericOpenRtbValidator(String schemaResource) {
/**
* Constructs a JSON validator using the given schema read as a resource.
*
* @param schemaResource
* the schema resource
*/
GenericOpenRtbValidator(String schemaResource) {
try {
JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
JsonNode jsonNode = JsonLoader.fromResource(schemaResource);
schema = factory.getJsonSchema(jsonNode);
} catch (IOException e) {
throw new IllegalStateException("could not initialize validator due to previous exception", e);
} catch (ProcessingException e) {
} catch (IOException | ProcessingException e) {
throw new IllegalStateException("could not initialize validator due to previous exception", e);
}
}

@Override
public final boolean isValid(String json) throws IOException, ProcessingException {
ProcessingReport report = validate(json);
return report != null ? report.isSuccess() : false;
public final boolean isValid(String json) {
try {
return validate(json).isValid();
} catch (IOException e) {
return false;
}
}

@Override
public final boolean isValid(JsonNode jsonNode) throws ProcessingException {
ProcessingReport report = validate(jsonNode);
return report != null ? report.isSuccess() : false;
public final boolean isValid(JsonNode jsonNode) {
try {
return validate(jsonNode).isValid();
} catch (IOException e) {
return false;
}
}

@Override
public final boolean isValid(File file) throws IOException, ProcessingException {
ProcessingReport report = validate(file);
return report != null ? report.isSuccess() : false;
public final boolean isValid(File file) {
try {
return validate(file).isValid();
} catch (IOException e) {
return false;
}
}

@Override
public final boolean isValid(Reader reader) throws IOException, ProcessingException {
ProcessingReport report = validate(reader);
return report != null ? report.isSuccess() : false;
public final boolean isValid(Reader reader) {
try {
return validate(reader).isValid();
} catch (IOException e) {
return false;
}
}

@Override
public final ProcessingReport validate(String json) throws IOException, ProcessingException {
public final ValidationResult validate(String json) throws IOException {
JsonNode jsonNode = JsonLoader.fromString(json);
return schema.validate(jsonNode);
return getValidationResult(jsonNode);
}

@Override
public final ProcessingReport validate(JsonNode jsonNode) throws ProcessingException {
return schema.validate(jsonNode);
public final ValidationResult validate(JsonNode jsonNode) throws IOException {
return getValidationResult(jsonNode);
}

@Override
public final ProcessingReport validate(File file) throws IOException, ProcessingException {
public final ValidationResult validate(File file) throws IOException {
JsonNode jsonNode = JsonLoader.fromFile(file);
return schema.validate(jsonNode);
return getValidationResult(jsonNode);
}

@Override
public final ProcessingReport validate(Reader reader) throws IOException, ProcessingException {
public final ValidationResult validate(Reader reader) throws IOException {
JsonNode jsonNode = JsonLoader.fromReader(reader);
return schema.validate(jsonNode);
return getValidationResult(jsonNode);
}

private final ValidationResult getValidationResult(JsonNode jsonNode) throws IOException {
try {
ProcessingReport processingReport = schema.validate(jsonNode);
if (processingReport != null) {
return new ValidationResult(processingReport.isSuccess(), processingReport.toString());
} else {
return new ValidationResult(false, null);
}
} catch (ProcessingException e) {
throw new IOException(e.getMessage());
}
}

}
Loading

0 comments on commit da1454b

Please sign in to comment.