Skip to content

Commit

Permalink
implemented schema.valid and schema.errors
Browse files Browse the repository at this point in the history
  • Loading branch information
roll committed Jun 24, 2017
1 parent 720106c commit 701b104
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ rules:
semi:
- error
- never
max-len:
- error
- 120
comma-style:
- off
- first
Expand Down
73 changes: 56 additions & 17 deletions src/schema.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs'
import _ from 'lodash'
import 'isomorphic-fetch'
import lodash from 'lodash'
import {Field} from './field'
import {validate} from './validate'
import * as helpers from './helpers'
Expand All @@ -16,18 +16,40 @@ export class Schema {
* Load Schema instance
* https://github.com/frictionlessdata/tableschema-js#schema
*/
static load(descriptor, {caseInsensitiveHeaders}={caseInsensitiveHeaders: false}) {
return new Promise(async (resolve, reject) => {
try {
descriptor = await helpers.retrieveDescriptor(descriptor)
descriptor = helpers.expandSchemaDescriptor(descriptor)
await validate(descriptor)
const schema = new Schema(descriptor, {caseInsensitiveHeaders})
resolve(schema)
} catch (error) {
reject(error)
}
})
static async load(descriptor, {strict, caseInsensitiveHeaders}={strict: true, caseInsensitiveHeaders: false}) {

// Process descriptor
descriptor = await helpers.retrieveDescriptor(descriptor)
descriptor = helpers.expandSchemaDescriptor(descriptor)

// Process descriptor
// It should be moved to constructor after writing sync validate function
// See proper implementation in datapackage library based on profile class
// Also there should be moved helpers.expandSchemaDescriptor(descriptor)
let errors = []
try {
await validate(descriptor)
} catch (err) {
errors = err
}

return new Schema(descriptor, {strict, caseInsensitiveHeaders, errors})
}

/**
* Schema valid
* https://github.com/frictionlessdata/tableschema-js#schema
*/
get valid() {
return this._errors.length === 0
}

/**
* Schema errors
* https://github.com/frictionlessdata/tableschema-js#schema
*/
get errors() {
return this._errors
}

/**
Expand Down Expand Up @@ -92,7 +114,7 @@ export class Schema {
*/
getField(fieldName, {index}={index: 0}) {
const name = fieldName
const fields = _.filter(this._fields, field => {
const fields = lodash.filter(this._fields, field => {
if (this._caseInsensitiveHeaders) {
return field.name.toLowerCase === name.toLowerCase
}
Expand Down Expand Up @@ -182,18 +204,35 @@ export class Schema {
* https://github.com/frictionlessdata/tableschema-js#schema
*/
update() {
// It should be implemented after writing sync validate function
// See proper implementation in datapackage library based on profile class
// For descriptor tracking should be used this._nextDescriptor as in datapackage
throw new Error('Not Implemented')
}

// Private

constructor(descriptor, {caseInsensitiveHeaders}={caseInsensitiveHeaders: false}) {
constructor(descriptor, {strict, caseInsensitiveHeaders, errors}={strict: true, caseInsensitiveHeaders: false}) {

// Raise errors in strict mode
if (strict && errors.length) {
throw errors
}

// Set attributes
this._errors = errors
this._strict = strict
this._descriptor = descriptor
this._caseInsensitiveHeaders = caseInsensitiveHeaders

// Fill fields
this._fields = []
for (const field of descriptor.fields) {
this._fields.push(new Field(field, {missingValues: this._descriptor.missingValues}))
if (this.valid) {
for (const field of descriptor.fields) {
this._fields.push(new Field(field, {missingValues: this._descriptor.missingValues}))
}
}

}

}
3 changes: 3 additions & 0 deletions src/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ export class Table {
* https://github.com/frictionlessdata/tableschema-js#table
*/
static async load(source, {schema}) {

// Load schema
if (!(schema instanceof Schema)) {
schema = await Schema.load(schema)
}

return new Table(source, {schema})
}

Expand Down
16 changes: 16 additions & 0 deletions test/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const SCHEMA_MIN = {
}

describe('Schema', () => {

beforeEach(done => {
SCHEMA = {
fields: [
Expand Down Expand Up @@ -290,4 +291,19 @@ describe('Schema', () => {
const schema = await Schema.load(descriptor)
assert.deepEqual(schema.castRow(['2005']), [new Date(2005, 0, 1)])
})

it('should work in strict mode', async () => {
const descriptor = {fields: [{name: 'name', type: 'string'}]}
const schema = await Schema.load(descriptor)
assert.deepEqual(schema.valid, true)
assert.deepEqual(schema.errors, [])
})

it('should work in non-strict mode', async () => {
const descriptor = {fields: [{name: 'name', type: 'bad'}]}
const schema = await Schema.load(descriptor, {strict: false})
assert.deepEqual(schema.valid, false)
assert.deepEqual(schema.errors.length, 1)
})

})

0 comments on commit 701b104

Please sign in to comment.