Class JsonSchema
java.lang.Object
com.inductiveautomation.ignition.common.jsonschema.BaseJsonValidator
com.inductiveautomation.ignition.common.jsonschema.JsonSchema
- All Implemented Interfaces:
JsonValidator
This is the core of json constraint implementation. It parses json constraint file and generates
JsonValidators. The class is thread safe, once it is constructed, it can be used to validate
multiple json data concurrently.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Patternstatic final Stringprotected Map<String, JsonValidator> Fields inherited from interface JsonValidator
AT_ROOT -
Constructor Summary
ConstructorsConstructorDescriptionJsonSchema(com.inductiveautomation.ignition.common.gson.JsonElement schemaNode) JsonSchema(com.inductiveautomation.ignition.common.gson.JsonElement schemaNode, JsonSchema subSchema) -
Method Summary
Modifier and TypeMethodDescriptionbooleanacceptsType(JsonType type) Returns true if this schema accepts this typefindAncestorMatchingPath(String matchingPath) findSchemaForPath(JsonPath path) Attempts to find a sub-schema for a specific json path into a hypothetical document that this schema might validate.Optional<com.inductiveautomation.ignition.common.gson.JsonElement> getChildDefaultValue(JsonType type) @Nullable com.inductiveautomation.ignition.common.gson.JsonElementIf the schema specified a default value, it will be returned here.@Nullable com.inductiveautomation.ignition.common.gson.JsonElementgetDefaultValue(boolean includeExamples) If the schema specified a default value, it will be returned here.@Nullable String@Nullable String@NonNull List<com.inductiveautomation.ignition.common.gson.JsonElement> @NonNull Optional<com.inductiveautomation.ignition.common.gson.JsonElement> Returns an optional containing an example value if one exists.@NonNull Optional<com.inductiveautomation.ignition.common.gson.JsonElement> getExtension(String key) Returns the json data attached under this schema's "extension" validator, if any.@Nullable String@Nullable ItemsValidatorFinds the ItemsValidator associated with this schema, if there is one.com.inductiveautomation.ignition.common.gson.JsonElementgetRefSchemaNode(String ref) Find the schema node for $ref attribute.@Nullable StringgetTitle()@Nullable TypeValidatorFinds the TypeValidator associated with this schema, if there is one.Optional<org.apache.commons.lang3.tuple.Pair<String, List<com.inductiveautomation.ignition.common.gson.JsonElement>>> booleanbooleanbooleanReturns true if this schema accepts this type and only this typestatic JsonSchemaparse(InputStream stream) Parses a schema from a UTF-8 encoded stream, such as a classpath resource.@NonNull com.inductiveautomation.ignition.common.gson.JsonElementtoJson()Returns a defensive deep copy of this schema's underlying JSON node — the raw schema document as parsed, before any$refresolution.toString()validate(com.inductiveautomation.ignition.common.gson.JsonElement jsonElement, com.inductiveautomation.ignition.common.gson.JsonElement rootNode, String at) Validate the given JsonElement, the given node is the child node of the root node at given data path.Methods inherited from class BaseJsonValidator
asInt, buildValidationMessage, debug, equals, getParentSchema, getSchemaNode, getSchemaPath, getSubSchema, getValidatorType, greaterThan, hasSubSchema, lessThan, obtainSubSchemaNode, parseErrorCode, useRefCache, validate
-
Field Details
-
validators
-
IGNITION_SCHEMA_URN
- See Also:
-
IGNITION_SCHEMA_PATTERN
-
-
Constructor Details
-
JsonSchema
public JsonSchema(com.inductiveautomation.ignition.common.gson.JsonElement schemaNode) -
JsonSchema
public JsonSchema(com.inductiveautomation.ignition.common.gson.JsonElement schemaNode, JsonSchema subSchema)
-
-
Method Details
-
parse
Parses a schema from a UTF-8 encoded stream, such as a classpath resource.The caller passes ownership of the stream: this method reads it fully and closes it before returning, whether parsing succeeds or fails.
JsonSchema.parse(MyConfig.class.getResourceAsStream("my-config.schema.json"))A missing classpath resource yields a
nullstream, which is rejected outright rather than parsed as an empty schema — so a schema constant declared this way fails at class initialization.- Parameters:
stream- the schema document; consumed and closed by this method.- Returns:
- the parsed schema.
- Throws:
IllegalArgumentException- ifstreamisnull.UncheckedIOException- if the stream cannot be read or closed.
-
findSchemaForPath
Attempts to find a sub-schema for a specific json path into a hypothetical document that this schema might validate. Only supports finding sub-schemas inside "properties" and "items" keywords. -
getRefSchemaNode
Find the schema node for $ref attribute.- Parameters:
ref- String- Returns:
- JsonElement
-
findAncestorMatchingPath
-
findAncestor
-
validate
public Set<ValidationMessage> validate(com.inductiveautomation.ignition.common.gson.JsonElement jsonElement, com.inductiveautomation.ignition.common.gson.JsonElement rootNode, String at) Description copied from interface:JsonValidatorValidate the given JsonElement, the given node is the child node of the root node at given data path.- Parameters:
jsonElement- JsonElementrootNode- JsonElementat- String- Returns:
- A list of ValidationMessage if there is any validation error, or an empty list if there is no error.
-
getTitle
-
getDescription
-
getDefaultValue
public @Nullable com.inductiveautomation.ignition.common.gson.JsonElement getDefaultValue()If the schema specified a default value, it will be returned here. Otherwise, a default value may be derived based on the sub-properties specified. -
getDefaultValue
public @Nullable com.inductiveautomation.ignition.common.gson.JsonElement getDefaultValue(boolean includeExamples) If the schema specified a default value, it will be returned here. Otherwise, a default value may be derived based on the sub-properties specified. -
getChildDefaultValue
-
getExamples
-
isDeprecated
public boolean isDeprecated() -
getExampleValue
public @NonNull Optional<com.inductiveautomation.ignition.common.gson.JsonElement> getExampleValue()Returns an optional containing an example value if one exists. -
getFormat
- Returns:
- The format specifier for this schema, if there is one, null otherwise. There will also be a format validator added to the schema.
-
getExtension
-
getTypeValidator
Finds the TypeValidator associated with this schema, if there is one. This can be useful if you want to know the accepted types for this schema. -
getItemsValidator
Finds the ItemsValidator associated with this schema, if there is one. The items validator defines an expected shape for each element in a (potentially) arbitrary length sequence. -
isType
Returns true if this schema accepts this type and only this type -
acceptsType
Returns true if this schema accepts this type -
getEnumChoices
-
getSuggestions
-
getDynamicSuggestionPath
-
getVisibleWhenCondition
-
getDeclaredProperties
-
getSchemasForProperties
- Returns:
- a map of declared property names and their individual schemas, or an empty map if this schema does not define properties.
-
getSchemasForItems
- Returns:
- an ordered list of item validators. If a singleton list, pull the
getItemsValidator(), and then checkItemsValidatorviaItemsValidator.isBoundedSchema()to see whether this schema applies to each element in an arbitrarily-long sequence, or only allows a finite list.
-
hasChildren
public boolean hasChildren() -
toJson
public @NonNull com.inductiveautomation.ignition.common.gson.JsonElement toJson()Returns a defensive deep copy of this schema's underlying JSON node — the raw schema document as parsed, before any$refresolution. -
toString
-