Skip to content

Commit

Permalink
Merge pull request #8 from thedumbterminal/anyOfMultipleTypes
Browse files Browse the repository at this point in the history
Supporting nullable types using anyof syntax
  • Loading branch information
thedumbterminal authored Aug 13, 2018
2 parents 99e2f9a + 2b9969a commit 197c986
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v0.0.7 (13/08/2018)

* Support fields with nullable types using `anyOf` method.

## v0.0.6 (11/08/2018)

* Supports `allOf`, `anyOf` and `oneOf` combined schemas.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsonschema-bigquery",
"version": "0.0.6",
"version": "0.0.7",
"description": "Convert JSON schema to Google BigQuery schema",
"main": "src/index.js",
"scripts": {
Expand Down
9 changes: 9 additions & 0 deletions src/merger.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ merger._mergeItem = (dst, src) => {
return merger._mergeItem(dst[entry[0]], entry[1])
}

//Convert type to an array if needed
if(entry[0] === 'type'){
const types = new Set([dst[entry[0]], entry[1]])
if(types.size > 1){
dst[entry[0]] = Array.from(types)
}
return
}

throw new Error(`Incompatible field: ${entry[0]}`)
})
return dst
Expand Down
11 changes: 11 additions & 0 deletions test/samples/anyOfMultipleTypes/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"schema": {
"fields": [
{
"name": "first_name",
"type": "STRING",
"mode": "NULLABLE"
}
]
}
}
20 changes: 20 additions & 0 deletions test/samples/anyOfMultipleTypes/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"id": "http://yourdomain.com/schemas/myschema.json",
"description": "Example description",
"type": "object",
"properties": {
"first_name": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
]
}
},
"required": [
"first_name"
]
}

0 comments on commit 197c986

Please sign in to comment.