Factory method to instantiate Schema
class.
This method is async and it should be used with value
keyword from
future package.
Schema.load(descriptor, strict=FALSE, caseInsensitiveHeaders = FALSE)
descriptor | schema descriptor, a JSON string, URL or file |
---|---|
strict | flag to alter validation behaviour:
|
caseInsensitiveHeaders | default is set to |
Schema
class object
SCHEMA <- '{"fields": [ {"name": "id", "type": "string", "constraints": {"required": true}}, {"name": "height", "type": "number"}, {"name": "age", "type": "integer"}, {"name": "name", "type": "string", "constraints": {"required": true}}, {"name": "occupation", "type": "string"} ]}' # instantiate Schema class def = Schema.load(descriptor = SCHEMA) schema = future::value(def) # correct number of fields length(schema$fields)#> [1] 5# correct field names schema$fieldNames#> [[1]] #> [1] "id" #> #> [[2]] #> [1] "height" #> #> [[3]] #> [1] "age" #> #> [[4]] #> [1] "name" #> #> [[5]] #> [1] "occupation" #># convert row row = list('string', '10.0', '1', 'string', 'string') castRow = schema$castRow(row) castRow#> [[1]] #> [1] "string" #> #> [[2]] #> [1] 10 #> #> [[3]] #> [1] 1 #> #> [[4]] #> [1] "string" #> #> [[5]] #> [1] "string" #>SCHEMA_MIN <- '{ "fields": [ {"name": "id"}, {"name": "height"} ]}' # load schema def2 = Schema.load(descriptor = SCHEMA_MIN) schema2 = future::value(def2) # set default types if not provided schema2$fields[[1]]$type#> [1] "string"schema2$fields[[2]]$type#> [1] "string"# fields are not required by default schema2$fields[[1]]$required#> [1] FALSEschema2$fields[[2]]$required#> [1] FALSE#work in strict mode descriptor = '{"fields": [{"name": "name", "type": "string"}]}' def3 = Schema.load(descriptor = descriptor, strict = TRUE) schema3 = future::value(def3) schema3$valid#> [1] TRUE# work in non-strict mode descriptor = '{"fields": [{"name": "name", "type": "string"}]}' def4 = Schema.load(descriptor = descriptor, strict = FALSE) schema4 = future::value(def4) schema4$valid#> [1] TRUE# work with primary/foreign keys as arrays descriptor2 = '{ "fields": [{"name": "name"}], "primaryKey": ["name"], "foreignKeys": [{ "fields": ["parent_id"], "reference": {"resource": "resource", "fields": ["id"]} }]}' def5 = Schema.load(descriptor2) schema5 = future::value(def5) schema5$primaryKey#> [[1]] #> [1] "name" #>schema5$foreignKeys#> [[1]] #> [[1]]$fields #> [[1]]$fields[[1]] #> [1] "parent_id" #> #> #> [[1]]$reference #> [[1]]$reference$resource #> [1] "resource" #> #> [[1]]$reference$fields #> [[1]]$reference$fields[[1]] #> [1] "id" #> #> #> #># work with primary/foreign keys as string descriptor3 = '{ "fields": [{"name": "name"}], "primaryKey": "name", "foreignKeys": [{ "fields": "parent_id", "reference": {"resource": "resource", "fields": "id"} }]}' def6 = Schema.load(descriptor3) schema6 = future::value(def6) schema6$primaryKey#> [[1]] #> [1] "name" #>schema6$foreignKeys#> [[1]] #> [[1]]$fields #> [[1]]$fields[[1]] #> [1] "parent_id" #> #> #> [[1]]$reference #> [[1]]$reference$resource #> [1] "resource" #> #> [[1]]$reference$fields #> [[1]]$reference$fields[[1]] #> [1] "id" #> #> #> #>