Skip to content

Commit

Permalink
Schema based server config validator (ExpediaGroup#137)
Browse files Browse the repository at this point in the history
Add a simple Java DSL for constructing a Schema for JSON (and YAML) objects. 
The schema can validate JSON documents like styx configuration. The implemented 
schema supports most JSON constructs apart from nested (or multidimensional) arrays. 
The validation is disabled by default until it has been tested against some more live 
configurations from existing styx deployments.
  • Loading branch information
mikkokar committed Apr 24, 2018
1 parent b064895 commit b05f3b3
Show file tree
Hide file tree
Showing 19 changed files with 2,449 additions and 26 deletions.
10 changes: 6 additions & 4 deletions components/common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
<groupId>com.hotels.styx</groupId>
<artifactId>styx-testsupport</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -53,10 +58,7 @@
</resources>
<testResources>
<testResource>
<directory>src/test/unit/resources</directory>
</testResource>
<testResource>
<directory>src/test/integration/resources</directory>
<directory>src/test/resources</directory>
</testResource>
</testResources>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright (C) 2013-2018 Expedia Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.config.schema;

import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;

import java.util.Set;

import static java.lang.String.format;
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.joining;

/**
* A constraint to ensure at least one of many specified fields is mandatory.
*/
class AtLeastOneFieldPresenceConstraint implements Constraint {
private final Set<String> fieldNames;
private final String description;

AtLeastOneFieldPresenceConstraint(String... fieldNames) {
this.fieldNames = ImmutableSet.copyOf(fieldNames);
this.description = description(fieldNames);
}

private static String description(String[] fieldNames) {
String displayNames = stream(fieldNames)
.map(name -> format("'%s'", name))
.collect(joining(", "));
return format("At least one of (%s) must be present.", displayNames);
}

@Override
public boolean evaluate(Schema schema, JsonNode node) {
long fieldsPresent = ImmutableList.copyOf(node.fieldNames())
.stream()
.filter(fieldNames::contains)
.count();

return fieldsPresent >= 1;
}

@Override
public String describe() {
return description;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright (C) 2013-2018 Expedia Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.config.schema;

import com.fasterxml.jackson.databind.JsonNode;

/**
* An additional requirement, or constraint, for the JSON document.
*
* A constraint could describe a particular relationship between object's
* fields.
*/
public interface Constraint extends SchemaDirective {
/**
* Evaluates a constraint associated with `schema` against the JSON `node`
*
* A constraint specifies a rule which the parsed Json/Yaml object must
* satisfy in order to be considered conformant. An example rule might be
* "At least one of (a, b, c) fields must be present."
*
* @param schema Json object schema
* @param node Parsed JSON object
* @return true if `node` conforms to the constraint, false otherwise
*/
boolean evaluate(Schema schema, JsonNode node);

/**
* An informational description of the constraint. This can be used
* for example in the error messages to provide more information about
* validation failures, or perhaps to generate end-user documentation.
*
* @return An contraint description.
*/
String describe();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright (C) 2013-2018 Expedia Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.config.schema;

/**
* Thrown during schema object construction to indicate an inconsistent schema.
*
* May be thrown when the schema object is being built to indicate an internal inconsistency
* within a schema object. Such inconsistency may be a named reference to a non-existing schema,
* duplicate field names, and so on.
*/
public class InvalidSchemaException extends RuntimeException {
public InvalidSchemaException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Copyright (C) 2013-2018 Expedia Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.config.schema;

/**
* A schema directive to mark the object as opaque, making its internal layout unknown.
*/
final class OpaqueSchema implements SchemaDirective {
}
Loading

0 comments on commit b05f3b3

Please sign in to comment.