Skip to content

Instantly share code, notes, and snippets.

@The-Quill
Last active July 18, 2016 03:30
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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