0.11.0 • Published 5 years ago

@lion/validate v0.11.0

Weekly downloads
84
License
MIT
Repository
github
Last release
5 years ago

Validate

Features

  • allow for advanced UX scenarios by updating validation state on every value change
  • provide a powerful way of writing validation via pure functions
  • multiple validation types(error, warning, info, success)
  • default validators
  • custom validators

Validation is applied by default to all form controls via the ValidateMixin.

For a detailed description of the validation system and the ValidateMixin, please see ValidationSystem.

How to use

Installation

npm i --save @lion/validate
import '@lion/input/lion-input.js';
import { %ValidatorName% } from '@lion/validate';

Note that we import an lion-input here as an example of a form control implementing ValidateMixin. We could equally well use lion-textarea, lion-select, lion-fieldset etc. to illustrate our example.

Example

All validators are provided as pure functions. They should be applied to the formcontrol (implementing ValidateMixin) as follows:

import '@lion/input/lion-input.js';
import { Required, IsString, MaxLength, DefaultSuccess, Validator } from '@lion/validate';

const isInitialsRegex = /^([A-Z]\.)+$/;
class IsInitialsExample extends Validator {
  static get validatorName() {
    return 'IsExampleInitials';
  }

  execute(value) {
    let hasFeedback = false;
    const isStringValidator = new IsString();
    if (!isStringValidator.execute(value) || !isInitialsRegex.test(value.toLowerCase())) {
      hasFeedback = true;
    }
    return hasFeedback;
  }

  static getMessage({ fieldName }) {
    return `Please enter a valid {fieldName} in the format "A.B.C.".`;
  }
}
<lion-input
  label="Initials"
  name="initials"
  .validators="${[new Required(), new MaxLength(10), new IsInitialsExample(null, { type: 'warning' }), new DefaultSuccess()]}"
></lion-input>

In the example above we use different types of validators. A validator applied to .validators expects an array with a function, a parameters object and optionally an additional configuration object.

MinMaxLength({ min: 5, max: 10 });

The custom IsInitialsExample checks if the value is fitting our regex, but does not prevent the user from submitting other values.

Retrieving validity states is as easy as checking for:

myInitialsInput.hasFeedbackFor.include('error');
0.11.0

5 years ago

0.10.0

5 years ago

0.9.2

5 years ago

0.9.1

5 years ago

0.9.0

5 years ago

0.8.0

5 years ago

0.7.1

5 years ago

0.7.0

5 years ago

0.6.8

5 years ago

0.6.7

5 years ago

0.6.6

5 years ago

0.6.5

5 years ago

0.6.3

5 years ago

0.6.4

5 years ago

0.6.2

5 years ago

0.6.1

5 years ago

0.6.0

5 years ago

0.5.4

5 years ago

0.5.3

6 years ago

0.5.2

6 years ago

0.5.1

6 years ago

0.5.0

6 years ago

0.4.1

6 years ago

0.4.2

6 years ago

0.3.1

6 years ago

0.4.0

6 years ago

0.3.0

6 years ago

0.2.40

6 years ago

0.2.39

6 years ago

0.2.38

6 years ago

0.2.37

6 years ago

0.2.36

6 years ago

0.2.35

6 years ago

0.2.34

6 years ago

0.2.33

6 years ago

0.2.32

6 years ago

0.2.31

6 years ago

0.2.30

6 years ago

0.2.29

6 years ago

0.2.28

6 years ago

0.2.27

6 years ago

0.2.26

6 years ago

0.2.25

6 years ago

0.2.24

6 years ago

0.2.23

6 years ago

0.2.22

6 years ago

0.2.21

6 years ago

0.2.20

6 years ago

0.2.19

6 years ago

0.2.18

6 years ago

0.2.17

6 years ago

0.2.16

6 years ago

0.2.15

6 years ago

0.2.14

6 years ago

0.2.13

6 years ago

0.2.12

6 years ago

0.2.11

6 years ago

0.2.10

6 years ago

0.2.9

6 years ago

0.2.8

6 years ago

0.2.7

6 years ago

0.2.6

6 years ago

0.2.5

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.8

6 years ago

0.1.7

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago