0.11.0 • Published 4 years ago

@lion/validate v0.11.0

Weekly downloads
84
License
MIT
Repository
github
Last release
4 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

4 years ago

0.10.0

4 years ago

0.9.2

4 years ago

0.9.1

4 years ago

0.9.0

4 years ago

0.8.0

4 years ago

0.7.1

4 years ago

0.7.0

4 years ago

0.6.8

4 years ago

0.6.7

4 years ago

0.6.6

4 years ago

0.6.5

4 years ago

0.6.3

4 years ago

0.6.4

4 years ago

0.6.2

4 years ago

0.6.1

4 years ago

0.6.0

4 years ago

0.5.4

4 years ago

0.5.3

4 years ago

0.5.2

4 years ago

0.5.1

4 years ago

0.5.0

4 years ago

0.4.1

4 years ago

0.4.2

4 years ago

0.3.1

5 years ago

0.4.0

5 years ago

0.3.0

5 years ago

0.2.40

5 years ago

0.2.39

5 years ago

0.2.38

5 years ago

0.2.37

5 years ago

0.2.36

5 years ago

0.2.35

5 years ago

0.2.34

5 years ago

0.2.33

5 years ago

0.2.32

5 years ago

0.2.31

5 years ago

0.2.30

5 years ago

0.2.29

5 years ago

0.2.28

5 years ago

0.2.27

5 years ago

0.2.26

5 years ago

0.2.25

5 years ago

0.2.24

5 years ago

0.2.23

5 years ago

0.2.22

5 years ago

0.2.21

5 years ago

0.2.20

5 years ago

0.2.19

5 years ago

0.2.18

5 years ago

0.2.17

5 years ago

0.2.16

5 years ago

0.2.15

5 years ago

0.2.14

5 years ago

0.2.13

5 years ago

0.2.12

5 years ago

0.2.11

5 years ago

0.2.10

5 years ago

0.2.9

5 years ago

0.2.8

5 years ago

0.2.7

5 years ago

0.2.6

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.8

5 years ago

0.1.7

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago