How react-component/async-validator works
Project type and purpose
• This is an open-source npm library (“@rc-component/async-validator”) for declarative, asynchronous form or object validation in JavaScript/TypeScript. It provides a Schema API that takes a descriptor (a JSON-like specification of fields and rules) and validates arbitrary JS objects, returning results via callbacks or Promises.
File structure
• Top-level files:
• package.json, tsconfig.json, build config (fatherrc.js), lint and formatting configs, CI workflows.
• README.md documents installation, API, rule types, usage patterns.
• src/ – actual library source:
• index.ts: exports the Schema class and orchestrates validation.
• interface.ts, messages.ts: type definitions and default error messages.
• util.ts: shared utility functions (e.g. formatting, deep merging).
• rule/ – definitions of rule metadata/enums (type names, patterns, ranges, whitespace, URL, etc.).
• validator/ – implementations of each validation strategy (string, number, boolean, object, array, date, enum, pattern, required, asyncValidator, etc.).
• tests/ – unit tests for each validator, rule combinations, async behavior, deep rules, messages, promise use.
README for architectural insights
• The README emphasizes:
• A “Schema” entry point.
• Descriptor → schema → validate(source, options, callback) or Promise.
• Built-in rules (type, required, pattern, range, whitespace, enum, deep rules via fields/defaultField, transform).
• Custom synchronous validator vs. asyncValidator vs. promise.
• Message customization / i18n via options.messages.
a. Main components:
1. Consumer / Client Code – calls new Schema(descriptor) and invoke validate().
2. Schema (src/index.ts) – orchestrator.
3. Descriptor Model – JSON/object that describes fields + rules.
4. Rule Parser (src/rule/) – maps descriptor entries to internal Rule objects, normalizes arrays vs. single rule, deep rule expansion.
5. Validator Strategy Modules (src/validator/) – one module per rule type implementing synchronous or asynchronous checks.
6. Util & Messages (src/util.ts + src/messages.ts) – shared functions (e.g. message formatting, deep merge) and default error messages.
7. Build & Packaging – father build config, output to dist/, type declarations, dumi for docs.
8. CI/CD – GitHub Actions running lint, tests, coverage.
b. Relationships and interactions:
• Client invokes Schema.validate() → Schema uses Rule Parser to normalize the descriptor.
• For each field rule: Schema selects appropriate Validator module from the “validator” directory per rule.type or custom validator function.
• Validator modules use util functions and messages; may return synchronously or return a Promise or use callback.
• Schema collects all results, aggregates errors/fields, resolves or rejects the returned Promise (or invokes callback).
• Tests invoke the same Schema.validate() path with various descriptors to verify each rule module.
c. Architectural patterns & design principles:
• Strategy pattern: each validator type is encapsulated in its own module, pluggable via “type” name.
• Chain-of-Responsibility: multiple rules per field are processed in order, optionally stopping on first error.
• Extension points: custom validator() and asyncValidator() hooks.
• Separation of concerns: parsing vs. validation vs. messaging.
• Promise/Callback duality: supports both async models.
d. Key technologies and frameworks:
• TypeScript for source + types.
• Node.js module targeting CommonJS and ESModule via father (build tool).
• Jest or Mocha (implied by .spec.ts) for testing.
• Dumi for documentation.
• GitHub Actions for CI.