Last active
July 18, 2016 03:30
-
-
Save The-Quill/4c1dce39829e8e2ceae956725d2897aa to your computer and use it in GitHub Desktop.
Making a data type that can support building a schema with both required and not required fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default class DataType { | |
constructor(name){ | |
this._name = name; | |
this._fields = {} | |
this.createDataSet = data => { | |
var newData = {} | |
Object.keys(this._fields).forEach(key => { | |
({ [key]: newData[key] = this._fields[key]() } = data); | |
}) | |
return newData | |
} | |
} | |
setField(name, type){ | |
this._fields[name] = () => '' | |
} | |
setRequiredField(name, type){ | |
this._fields[name] = () => { throw new Error(`missing ${name}`) } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment