You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Adds support for translating one URI into another (#682)
* Adds support for translating one URI into another
* Adds documentation about URITranslator.
---------
Co-authored-by: Faron Dutton <[email protected]>
Co-authored-by: fdutton <fdutton@noreply>
Copy file name to clipboardExpand all lines: doc/schema-map.md
+125-8Lines changed: 125 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,24 +1,141 @@
1
1
While working with JSON schema validation, we have to use external references sometimes. However, there are two issues to have references to schemas on the Internet.
2
2
3
3
* Some applications are running inside a corporate network without Internet access.
4
-
* Some of the Internet resources are not reliable
4
+
* Some of the Internet resources are not reliable.
5
+
* A test environment may serve unpublished schemas, which are not yet available at the location identified by the payload's `$schema` property.
5
6
6
7
One solution is to change all the external reference to internal in JSON schemas, but this is error-prone and hard to maintain in a long run.
7
8
8
9
A smart solution is to map the external references to internal ones in a configuration file. This allows us to use the resources as they are without any modification. In the JSON schema specification, it is not allowed to use local filesystem resource directly. With the mapping, we can use the local resources without worrying about breaking the specification as the references are still in URL format in schemas. In addition, the mapped URL can be a different external URL, or embbeded within a JAR file with a lot more flexibility.
9
10
10
11
Note that when using a mapping, the local copy is always used, and the external reference is not queried.
11
12
12
-
### Usage
13
+
### URI Translation
13
14
14
-
Basically, you can specify a mapping in the builder. For more details, please take a look at the test cases and the [PR](https://github.com/networknt/json-schema-validator/pull/125).
15
+
Both `SchemaValidatorsConfig` and `JsonSchemaFactory` accept one or more `URITranslator` instances. A `URITranslator` is responsible for providing a new URI when the given URI matches certain criteria.
In case you provide the schema through an `InputStream` or a `String` to resolve `$ref` with URN (relative path), you need to provide the `URNFactory` to the `JsonSchemaFactory.Builder.
22
-
URNFactory` interface will allow you to resolve URN to URI.
Both `SchemaValidatorsConfig` and `JsonSchemaFactory` accept multiple `URITranslator`s and in general, they are evaluated in the order of addition. This means that each `URITranslator` receives the output of the previous translator. For example, assuming the following configuration:
Given a starting URI of `https://schemas.acme.org/Foo`, the configuration above produces the following translations (in order):
131
+
132
+
1.The translation from HTTP to HTTPS does not occur since the original URI already specifies HTTPS.
133
+
2.The second rule receives the original URI since nothing happened in the first rule. The second rule translates the URI from `https://schemas.acme.org/Foo` to `http://test-schemas.acme.org:8080/Foo` since the scheme, host and port match this rule.
134
+
3.The third rule receives the URI produced by the second rule and performs a simple mapping to a local resource.
135
+
136
+
Since all `JsonSchemaFactory`s are created from an optional `SchemaValidatorsConfig`, any `URITranslator`s added to the factory are evaluated after those provided by `SchemaValidatorsConfig`.
137
+
138
+
### Deprecated
139
+
140
+
Previously, this library supported simple mappings from one URI to another through `SchemaValidatorsConfig.setUriMappings()` and `JsonSchemaFactory.addUriMappings()`.Usage of these methods are still supported but are now discouraged. `URITranslator` provides a more powerful mechanism of dealing with URI mapping than what was provided before.
23
141
24
-
please take a look at the test cases and the [PR](https://github.com/networknt/json-schema-validator/pull/274).
0 commit comments