|
| 1 | +# json-schema-validator |
| 2 | + |
| 3 | +[](https://opensource.org/license/mit/) |
| 4 | + |
| 5 | +This multiplatform library is an implementation of JSON schema that can validate |
| 6 | +[JsonElement](https://github.com/Kotlin/kotlinx.serialization/blob/master/formats/json/commonMain/src/kotlinx/serialization/json/JsonElement.kt) |
| 7 | +from [kotlinx.serialization-json](https://github.com/Kotlin/kotlinx.serialization/tree/master/formats/json) library. |
| 8 | + |
| 9 | +## Usage |
| 10 | + |
| 11 | +```kotlin |
| 12 | +import com.github.optimumcode.json.schema.JsonSchema |
| 13 | +import com.github.optimumcode.json.schema.ValidationError |
| 14 | +import kotlinx.serialization.json.JsonElement |
| 15 | + |
| 16 | +val key = "\$" // to use $ in multiline string |
| 17 | +val schema = JsonSchema.fromDescription( |
| 18 | + """ |
| 19 | + { |
| 20 | + "${key}schema": "http://json-schema.org/draft-07/schema#", |
| 21 | + "definitions": { |
| 22 | + "positiveInteger": { |
| 23 | + "type": "integer", |
| 24 | + "minimum": 0 |
| 25 | + } |
| 26 | + }, |
| 27 | + "properties": { |
| 28 | + "size": { "${key}ref": "#/definitions/positiveInteger" } |
| 29 | + } |
| 30 | + } |
| 31 | + """.trimIndent(), |
| 32 | +) |
| 33 | +val errors = mutableListOf<ValidationError>() |
| 34 | +val elementToValidate: JsonElement = loadJsonToValidate() |
| 35 | + |
| 36 | +val valid = schema.validate(elementToValidate, errors::add) |
| 37 | +``` |
| 38 | + |
| 39 | +## Supported JSON schema drafts: |
| 40 | + |
| 41 | +- [Draft 7](https://json-schema.org/specification-links.html#draft-7) |
| 42 | + - Keywords |
| 43 | + |
| 44 | + | Keyword | Status | |
| 45 | + |:------------|:-----------------------------------------------------------------------------------------------------------------------| |
| 46 | + | $id | Basic support. Only in root schema. Currently, it is interpreted as a string. Validation is in the future plans | |
| 47 | + | $schema | There is not validation of the $schema property at the moment | |
| 48 | + | $ref | Partially supported. Only references like _**#/path/in/schema**_ will work. The circled references validation is added | |
| 49 | + | definitions | Supported. Definitions are loaded and can be referenced | |
| 50 | + |
| 51 | + - Assertions |
| 52 | + |
| 53 | + | Category | Assertion | Status | |
| 54 | + |:--------------|:---------------------|:------------------------------------------------------------------------------------------------------------------------------------------------| |
| 55 | + | General | type | Supported all type [defined in the specification](https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.1.1) | |
| 56 | + | | enum | Supported | |
| 57 | + | | const | Supported | |
| 58 | + | Numbers | multipleOf | Supported | |
| 59 | + | | maximum | Supported | |
| 60 | + | | exclusiveMaximum | Supported | |
| 61 | + | | minimum | Supported | |
| 62 | + | | exclusiveMinimum | Supported | |
| 63 | + | Strings | maxLength | Supported | |
| 64 | + | | minLength | Supported | |
| 65 | + | | pattern | Supported (kotlin.text.Regex is used) | |
| 66 | + | Arrays | items | Supported | |
| 67 | + | | additionalItems | Supported | |
| 68 | + | | maxItems | Supported | |
| 69 | + | | uniqueItems | Supported | |
| 70 | + | | contains | Supported | |
| 71 | + | Objects | maxProperties | Supported | |
| 72 | + | | minProperties | Supported | |
| 73 | + | | required | Supported | |
| 74 | + | | properties | Supported | |
| 75 | + | | patternProperties | Supported (kotlin.text.Regex is used) | |
| 76 | + | | additionalProperties | Supported | |
| 77 | + | | dependencies | Supported | |
| 78 | + | | propertyNames | Supported | |
| 79 | + | Conditions | if/then/else | Supported | |
| 80 | + | Boolean logic | allOf | Supported | |
| 81 | + | | anyOf | Supported (all validation will be executed even if the element matches the first one) | |
| 82 | + | | oneOf | Supported | |
| 83 | + | | not | Supported | |
| 84 | + |
| 85 | +## Future plans |
| 86 | + |
| 87 | +- [ ] Add `$schema` property validation (if not set the latest supported will be used) |
| 88 | +- [ ] Add proper `$id` support (for nested schemas and for referencing) |
| 89 | +- [ ] Add support for newer drafts |
| 90 | + - [ ] [Draft 2019-09 (Draft 8)](https://json-schema.org/specification-links.html#draft-2019-09-formerly-known-as-draft-8) |
| 91 | + - [ ] [2020-12](https://json-schema.org/specification-links.html#2020-12) |
| 92 | +- [ ] Formalize error output as it is defined in the latest drafts (have not fully decided if it should be done) |
0 commit comments