@bedrockstreaming/form-validation-rule-list
TypeScript icon, indicating that this package has built-in type declarations

0.9.2 • Public • Published

🧑‍⚖️ form-validation-rule-list

This utility package allows to add a visual validation feedback to your users. It exposes a Higher Order Component that helps displaying a list of rules with 3 possible states for each one: default, incomplete, complete

❓ Why ?

By default, you can already validate your forms without adding any other library to the mix. It is also very trivial to display some error message since every dictionary component are receiving it from props. So why would you need something like form validation rule list ? To give your users some hints about what is going wrong with their typing. It's too often that we apply several validations rules on a single input and many of those rules are complaining at the same time, then what to display ?

🏗️ Install

npm install @bedrockstreaming/form-validation-rule-list

🚀 Usage

Extra validation

const extraValidation = {
  oneUpperCaseCharacter: () => (fieldValue) => /(?=.*[A-Z])/.test(fieldValue),
  minLength: (minLengthValue) => (fieldValue) =>
    fieldValue && fieldValue.length >= minLengthValue,
};

Schema

const schema = {
  fields: {
    password: {
      id: 'password',
      type: 'password',
      validation: {
        oneUpperCaseCharacter: {
          key: 'oneUpperCaseCharacter',
          message: 'You need at least one upper cased character',
        },
        minLength: {
          key: 'minLength',
          message: 'You password should be minimum 12 characters long',
          value: 12,
        },
      },
    },
  },
  steps: {...},
  stepsById: [...]
};

Dictionary

import {
  getValidationRulesHints,
  checkRules,
  withValidationRuleList,
} from '@bedrockstreaming/form-validation-rule-list';

import { PasswordTextField } from '@mylib/textfield';
import { RuleList } from '@mylib/rule-list';

const ValidatedPasswordTextField = withValidationRuleList(PasswordTextField);

const dictionary = {
  password: ({ errors, validation, label, ...props }) => {
    // These rules don't match react-hook-form's rule API, its proper to the form-validation-rule-list package
    const rules = getValidationRulesHints({
      errors,
      validation,
    });

    const hasError = !!checkRules(props.value, rules).length;
    const fieldError = errors && errors.type;
    const isValid = !!(props.value && !hasError && !fieldError);

    return (
      <div>
        <ValidatedPasswordTextField
          label={label}
          hasError={hasError}
          valid={isValid}
          {...props}
          // specific props required by ValidationRuleList
          rules={rules}
          ruleComponent={RuleList}
        />
      </div>
    );
  },
};

⚠️ Beware, you can't use several react-hook-form default rules as validation hints since the errors object returned by the library can only contain one default rule error at a time.

Running unit tests

Run nx test form-validation-rule-list to execute the unit tests via Jest.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.9.3-fix-toomuch-calls.21dev
0.9.21latest

Version History

VersionDownloads (Last 7 Days)Published
0.9.3-fix-toomuch-calls.21
0.9.3-fix-toomuch-calls.11
0.9.3-fix-toomuch-calls.01
0.9.21
0.9.11
0.9.1-fix-required.00
0.9.01
0.8.5-add-meta-display-required-hint.00
0.8.5-bump-nx.00
0.8.5-update-nx-13.00
0.8.41
0.8.4-testprerelease.10
0.8.4-testprerelease.00
0.8.31
0.8.21
0.8.11
0.8.01
0.7.11
0.7.01
0.6.11
0.6.01
0.5.11
0.5.01
0.4.11
0.4.01
0.3.01
0.2.11
0.2.01
0.1.01
0.0.11

Package Sidebar

Install

npm i @bedrockstreaming/form-validation-rule-list

Weekly Downloads

24

Version

0.9.2

License

none

Unpacked Size

84 kB

Total Files

10

Last publish

Collaborators

  • tetsu9901
  • jsulpis
  • martinschneider01
  • jcoquet
  • fdubost
  • slashgear
  • hpierre74
  • nbazille