@flatfile/plugin-validate-string

中文文档
0.3.1发布于 2025/01/10ISCTypeScriptESM + CJS近 30 天下载 2798

A Flatfile plugin for string configuration and validation

原始使用文档
文档目录5 节
  1. Features
  2. Installation
  3. Example Usage
  4. Configuration
  5. Behavior

The @flatfile/plugin-validate-string plugin for string configuration and validation. This plugin combines multiple string validations in a single configuration, including regex pattern matching, length validation, case sensitivity, trimming options, and custom transformations.

Event Type: listener.on('commit:created')

Features

  • Regular expression pattern matching
  • Length validation (min, max, exact)
  • Case type enforcement (lowercase, uppercase, titlecase)
  • Whitespace trimming (leading, trailing)
  • Custom transformation functions
  • Configurable error messages
  • Common regex patterns for email, phone, and URL validation
  • Empty string handling

Installation

To install the plugin, use npm:

npm install @flatfile/plugin-validate-string

Example Usage

import { FlatfileListener } from '@flatfile/listener';
import { validateString } from '@flatfile/plugin-validate-string';

const listener = new FlatfileListener();

const stringConfig = {
  fields: ['name'],
  minLength: 2,
  maxLength: 50,
  caseType: 'titlecase',
  errorMessages: {
    length: 'Name must be between 2 and 50 characters',
    case: 'Name must be in Title Case',
  },
  
};

listener.use(validateString(stringConfig));

Pattern usage:

const config = {
  fields: ['email'],
  pattern: 'email' // Uses predefined email pattern
};

// Or with a custom pattern:
const customConfig = {
  fields: ['customField'],
  pattern: /^[A-Z]{3}-\d{2}$/ // Custom pattern for format like 'ABC-12'
};

Configuration

The validateString accepts a StringValidationConfig object with the following properties:

  • fields: string[] - Fields to validate
  • sheetSlug: string - Sheet slug to validate (defaults to '**' all sheets)
  • pattern: RegExp - A regular expression pattern to match against
  • pattern: keyof typeof commonRegexPatterns | RegExp - A regular expression pattern to match against. You can use one of the predefined patterns ('email', 'phone', 'url') or provide a custom RegExp. The predefined patterns are:
    • email: Validates email addresses
    • phone: Validates phone numbers (10-14 digits, optional '+' prefix)
    • url: Validates URLs (with or without protocol)
  • minLength: number - Minimum length of the string
  • maxLength: number - Maximum length of the string
  • exactLength: number - Exact required length of the string
  • caseType: 'lowercase' | 'uppercase' | 'titlecase' - Enforces specific case type
  • trim: { leading?: boolean, trailing?: boolean } - Trims whitespace
  • emptyStringAllowed: boolean - Whether empty strings are allowed
  • errorMessages: Object with custom error messages for different validations

Behavior

The plugin processes each record in the Flatfile import, applying the configured validations to the specified fields. If a validation fails, an error is added to the record for that field. If a custom transformation is specified and all validations pass, the transformed value is set for the field.

The plugin uses the recordHook to process individual records, allowing for efficient and flexible validation and transformation of string fields during the import process.

版本与发布记录

@flatfile/plugin-validate-string 共发布 5 个版本,首次发布于 2024/10/10;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.3.1latest2025/01/101.8 年前
0.3.02024/11/082 年前
0.2.02024/10/302 年前
0.1.02024/10/112 年前
0.0.02024/10/102 年前

关于 @flatfile/plugin-validate-string 的常见问题

@flatfile/plugin-validate-string 是什么?

A Flatfile plugin for string configuration and validation

如何安装 @flatfile/plugin-validate-string?

在项目目录运行 npm install @flatfile/plugin-validate-string,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@flatfile/listener(^1.1.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@flatfile/plugin-validate-string 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@flatfile/plugin-validate-string 支持 ESM 还是 CommonJS?

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

@flatfile/plugin-validate-string 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@flatfile/plugin-validate-string 可以商用吗?

@flatfile/plugin-validate-string 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@flatfile/plugin-validate-string 的下载量有多少?

根据 npm 官方下载统计,@flatfile/plugin-validate-string 在 2026/10/15 – 2026/11/13 这 30 天内被下载 2,798 次,日均约 93 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@flatfile/plugin-validate-string 还在维护吗?

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