Skip to content

Commit

Permalink
Merge pull request #49 from thedumbterminal/bq-column-naming
Browse files Browse the repository at this point in the history
Make sure fields are in BigQuery's format
  • Loading branch information
thedumbterminal authored Sep 27, 2021
2 parents fac77aa + a789806 commit e2445ca
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 22 deletions.
7 changes: 0 additions & 7 deletions .travis.yml

This file was deleted.

4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v4.1.0 (27/09/2021)

* Now checks for field name format.

## v4.0.1 (12/02/2021)

* Support more formats for table ID utility.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# jsonschema-bigquery

[![npm](https://img.shields.io/npm/v/jsonschema-bigquery.svg)](https://www.npmjs.com/package/jsonschema-bigquery)
[![Build Status](https://travis-ci.com/thedumbterminal/jsonschema-bigquery.svg?branch=master)](https://travis-ci.com/github/thedumbterminal/jsonschema-bigquery)
[![Node.js CI](https://github.com/thedumbterminal/jsonschema-bigquery/actions/workflows/main.yml/badge.svg)](https://github.com/thedumbterminal/jsonschema-bigquery/actions/workflows/main.yml)

Convert JSON schema to Google BigQuery schema

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": "4.0.1",
"version": "4.1.0",
"description": "Convert JSON schema to Google BigQuery schema",
"main": "src/converter.js",
"scripts": {
Expand Down
24 changes: 11 additions & 13 deletions src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const JSON_SCHEMA_TO_BIGQUERY_TYPE_DICT = {

const OFS = ['allOf', 'anyOf', 'oneOf']

const BIGQUERY_FIELD_NAME_REGEXP = /^[a-z_][a-z0-9_]+$/i

converter._merge_property = (merge_type, property_name, destination_value, source_value) => {
// Merges two properties.
let destination_list
Expand Down Expand Up @@ -112,10 +114,14 @@ converter._merge_dicts = (merge_type, dest_dict, source_dict) => {
}

converter._scalar = (name, type, mode, description) => {
if(!name.match(BIGQUERY_FIELD_NAME_REGEXP)){
throw new SchemaError(`Invalid field name: ${name}`)
}

const result = {
name: name,
type: type,
mode: mode
name,
type,
mode
}

if (description) {
Expand Down Expand Up @@ -160,16 +166,8 @@ converter._object = (name, node, mode) => {
return field != null
})

result = {
name: name,
type: 'RECORD',
mode: mode,
fields: fields
}

if(node.description){
result.description = node.description
}
result = converter._scalar(name, 'RECORD', mode, node.description)
result.fields = fields
}
catch (e) {
if(!converter._options.continueOnError){
Expand Down
22 changes: 22 additions & 0 deletions test/unit/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,26 @@ describe('converter unit', () => {
})
})
})

describe('_scalar()', () => {
context('with an invalid field', () => {
it('throws an error', () => {
assert.throws(() => {
converter._scalar('123test', 'STRING', 'NULLABLE')
}, /Invalid field name: 123test/)
})
})
})

describe('_scalar()', () => {
context('with a valid field', () => {
it('returns a bigquery field object', () => {
assert.deepStrictEqual(converter._scalar('test123', 'STRING', 'NULLABLE'), {
mode: 'NULLABLE',
name: 'test123',
type: 'STRING'
})
})
})
})
})

0 comments on commit e2445ca

Please sign in to comment.