Skip to content

Commit 68414e7

Browse files
committed
Add additional examples into README
1 parent 11eb889 commit 68414e7

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ implementation 'io.github.optimumcode:json-schema-validator:0.0.11-SNAPSHOT'
103103

104104
### Example
105105

106+
If you have just one JSON schema or many independent schemes
107+
you can create it using factory methods defined on `JsonSchema` class.
108+
106109
```kotlin
107110
import io.github.optimumcode.json.schema.JsonSchema
108111
import io.github.optimumcode.json.schema.ValidationError
@@ -131,6 +134,44 @@ val elementToValidate: JsonElement = loadJsonToValidate()
131134
val valid = schema.validate(elementToValidate, errors::add)
132135
```
133136

137+
If you need to use more than one schema, and they have references to other schemas you should use `JsonSchemaLoader` class.
138+
139+
```kotlin
140+
import io.github.optimumcode.json.schema.JsonSchemaLoader
141+
import io.github.optimumcode.json.schema.JsonSchema
142+
import io.github.optimumcode.json.schema.ValidationError
143+
import kotlinx.serialization.json.JsonElement
144+
145+
val schema: JsonSchema = JsonSchemaLoader.create()
146+
.register(
147+
"""
148+
{
149+
"${KEY}id": "https://test.com",
150+
"properties": {
151+
"name": {
152+
"type": "string"
153+
}
154+
}
155+
}
156+
""".trimIndent(),
157+
).fromDefinition(
158+
"""
159+
{
160+
"properties": {
161+
"anotherName": {
162+
"${KEY}ref": "https://test.com#/properties/name"
163+
}
164+
}
165+
}
166+
""".trimIndent(),
167+
)
168+
169+
val errors = mutableListOf<ValidationError>()
170+
val elementToValidate: JsonElement = loadJsonToValidate()
171+
172+
val valid = schema.validate(elementToValidate, errors::add)
173+
```
174+
134175
## Supported JSON schema drafts:
135176

136177
- [Draft 7](https://json-schema.org/specification-links.html#draft-7)

0 commit comments

Comments
 (0)