Skip to content
This repository has been archived by the owner on Apr 19, 2022. It is now read-only.

Fail validation when no data is proved at all #253

Open
McSneaky opened this issue Jan 20, 2020 · 0 comments
Open

Fail validation when no data is proved at all #253

McSneaky opened this issue Jan 20, 2020 · 0 comments

Comments

@McSneaky
Copy link

McSneaky commented Jan 20, 2020

In Indicative version 7.4.4 validation is passing when there is no data to validate and schema contains multiple required fields.

Example code:

validate() {
  const data = undefined;
  const schema = {
    campaignId: 'required|number',
    connectionId: 'required|number',
    objectType: 'required|string'
  };

  return validate(data, schema, {}, {
    removeAdditional: true,
  }).catch(([error]) => {
    throw new Error(error.message);
  });
}

Then calling it with await validate() will always pass

Workaround is to manually check if data exists or not

validate(data) {
  if (!data) {
    throw new Error('No data given');
  }
  const schema = {
    campaignId: 'required|number',
    connectionId: 'required|number',
    objectType: 'required|string'
  };

  return validate(data, schema, {}, {
    removeAdditional: true,
  }).catch(([error]) => {
    throw new Error(error.message);
  });
}

Maybe could make it as default that it fails or if not default, then perhaps some option like allowEmpty that is set to true by default, so it won't break any existing code

Are you willing to work on it with little guidance?

Yes

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

No branches or pull requests

1 participant