Skip to content

Commit 7ce4d65

Browse files
committed
README improvements
1 parent a1aa468 commit 7ce4d65

File tree

1 file changed

+59
-4
lines changed

1 file changed

+59
-4
lines changed

README.md

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* [readOnly and writeOnly context](#readonly-and-writeonly-context)
1717
* [Format validators](#format-validators)
1818
* [Example](#example)
19-
* [Resolution scopes](#resolution-scopes)
19+
* [$ref resolution](#ref-resolution)
2020
* [Javadoc](#javadoc)
2121

2222
<a href="http://jetbrains.com"><img src="./jetbrains-logo.png" /></a> Supported by JetBrains.
@@ -433,10 +433,10 @@ schema.validate(jsonDocument); // the document validation happens here
433433
```
434434

435435

436-
## Resolution scopes
436+
## $ref resolution
437437

438438
In a JSON Schema document it is possible to use relative URIs to refer previously defined
439-
types. Such references are expressed using the `"$ref"` and `"id"` keywords. While the specification describes resolution scope alteration and dereferencing in detail, it doesn't explain the expected behavior when the first occurring `"$ref"` or `"id"` is a relative URI.
439+
types. Such references are expressed using the `"$ref"` and `"$id"` keywords. While the specification describes resolution scope alteration and dereferencing in detail, it doesn't explain the expected behavior when the first occurring `"$ref"` or `"$id"` is a relative URI.
440440

441441
In the case of this implementation it is possible to explicitly define an absolute URI serving as the base URI (resolution scope) using the appropriate builder method:
442442

@@ -447,6 +447,61 @@ SchemaLoader schemaLoader = SchemaLoader.builder()
447447
.build();
448448
```
449449

450+
### Loading from the classpath
451+
452+
As your schemas grow you will want to split that up into multiple source files and wire them with `"$ref"` references.
453+
If you want to store the schemas on the classpath (instead of eg. serving them through HTTP) then the recommended way is
454+
to use the `classpath:` protocol to make the schemas reference each other. To make the `classpath:` protocol work:
455+
* if you use the [Spring framework](https://spring.io) you don't have to do anything, spring installs the necessary
456+
protocol handler out of the box
457+
* otherwise you can utilize the library's built-in classpath-aware `SchemaClient`, exampple:
458+
459+
```java
460+
SchemaLoader schemaLoader = SchemaLoader.builder()
461+
.schemaClient(SchemaClient.classPathAwareClient())
462+
.schemaJson(jsonSchema)
463+
.resolutionScope("classpath://my/schemas/directory/") // setting the default resolution scope
464+
.build();
465+
```
466+
467+
Given this configuration, the following references will be properly resolved in `jsonSchema`:
468+
469+
```json
470+
{
471+
"properties": {
472+
"sameDir": { "$ref": "sameDirSchema.json" },
473+
"absPath": { "$ref": "classpath://somewhere/else/otherschema.json" },
474+
"httpPath": { "$ref": "http://example.org/http-works-as-usual" },
475+
}
476+
}
477+
```
478+
479+
and `sameDirSchema.json` will be looked for in `/my/schemas/directory/sameDirSchema.json` on the classpath.
480+
481+
### Registering schemas by URI
482+
483+
Sometimes it is useful to work with preloaded schemas, to which we assign an arbitary URI (maybe an uuid) instead of
484+
loading the schema through a URL. This can be done by assigning the schemas to a URI with the `#registerSchemaByURI()`
485+
method of the schema loader. Example:
486+
487+
```java
488+
SchemaLoader schemaLoader = SchemaLoader.builder()
489+
.registerSchemaByURI(new URI("urn:uuid:a773c7a2-1a13-4f6a-a70d-694befe0ce63"), aJSONObject)
490+
.registerSchemaByURI(new URI("http://example.org"), otherJSONObject)
491+
.schemaJson(jsonSchema)
492+
.resolutionScope("classpath://my/schemas/directory/") // setting the default resolution scope
493+
.build();
494+
```
495+
496+
Notes:
497+
* the passed schema object must be a `JSONObject` or a `Boolean` (the formal parameter type is `Object` only because
498+
these two don't have any common superclass).
499+
* if you want you can pass a URL with HTTP protocol, it is still a valid URI. Since in this case you pre-assigned a schema
500+
to an URI, there will be no network call made. This can be a caching strategy (though defining your own `SchemaClient`
501+
implementation works too)
502+
503+
504+
450505
[ASL 2.0 badge]: https://img.shields.io/:license-Apache%202.0-blue.svg
451506
[ASL 2.0]: https://www.apache.org/licenses/LICENSE-2.0
452507
[Travis badge master]: https://travis-ci.org/everit-org/json-schema.svg?branch=master
@@ -459,7 +514,7 @@ SchemaLoader schemaLoader = SchemaLoader.builder()
459514

460515
## Javadoc
461516

462-
For the latest releases (1.10.0) the javadoc is published [on erosb.github.io](http://erosb.github.io/everit-json-schema/javadoc/1.10.0/)
517+
For the latest releases (1.11.0) the javadoc is published [on erosb.github.io](http://erosb.github.io/everit-json-schema/javadoc/1.11.0/)
463518

464519
The generated javadoc of versions 1.0.0 - 1.5.1 is available at [javadoc.io](http://javadoc.io/doc/org.everit.json/org.everit.json.schema/1.5.1)
465520

0 commit comments

Comments
 (0)