All files / validators validators.js

46.66% Statements 14/30
26.66% Branches 8/30
36.36% Functions 4/11
46.66% Lines 14/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96      27x 178x 178x 154x   24x     27x                   27x 80x 40x 80x           27x                 27x                             27x                               27x                                                  
import { FormattedMessage } from 'react-intl';
import FormattedKintMessage from '../FormattedKintMessage';
 
const required = value => {
  const blankString = /^\s+$/;
  if ((value && !blankString.test(value)) || value === false || value === 0) {
    return undefined;
  }
  return <FormattedMessage id="stripes-core.label.missingRequiredField" />;
};
 
const requiredObject = (formValue = {}) => {
  // withKiwtFieldArray sets the _delete property on new objects by default
  // eslint-disable-next-line no-unused-vars
  const { _delete, ...value } = formValue;
  if (Object.keys(value).length === 0) {
    return <FormattedMessage id="stripes-core.label.missingRequiredField" />;
  }
  return undefined;
};
 
const composeValidators = (...validators) => (
  (value, allValues, meta) => (
    validators.reduce((error, validator) => (
      error || validator(value, allValues, meta)
    ), undefined)
  )
);
 
// Similar to the above, but allow explicit setting of arguments to the validators
const composeValidatorsWithArgs = (...validators) => (
  (...args) => (
    validators.reduce((error, validator) => (
      error || validator(...args)
    ), undefined)
  )
);
 
// Make same shape as rangeOverflow/rangeUnderflow so we can combine them
const invalidNumber = (value, _min, _max, intlKey, intlNS, labelOverrides = {}) => {
  if (!value && value !== 0) {
    return (
      <FormattedKintMessage
        id="errors.invalidNumber"
        intlKey={intlKey}
        intlNS={intlNS}
        overrideValue={labelOverrides?.invalidNumberError}
      />
    );
  }
 
  return undefined;
};
 
const rangeOverflow = (value, min, max, intlKey, intlNS, labelOverrides = {}) => {
  if ((value || value === 0) && value > max) {
    return (
      <FormattedKintMessage
        id="errors.decimalValueNotInRange"
        intlKey={intlKey}
        intlNS={intlNS}
        overrideValue={labelOverrides?.decimalValueNotInRangeError}
        values={{ min, max }}
      />
    );
  }
 
  return undefined;
};
 
const rangeUnderflow = (value, min, max, intlKey, intlNS, labelOverrides = {}) => {
  if ((value || value === 0) && value < min) {
    return (
      <FormattedKintMessage
        id="errors.decimalValueNotInRange"
        intlKey={intlKey}
        intlNS={intlNS}
        overrideValue={labelOverrides?.decimalValueNotInRangeError}
        values={{ min, max }}
      />
    );
  }
 
  return undefined;
};
 
export {
  composeValidators,
  composeValidatorsWithArgs,
  invalidNumber,
  rangeOverflow,
  rangeUnderflow,
  required,
  requiredObject,
};