schema_compact() simplifies schema documents produced by inference or
hand-authoring. It can merge observed array element alternatives and group
sibling fields that share identical schemas.
Examples
schema <- schema_infer(
list(items = list(list(id = 1L, name = "a"), list(id = 2L, label = "b"))),
keys = "named",
arrays = "rest"
)
schema
#> {
#> "check": {
#> "kind": "list"
#> },
#> "keys": {
#> "type": "named"
#> },
#> "fields": {
#> "items": {
#> "check": {
#> "kind": "list"
#> },
#> "keys": {
#> "type": "unnamed"
#> },
#> "rest": {
#> "any": [
#> {
#> "check": {
#> "kind": "list"
#> },
#> "keys": {
#> "type": "named"
#> },
#> "fields": {
#> "id": {
#> "check": {
#> "kind": "int"
#> }
#> },
#> "name": {
#> "check": {
#> "kind": "string"
#> }
#> }
#> }
#> },
#> {
#> "check": {
#> "kind": "list"
#> },
#> "keys": {
#> "type": "named"
#> },
#> "fields": {
#> "id": {
#> "check": {
#> "kind": "int"
#> }
#> },
#> "label": {
#> "check": {
#> "kind": "string"
#> }
#> }
#> }
#> }
#> ]
#> }
#> }
#> }
#> }
schema_compact(schema)
#> {
#> "check": {
#> "kind": "list"
#> },
#> "keys": {
#> "type": "named"
#> },
#> "fields": {
#> "items": {
#> "check": {
#> "kind": "list"
#> },
#> "keys": {
#> "type": "unnamed"
#> },
#> "rest": {
#> "check": {
#> "kind": "list"
#> },
#> "keys": {
#> "type": "named"
#> },
#> "fields": {
#> "id": {
#> "check": {
#> "kind": "int"
#> }
#> }
#> },
#> "groups": [
#> {
#> "names": ["name", "label"],
#> "check": {
#> "kind": "string"
#> }
#> }
#> ]
#> }
#> }
#> }
#> }