Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validating a nested object depending on another data member 🤔 #263

Open
anirudhr95 opened this issue Dec 17, 2021 · 2 comments
Open

Validating a nested object depending on another data member 🤔 #263

anirudhr95 opened this issue Dec 17, 2021 · 2 comments

Comments

@anirudhr95
Copy link

Hello,
I have an object - which looks like this 👇

export const user = {
  id: undefined,
  name: undefined,
  createdOn: undefined,
  contact: {
    title: '',
    honorific: '',
    lastName: '',
    firstName: '',
    email: '',
    countryCode: '',
    phone: '',
  } as Contact,
  organizationType: undefined,
  advertiserBrands: box([{
    name: '',
    category: '',
    prohibitions: [],
  }]),
};

I know how to validate objects conditionally i.e, Validate name, only if ID is present
like so

const formValidation = updateGroup<CreateOrganizationForm>({
  name: (name, formState) => {
    return (formState.values.id !=== undefined) ? validate(name, required) : validate(name, () => ({}));
   },
  }),
}

However, when I try to do this for a nested object, the scope of FormState is the nested object itself and not the entire object.

What I want to do is - check for Advertiser.Name, Advertiser.category and Advertiser.Prohibitions only if organizationType is not undefined. If I try to do this - the auto-completion in my IDE only shows this :(
Screenshot 2021-12-17 at 9 09 14 AM

How do I do this correctly 🤔

  advertiserBrands: updateGroup<AdvertiserBrand>({
    name: (name, formState) => {
      return formState.organizationType === OrganizationType.ADVERTISER ? validate(name, required) : validate(name, () => ({}));
    }
  })

(What I have currently ☝️)

@anirudhr95
Copy link
Author

Note: I also tried formState.values and formState.controls, I get the same thing
Screenshot 2021-12-17 at 9 12 27 AM

@mminor-dev
Copy link

sorry, this might be flakey/not precise with no stackblitz or anything, but essentially you just need to scope your updates to the context of the entire form, so you can query outside the intended updateGroup

const formValidation = updateGroup<CreateOrganizationForm>({
	// @ab:  shorthand for `advertiserBrands`, gets passed explicitly to child `updateGroup`
	// @form:  the parent `FormGroupState`
	advertiserBrands: (ab, form) => updateGroup<AdvertiserBrands>(ab, {
		// @c:  shorthand for `FormControlState`
		name: c => form.value.organizationType === OrganizationType.ADVERTISER ? validate(c, required) : c
	})
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants