Transition from Partial to Required

Viewed 37

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:

  1. Does the assertFormFilled function makes sense?
  2. Is there a way to automate assertion of all properties.
  3. Am I missing some domain concept here?
  4. Does Partial<IForm> in domain layer makes better sense than Required<IForm> in doSomething?
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.

0 Answers
Related