zapier-platform icon indicating copy to clipboard operation
zapier-platform copied to clipboard

Child field ‘required’ settings are globally scoped, but maybe should be parent-scoped

Open wking opened this issue 7 years ago • 3 comments

I'm getting:

Error: missing the required address field locality

with inputFields like:

{
  key: 'address',
  label: 'Address',
  children: [
    {
      key: 'street-address',
      label:'Street Address',
      type: 'string',
      placeholder: '123 Main St.'
    },
    {
      key: 'locality',
      label: 'Locality',
      helpText: 'For most addresses, this is the city name.',
      type: 'string',
      required: true,
      placeholder: 'Anytown'
    },
    …
  ]
}

My intention is to have address be optional as a whole (e.g. {} would be valid input) but if address is set, then address.locality must be set (e.g. {address: {locality: 'Anytown'}} would be valid, while {address: {}} would be invalid).

wking avatar Mar 30 '18 23:03 wking

Hey there!

In this case, it's best to set everything optional in the schema and validate input in your perform function. Something to the effect of:

if (bundle.inputData.address && !bundle.inputData.address.locality) {
  throw new Error('Locality is required when an address is provided')
}

Make sure to add help text describing that restriction.

xavdid avatar Apr 03 '18 04:04 xavdid

On Tue, Apr 03, 2018 at 04:01:17AM +0000, David Brownman wrote:

In this case, it's best to …

Right, that's what I'm doing. But:

There's no loss-of-generality with parent-scoped required; folks who want to require child fields just need to set required for both the parent and child. And having platform support for it saves dev time by consolidating the work in the platform libraries vs. repeating it in any number of consumers.

This is obviously not a must-have issue, but I think we can leave this open as a feature request. And if there are public repositories where I can put in work towards supporting this use-case, please point me at them. In the mean time, I think we should document the current scoping in the schema repo.

wking avatar Apr 03 '18 04:04 wking

That's fair! I believe the generated fields are plugged into our formatic library and presented (though I don't know enough about the minor details of our front end to confirm that). @mjyoung might be able to say for sure.

xavdid avatar Apr 03 '18 04:04 xavdid