@carecard/validate

中文文档
3.45.0发布于 2026/11/03ISCTypeScriptCommonJS构建来源已验证近 30 天下载 3527

Validate data

原始使用文档
文档目录4 节
  1. Explicit schemas
  2. Configured password validation
  3. Format predicates and normalization
  4. Validation

@carecard/validate

Reusable format predicates, canonical input normalization, and caller-configured validation. Domain field schemas, allowed roles, workflow states, and password policy belong to the service or application that owns them. This package never interprets a property name or supplies a business-policy default.

Explicit schemas

createValueParser(predicate, transform?) adapts a boolean predicate and optional value transformation. A custom parser returns { isValid: true, value } or { isValid: false }. Predicates must return exactly true to accept a value.

validateSchemaProperties(input, schema) returns only valid own fields named in the caller schema. It copies and trims JSON keys and string edges before parsing, omits unknown or rejected fields, and does not mutate the input.

validateSchemaWhitelistProperties(input, requiredPaths, options) requires an explicit options.schema with one parser for every full selected path. Required missing/invalid fields and supplied invalid optional fields produce BAD_INPUT. Missing optional fields are omitted. Array leaves are parsed element by element. Missing or invalid schema configuration raises TypeError.

const {
  createValueParser,
  isInteger,
  validateSchemaWhitelistProperties,
} = require('@carecard/validate');

const output = await validateSchemaWhitelistProperties(
  { measurement: { count: 3 }, ignored: true },
  ['measurement.count'],
  { schema: { 'measurement.count': createValueParser(isInteger) } },
);
// { measurement: { count: 3 } }

Options also support optionalProperties, convertToSnakeCase, flattenOutput, and flattenKeyStyle (path by default or leaf). Snake/camel path variants resolve as before; mixed casing within a segment is rejected. Selection is bounded to five path segments and 5,000 paths per call. Public constants are MAX_NESTING_DEPTH and MAX_KEYS_PER_CALL.

Flattened leaf collisions favor the shallower value, then the first traversal. Reserved-looking keys remain inert own data, and inherited paths are ignored. Ambiguous normalized keys, cyclic JSON input, and accessors are rejected.

Configured password validation

validatePasswordAgainstPolicy(value, { minimumLength, maximumLength, isBlocked? }) requires caller-selected lengths. It trims string edges, normalizes NFC, and counts Unicode code points. Successful results contain the canonical value. Failure reasons are invalid_type, invalid_unicode, too_short, too_long, and blocked. Invalid policy configuration raises TypeError.

isCommonPassword(value) exposes a generic weak-password corpus lookup. It has no automatic effect; callers decide whether to use it as isBlocked.

Format predicates and normalization

Direct predicates cover integers, UUIDs, dates/timestamps, email, HTTP URLs, MIME types, safe JSON, Unicode text, names, filename metadata, phones, and other reusable formats. They return booleans and do not assign domain meaning to data. isInStringArray(values, input) checks a caller-supplied case-insensitive list. The existing validate namespace contains the same generic format predicates.

normalizeInputWhitespace(input) copies JSON records and arrays, trims own keys and string edges, preserves internal whitespace and opaque/binary values, and rejects key collisions, accessors, and cycles. Raw format predicates retain their character constraints. Filename callers may explicitly normalize accepted metadata with NFKC and trim; display names remain unchanged.

The public root and focused imports stay safe for browser bundles. TypeScript contracts are supplied in index.d.ts.

Validation

Run npm run test:All, npm run lint, and npm run format:check. Runtime coverage remains 100% for statements, branches, functions, and lines. Tests exercise public behavior with caller-owned example schemas; business policy is verified in its owning repository.

Non-negotiable test order invariance rule: Every test must pass independently of which tests run before or after it, and the suite must pass in every execution order. Each test must establish the state it needs, isolate mutable state, and clean up state it owns; it must never rely on another test's setup, mutations, or cleanup. Default test, CI, and Husky commands must use the test framework's ordinary ordering and must not force randomized ordering. Random-order execution is an explicit diagnostic only, and every failure it exposes must be fixed at the root cause.

Non-negotiable root-cause solution rule: Always identify and solve the verified root cause, use the stronger solution, and deliver a correct, durable, production-quality result. Never treat a temporary workaround, resource increase, retry, suppression, bypass, or symptom-only patch as completion. Validate the root-cause fix against the real failing workflow and prove the end state.

Non-negotiable TDD rule: Always write the failing test first, run it to confirm it fails for the intended reason, then implement the code and rerun the test until it passes. Test Driven Development is required for all coding work and must not be skipped. For documentation- or skill-only edits, run the relevant focused non-test validation before changing the prose; do not add automated tests that inspect prose, files, or repository structure.

Non-negotiable repository isolation rule: Every repository must run its Husky hooks and tests using only files, code, fixtures, dependencies, and services contained within that repository. Tests and Husky scripts must not import, require, read, execute, or otherwise depend on sibling repositories or paths outside the repository root. app-e2e-tests is the only exception because cross-repository end-to-end testing is its explicit responsibility.

Non-negotiable error and warning rule: Never suppress, silence, hide, downgrade, filter, ignore, skip, or bypass errors or warnings from code, tests, tools, compilers, linters, or validation. Fix the root cause, then rerun the affected check and require a clean result. Expected error-path tests may assert errors, but must not conceal unexpected failures.

Non-negotiable TypeScript type rule: Never use the TypeScript type any; always use specific domain types, generics, existing project types, or unknown with explicit narrowing in all TypeScript-family files (.ts, .tsx, .mts, .cts, and .d.ts).

Non-negotiable code organization rule: Functions with the same or equivalent behavior must use the same or clearly corresponding descriptive names across CareCard repositories, and equivalent functionality must live in files with the same names within each repository's established architecture. No backward compatibility names, aliases, or duplicate locations are allowed.

版本与发布记录

@carecard/validate 共发布 87 个版本,首次发布于 2025/12/23;过去 12 个月发布了 87 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.45.0latest2026/11/036 天前
3.44.02026/10/1327 天前
3.43.02026/10/1327 天前
3.42.02026/10/1327 天前
3.41.02026/10/1227 天前
3.40.02026/10/1228 天前

关于 @carecard/validate 的常见问题

@carecard/validate 是什么?

Validate data

如何安装 @carecard/validate?

在项目目录运行 npm install @carecard/validate,或使用 pnpm add / yarn add / bun add。

@carecard/validate 支持 TypeScript 吗?

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

@carecard/validate 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@carecard/validate 需要什么版本的 Node.js?

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

@carecard/validate 可以商用吗?

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

@carecard/validate 的下载量有多少?

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

@carecard/validate 还在维护吗?

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