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