Skip to content

Commit

Permalink
Merge pull request #1 from thedumbterminal/nullable
Browse files Browse the repository at this point in the history
Support required fields
  • Loading branch information
thedumbterminal authored Jul 23, 2018
2 parents 6763f34 + 16bc947 commit 553188a
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 19 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.2 (23/07/2018)

* Required fields supported.

## v0.0.1 (21/07/2018)

* Initial release.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ Please ensure that the input JSON schema is dereferenced so that all external re

## TODO

* Repeated fields
* Enums
* Nullable fields
* Error messages
* Descriptions
* Repeated fields.
* Enums.
* Error messages.
* Descriptions.
* Handle `oneOf`, `anyOf` and `allOf`.
* Fields with multiple types.

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.1",
"version": "0.0.2",
"description": "Convert JSON schema to Google BigQuery schema",
"main": "src/index.js",
"scripts": {
Expand Down
25 changes: 17 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ const jsonSchemaBigquery = module.exports = {}

//Json schema on the left, bigquery on the right
const typeMapping = {
'string': 'string',
'boolean': 'boolean'
'string': 'STRING',
'boolean': 'BOOLEAN',
'date-time': 'TIMESTAMP',
'integer': 'INTEGER',
'number': 'FLOAT'
}

jsonSchemaBigquery.convert = (jsonSchema) => {
Expand All @@ -12,33 +15,39 @@ jsonSchemaBigquery.convert = (jsonSchema) => {
}
return {
schema: {
fields: jsonSchemaBigquery._convertProperties(jsonSchema.properties)
fields: jsonSchemaBigquery._convertProperties(jsonSchema.properties, jsonSchema.required)
}
}
}

jsonSchemaBigquery._isComplex = (schema) => schema.type === 'object'

jsonSchemaBigquery._convertProperties = (schema) => {
jsonSchemaBigquery._convertProperties = (schema, required = []) => {
return Object.keys(schema).map((item) => {
if(jsonSchemaBigquery._isComplex(schema[item])){
return jsonSchemaBigquery._convertComplexProperty(item, schema[item])
}
return jsonSchemaBigquery._convertProperty(item, schema[item])
const initialMode = jsonSchemaBigquery._initialMode(required, item)
return jsonSchemaBigquery._convertProperty(item, schema[item], initialMode)
})
}

jsonSchemaBigquery._convertProperty = (name, value) => {
jsonSchemaBigquery._initialMode = (required, field) => {
return required.includes(field) ? 'REQUIRED' : 'NULLABLE'
}

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

jsonSchemaBigquery._convertComplexProperty = (name, contents) => {
return {
name: name,
type: 'record',
type: 'RECORD',
fields: jsonSchemaBigquery._convertProperties(contents.properties)
}
}
8 changes: 5 additions & 3 deletions test/samples/complex/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
"fields": [
{
"name": "address",
"type": "record",
"type": "RECORD",
"fields":[
{
"name": "street_address",
"type": "string"
"type": "STRING",
"mode": "NULLABLE"
},
{
"name": "country",
"type": "string"
"type": "STRING",
"mode": "NULLABLE"
}
]
}
Expand Down
16 changes: 16 additions & 0 deletions test/samples/nullable/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"schema": {
"fields": [
{
"name": "first_name",
"type": "STRING",
"mode": "REQUIRED"
},
{
"name": "last_name",
"type": "STRING",
"mode": "NULLABLE"
}
]
}
}
18 changes: 18 additions & 0 deletions test/samples/nullable/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"id": "http://yourdomain.com/schemas/myschema.json",
"description": "Example description",
"type": "object",
"properties": {
"first_name": {
"description": "the first name",
"type": "string"
},
"last_name": {
"description": "the last name",
"type": "string"
}
},
"required": [
"first_name"
]
}
6 changes: 4 additions & 2 deletions test/samples/simple/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
"fields": [
{
"name": "first_name",
"type": "string"
"type": "STRING",
"mode": "NULLABLE"
},
{
"name": "last_name",
"type": "string"
"type": "STRING",
"mode": "NULLABLE"
}
]
}
Expand Down

0 comments on commit 553188a

Please sign in to comment.