I have a domain object Form and this form is being updated with data in random order.
So that makes my domain relax the required fields on the form.
Whenever user has to do an action with I the form has to be filled.
My main question: How do you deal with transition from object having optional fields to same structure object but with all required.
Few subquestions:
- Does the
assertFormFilledfunction makes sense? - Is there a way to automate assertion of all properties.
- Am I missing some domain concept here?
- Does
Partial<IForm>in domain layer makes better sense thanRequired<IForm>indoSomething?
interface IForm {
question1?: string
question2?: boolean
question3?: number
}
class Form implements IForm {
question1: string | undefined
question2: boolean | undefined
question3: number | undefined
}
class User {
form?: IForm
doSomething = (form: Required<IForm>): void => {
//todo use filled form
}
assertFormFilled = (): Required<IForm> => {
if (this.form && this.form.question1 && !!this.form.question2 && !!this.form.question3) {
return {
question1: this.form.question1,
question2: this.form.question2,
question3: this.form.question3,
}
}
else {
throw new Error('Form is not filled')
}
}
}
const user = new User()
user.form = { question2: false}
const form = user.assertFormFilled()
user.doSomething(form)
For example.