Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@anypoint-web-components/validator-mixin

anypoint-web-components2.8kApache-2.01.1.2TypeScript support: included

A mixin to implement custom input validator

api-components, arc-components, forms, validation, web-components

readme

ValidatorMixin

Use ValidatorMixin to implement a custom input/form validator. Element instances implementing this mixin will be registered for use in elements that implement ValidatableMixin.

Published on NPM

tests

Installation

npm i @anypoint-web-components/validator-mixin --save

Usage

Validator name

By default it takes lower case name of current HTML element. If this class is used outside custom elements environment then it uses static is property to get the name of the mixin.

static get is() {
 return 'my-validator';
}

Deregistering validator

When an instance of a validator is created it is being stored in a global validators cache controlled by the ValidatorStore module.

After registering the validator in the global store it is not tied to component's life cycle methods (it can extend an Object instead of HTMLElement). Also, usually validator stays in the document for the entire life cycle of the web app. But if you need to un-register the validator then call instance.unregister() function and detach this element from the DOM or remove references to the object so it can be GC'd.

You can also do this by calling ValidatorStore.unregister(instance).

In a LitElement

import { LitElement } from 'lit-element';
import { ValidatorMixin } from '@anypoint-web-components/validator-mixin';

class CatsOnly extends ValidatorMixin(LitElement) {
  validateObject(obj) {
    return !Object.keys(obj).some((key) => obj[key].match(/^(c|ca|cat|cats)?$/) === null);
  }

  validateArray(value) {
    return !value.some((value) => value.match(/^(c|ca|cat|cats)?$/) === null);
  }

  validate(values) {
    if (Array.isArray(values)) {
      return this.validateArray(values);
    }
    if (typeof values === 'object') {
      return this.validateObject(values);
    }
    return values.match(/^(c|ca|cat|cats)?$/) !== null;
  }
}
window.customElements.define('cats-only', CatsOnly);

Development

git clone https://github.com/anypoint-web-components/validator-mixin
cd validator-mixin
npm install

Testing

npm test

Demo

npm start

changelog

1.1.0 (2020-05-14)

Build

Update

  • upgrading project to latest ARC standard 2428416 by Pawel
  • upgrading dependencies to fix audit errors dfdf982 by Pawel Psztyc

Features

  • publishing stable element 8193b71 by Pawel

Bug Fixes

  • upgrading dev deps to fix npm audit errors fd156c4 by Pawel
  • fixing formatting 75f9dbf by Pawel

Refactor

  • removing use of the IronMeta and replaced with ValidatorStore 0c8c7e9 by Pawel

Reverting Changes

  • restoring version ee4794b by Pawel

Testing

  • adding sauce labs keys dd3a5a7 by Pawel

1.1.1 (2020-05-14)

Build

Update

  • [ci skip] automated merge master->stage. syncing main branches 4edd87f by Ci agent
  • upgrading project to latest ARC standard 2428416 by Pawel
  • upgrading dependencies to fix audit errors dfdf982 by Pawel Psztyc

Features

  • publishing stable element 8193b71 by Pawel

Bug Fixes

  • fixing type declaration for validate() function b7cefb3 by Pawel
  • upgrading dev deps to fix npm audit errors fd156c4 by Pawel
  • fixing formatting 75f9dbf by Pawel

Refactor

  • removing use of the IronMeta and replaced with ValidatorStore 0c8c7e9 by Pawel

Reverting Changes

  • restoring version ee4794b by Pawel

Testing

  • adding sauce labs keys dd3a5a7 by Pawel