Skip to content

Instantly share code, notes, and snippets.

@The-Quill
Last active July 18, 2016 03:30
Show Gist options
  • Save The-Quill/4c1dce39829e8e2ceae956725d2897aa to your computer and use it in GitHub Desktop.
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
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