@anypoint-web-components/validator-mixin

中文文档
1.1.2发布于 2021/03/03Apache-2.0ESM + CJS近 30 天下载 3147

A mixin to implement custom input validator

原始使用文档
文档目录8 节
  1. Installation
  2. Usage
  3. Validator name
  4. Deregistering validator
  5. In a LitElement
  6. Development
  7. Testing
  8. Demo

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

版本与发布记录

@anypoint-web-components/validator-mixin 共发布 4 个版本,首次发布于 2019/06/24;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.2latest2021/03/035 年前
1.1.12020/05/156 年前
1.1.02020/05/156 年前
1.0.02019/06/247 年前

关于 @anypoint-web-components/validator-mixin 的常见问题

@anypoint-web-components/validator-mixin 是什么?

A mixin to implement custom input validator

如何安装 @anypoint-web-components/validator-mixin?

在项目目录运行 npm install @anypoint-web-components/validator-mixin,或使用 pnpm add / yarn add / bun add。

@anypoint-web-components/validator-mixin 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/anypoint-web-components__validator-mixin 是否存在。

@anypoint-web-components/validator-mixin 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@anypoint-web-components/validator-mixin 可以商用吗?

@anypoint-web-components/validator-mixin 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@anypoint-web-components/validator-mixin 的下载量有多少?

根据 npm 官方下载统计,@anypoint-web-components/validator-mixin 在 2026/10/06 – 2026/11/04 这 30 天内被下载 3,147 次,日均约 105 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@anypoint-web-components/validator-mixin 还在维护吗?

最新版本 1.1.2 发布于 2021/03/03(5 年前);至今共发布 4 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/06/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。