forked from ExpediaGroup/styx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Schema based server config validator (ExpediaGroup#137)
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
Showing
19 changed files
with
2,449 additions
and
26 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
61 changes: 61 additions & 0 deletions
61
...common/src/main/java/com/hotels/styx/config/schema/AtLeastOneFieldPresenceConstraint.java
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,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; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
components/common/src/main/java/com/hotels/styx/config/schema/Constraint.java
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,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(); | ||
} |
29 changes: 29 additions & 0 deletions
29
components/common/src/main/java/com/hotels/styx/config/schema/InvalidSchemaException.java
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,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); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
components/common/src/main/java/com/hotels/styx/config/schema/OpaqueSchema.java
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,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 { | ||
} |
Oops, something went wrong.