Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tariel-x/polyschema
JSON-Schema based type resolver for golang
https://github.com/tariel-x/polyschema
json json-schema polymorphism subtyping type-checker type-system types
Last synced: 5 days ago
JSON representation
JSON-Schema based type resolver for golang
- Host: GitHub
- URL: https://github.com/tariel-x/polyschema
- Owner: tariel-x
- License: apache-2.0
- Created: 2018-08-26T13:36:45.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-09-01T22:37:48.000Z (over 6 years ago)
- Last Synced: 2023-12-14T14:10:31.832Z (about 1 year ago)
- Topics: json, json-schema, polymorphism, subtyping, type-checker, type-system, types
- Language: Go
- Homepage:
- Size: 19.5 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Polyschema
[![Go Report Card](https://goreportcard.com/badge/github.com/tariel-x/polyschema)](https://goreportcard.com/report/github.com/tariel-x/polyschema)
JSON-Schema based type resolver for golang.
Type here is any standard json-schema. However standard specification do not consider
any other cases except data validation via schema. This library provides realization of
subtyping and type-checking concept for schemas.Subtype `B` of type `A` here is the new schema describing document `b` which can be
successfully valided with both `A` and `B`. Though document `a` described by `A` is not
committed to be valid for schema `B`. This is called structural type system.### Example
**Type A**
```json
{ "type": "string" }
```**Type B**
```json
{ "type": "string", "maxLength": 10 }
```**Type C**
```json
{ "type": "integer" }
```Type `B` is the subtype of `A`: `A :> B`.
But `C` is neither subtype not equal type of `A`.### Complex example
**Type A**
```json
{
"title": "Person",
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"required": [
"name"
]
}
```**Type B**
```json
{
"title": "Student",
"type": "object",
"properties": {
"name": {
"type": "string"
},
"course": {
"type": "integer"
}
},
"required": [
"name",
"course"
]
}
```**Type C**
```json
{
"title": "Person_not_strict",
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
}
```Type `B` is the subtype of `A`: `A :> B`.
But `C` is neither subtype not equal type of `A`.## Usage of the package
Install
`go get -u github.com/tariel-x/polyschema`
Use
```go
package mainimport (
"encoding/json"
"fmt""github.com/tariel-x/polyschema"
)func main() {
typeA := polyschema.JsonSchema{}
typeB := polyschema.JsonSchema{}
typeC := polyschema.JsonSchema{}json.Unmarshal([]byte(`{ "type": "string" }`), &typeA)
json.Unmarshal([]byte(`{ "type": "string", "maxLength": 10 }`), &typeB)
json.Unmarshal([]byte(`{ "type": "integer" }`), &typeC)fmt.Println("A :> B ", polyschema.Subtype(typeA, typeB)) // 1, that means subtype
fmt.Println("A :> C ", polyschema.Subtype(typeA, typeC)) // -1, that means not subtype
fmt.Println("A = A ", polyschema.Subtype(typeA, typeA)) // 0, that means identity
}
```