Skip to content

Commit

Permalink
Merge pull request #2 from thedumbterminal/typeArray
Browse files Browse the repository at this point in the history
support fields types as arrays and descriptions
  • Loading branch information
thedumbterminal authored Jul 25, 2018
2 parents 553188a + 17c583b commit 065a494
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 6 deletions.
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.3 (25/07/2018)

* Now supporting fields with type(s) as an array.

## v0.0.2 (23/07/2018)

* Required fields supported.
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.2",
"version": "0.0.3",
"description": "Convert JSON schema to Google BigQuery schema",
"main": "src/index.js",
"scripts": {
Expand Down
24 changes: 19 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,37 @@ jsonSchemaBigquery._convertProperties = (schema, required = []) => {
if(jsonSchemaBigquery._isComplex(schema[item])){
return jsonSchemaBigquery._convertComplexProperty(item, schema[item])
}
const initialMode = jsonSchemaBigquery._initialMode(required, item)
return jsonSchemaBigquery._convertProperty(item, schema[item], initialMode)
const mode = jsonSchemaBigquery._getMode(required, item, schema[item])
return jsonSchemaBigquery._convertProperty(item, schema[item], mode)
})
}

jsonSchemaBigquery._initialMode = (required, field) => {
return required.includes(field) ? 'REQUIRED' : 'NULLABLE'
jsonSchemaBigquery._getMode = (required, name, item) => {
if(Array.isArray(item.type) && item.type.includes('null')){
return 'NULLABLE'
}
return required.includes(name) ? 'REQUIRED' : 'NULLABLE'
}

jsonSchemaBigquery._convertProperty = (name, value, mode) => {
return {
name: name,
type: typeMapping[value.type],
type: jsonSchemaBigquery._getType(value.type),
mode: mode
}
}

jsonSchemaBigquery._getType = (types) => {
if(!Array.isArray(types)){
return typeMapping[types]
}
const notNullTypes = types.filter(item => item !== 'null')
if(notNullTypes.length > 1){
throw new Error('Multiple types are not allowed')
}
return typeMapping[notNullTypes[0]]
}

jsonSchemaBigquery._convertComplexProperty = (name, contents) => {
return {
name: name,
Expand Down
16 changes: 16 additions & 0 deletions test/samples/typeArray/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"schema": {
"fields": [
{
"name": "first_name",
"type": "STRING",
"mode": "NULLABLE"
},
{
"name": "last_name",
"type": "STRING",
"mode": "NULLABLE"
}
]
}
}
24 changes: 24 additions & 0 deletions test/samples/typeArray/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"id": "http://yourdomain.com/schemas/myschema.json",
"description": "Example description",
"type": "object",
"properties": {
"first_name": {
"description": "the first name",
"type": [
"string",
"null"
]
},
"last_name": {
"description": "the first name",
"type": [
"string",
"null"
]
}
},
"required": [
"first_name"
]
}

0 comments on commit 065a494

Please sign in to comment.