diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 1fe6f50..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1,8 +0,0 @@ -const base = require('@umijs/fabric/dist/eslint'); - -module.exports = { - ...base, - rules: { - ...base.rules, - }, -}; diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index deb72e8..758659a 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1,15 +1,2 @@ -# These are supported funding model platforms - github: ant-design -patreon: # Replace with a single Patreon username -open_collective: # Replace with a single Open Collective username -ko_fi: # Replace with a single Ko-fi username -tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel -community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry -liberapay: # Replace with a single Liberapay username -issuehunt: # Replace with a single IssueHunt username -lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry -polar: # Replace with a single Polar username -buy_me_a_coffee: # Replace with a single Buy Me a Coffee username -thanks_dev: # Replace with a single thanks.dev username -custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] +open_collective: ant-design diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 5f0889c..5e6c7fa 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -1,11 +1,27 @@ -# To get started with Dependabot version updates, you'll need to specify which -# package ecosystems to update and where the package manifests are located. -# Please see the documentation for all configuration options: -# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file - version: 2 updates: - - package-ecosystem: "npm" # See documentation for possible values - directory: "/" # Location of package manifests + - package-ecosystem: npm + directory: '/' + schedule: + interval: weekly + day: monday + time: '21:00' + timezone: Asia/Shanghai + open-pull-requests-limit: 10 + groups: + npm-dependencies: + patterns: + - '*' + + - package-ecosystem: github-actions + directory: '/' schedule: - interval: "weekly" + interval: weekly + day: monday + time: '21:00' + timezone: Asia/Shanghai + open-pull-requests-limit: 10 + groups: + github-actions: + patterns: + - '*' diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml deleted file mode 100644 index f860ff1..0000000 --- a/.github/workflows/ci.yml +++ /dev/null @@ -1,6 +0,0 @@ -name: ✅ test -on: [push, pull_request] -jobs: - test: - uses: react-component/rc-test/.github/workflows/test.yml@main - secrets: inherit diff --git a/.github/workflows/react-doctor.yml b/.github/workflows/react-doctor.yml new file mode 100644 index 0000000..29d6f76 --- /dev/null +++ b/.github/workflows/react-doctor.yml @@ -0,0 +1,22 @@ +name: React Doctor + +on: + pull_request: + push: + branches: [master] + +permissions: + contents: read + pull-requests: write + issues: write + statuses: write + +jobs: + react-doctor: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v7 + with: + fetch-depth: 0 + persist-credentials: false + - uses: millionco/react-doctor@0b4f4f4bd248a154e64eb508a48347f71154b3f3 diff --git a/.github/workflows/surge-preview.yml b/.github/workflows/surge-preview.yml new file mode 100644 index 0000000..960a646 --- /dev/null +++ b/.github/workflows/surge-preview.yml @@ -0,0 +1,52 @@ +name: Surge Preview + +on: + pull_request: + +permissions: + contents: read + pull-requests: write + checks: write + +jobs: + preview: + runs-on: ubuntu-latest + concurrency: + group: surge-preview-${{ github.event.pull_request.number }} + cancel-in-progress: true + steps: + - uses: actions/checkout@v7 + with: + persist-credentials: false + - name: Check Surge token + id: surge-token + env: + SURGE_TOKEN: ${{ secrets.SURGE_TOKEN }} + run: | + if [ -n "$SURGE_TOKEN" ]; then + echo "enabled=true" >> "$GITHUB_OUTPUT" + else + echo "enabled=false" >> "$GITHUB_OUTPUT" + fi + - name: Setup utoo + if: ${{ steps.surge-token.outputs.enabled == 'true' }} + uses: utooland/setup-utoo@v1 + + - name: Build preview + if: ${{ steps.surge-token.outputs.enabled == 'true' }} + run: | + ut install + npm run build + - uses: afc163/surge-preview@bf90a5a86111f6311ca42f0a5a0f80fb0fb03cec + if: ${{ steps.surge-token.outputs.enabled == 'true' }} + env: + SURGE_TOKEN: ${{ secrets.SURGE_TOKEN }} + with: + surge_token: ${{ env.SURGE_TOKEN }} + github_token: ${{ secrets.GITHUB_TOKEN }} + dist: .docs-dist + failOnError: false + setCommitStatus: false + - name: Skip Surge preview + if: ${{ steps.surge-token.outputs.enabled != 'true' }} + run: echo "SURGE_TOKEN is not configured; skip Surge preview." diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..10c0ebb --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,16 @@ +name: ✅ test + +on: + push: + branches: [master] + pull_request: + branches: [master] + +permissions: + contents: read + +jobs: + test: + uses: react-component/rc-test/.github/workflows/test-utoo.yml@main + secrets: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} diff --git a/README.md b/README.md index ad1f295..78231d6 100644 --- a/README.md +++ b/README.md @@ -1,87 +1,80 @@ -# @rc-component/async-validator - -[![NPM version][npm-image]][npm-url] -[![npm download][download-image]][download-url] -[![build status][github-actions-image]][github-actions-url] -[![Codecov][codecov-image]][codecov-url] -[![dumi][dumi-image]][dumi-url] - -[npm-image]: http://img.shields.io/npm/v/@rc-component/async-validator.svg?style=flat-square -[npm-url]: http://npmjs.org/package/@rc-component/async-validator -[travis-image]: https://img.shields.io/travis/react-component/async-validator/master?style=flat-square -[travis-url]: https://travis-ci.com/react-component/async-validator -[github-actions-image]: https://github.com/react-component/async-validator/actions/workflows/ci.yml/badge.svg -[github-actions-url]: https://github.com/react-component/async-validator/actions/workflows/ci.yml -[codecov-image]: https://img.shields.io/codecov/c/github/react-component/async-validator/master.svg?style=flat-square -[codecov-url]: https://app.codecov.io/gh/react-component/async-validator -[david-url]: https://david-dm.org/react-component/async-validator -[david-image]: https://david-dm.org/react-component/async-validator/status.svg?style=flat-square -[david-dev-url]: https://david-dm.org/react-component/async-validator?type=dev -[david-dev-image]: https://david-dm.org/react-component/async-validator/dev-status.svg?style=flat-square -[download-image]: https://img.shields.io/npm/dm/@rc-component/async-validator.svg?style=flat-square -[download-url]: https://npmjs.org/package/@rc-component/async-validator -[bundlephobia-url]: https://bundlephobia.com/package/@rc-component/async-validator -[bundlephobia-image]: https://badgen.net/bundlephobia/minzip/@rc-component/async-validator -[dumi-url]: https://github.com/umijs/dumi -[dumi-image]: https://img.shields.io/badge/docs%20by-dumi-blue?style=flat-square - -Validate form asynchronous. A variation of https://github.com/freeformsystems/async-validate +
+

@rc-component/async-validator

+

Ant Design Part of the Ant Design ecosystem.

+

✅ Async validation engine for schema-based form rules.

+ +

+ NPM version + npm downloads + build status + Codecov + bundle size + dumi +

+
+ +

English | 简体中文

+ +## Highlights + +| Area | Support | +| ------- | ---------------------------------------------------- | +| Purpose | Async validation engine for schema-based form rules. | +| Package | `@rc-component/async-validator` | +| Release | `@rc-component/np` / `rc-np` | ## Install ```bash -npm i @rc-component/async-validator +npm install @rc-component/async-validator ``` ## Usage -Basic usage involves defining a descriptor, assigning it to a schema and passing the object to be validated and a callback function to the `validate` method of the schema: - -```js +```tsx | pure import Schema from '@rc-component/async-validator'; -const descriptor = { - name: { - type: 'string', - required: true, - validator: (rule, value) => value === 'muji', - }, - age: { - type: 'number', - asyncValidator: (rule, value) => { - return new Promise((resolve, reject) => { - if (value < 18) { - reject('too young'); // reject with error message - } else { - resolve(); - } - }); - }, - }, -}; -const validator = new Schema(descriptor); -validator.validate({ name: 'muji' }, (errors, fields) => { - if (errors) { - // validation failed, errors is an array of all errors - // fields is an object keyed by field name with an array of - // errors per field - return handleErrors(errors, fields); - } - // validation passed + +const validator = new Schema({ + name: { type: 'string', required: true }, }); -// PROMISE USAGE -validator - .validate({ name: 'muji', age: 16 }) - .then(() => { - // validation passed or without error message - }) - .catch(({ errors, fields }) => { - return handleErrors(errors, fields); - }); +validator.validate({ name: 'Ant Design' }).then(() => { + // validation passed +}); ``` ## API +| API | Description | +| --------------------------------------- | ------------------------------------------ | +| `new Schema(descriptor)` | Create a validator from a rule descriptor. | +| `validate(source, options?, callback?)` | Validate source data and return a Promise. | +| `messages` | Customize validation messages. | + +## Development + +```bash +npm install +npm start +npm test +npm run coverage +npm run lint +npm run tsc +npm run compile +``` + +The dumi site runs at `http://localhost:8000`. + +## Release + +```bash +npm run prepublishOnly +``` + +The release flow is handled by `@rc-component/np` through the `rc-np` command when the package uses the shared release flow. + +## Detailed API + ### Validate ```js @@ -478,4 +471,4 @@ Open coverage/ dir ## License -Everything is [MIT](https://en.wikipedia.org/wiki/MIT_License). +@rc-component/async-validator is released under the [MIT](./LICENSE.md) license. diff --git a/README.zh-CN.md b/README.zh-CN.md new file mode 100644 index 0000000..466afdf --- /dev/null +++ b/README.zh-CN.md @@ -0,0 +1,78 @@ +
+

@rc-component/async-validator

+

Ant Design Ant Design 生态的一部分。

+

✅ 基于规则描述的异步表单校验引擎。

+ +

+ NPM version + npm downloads + build status + Codecov + bundle size + dumi +

+
+ +

English | 简体中文

+ +## 亮点 + +| 方向 | 支持 | +| ---- | -------------------------------- | +| 定位 | 基于规则描述的异步表单校验引擎。 | +| 包名 | `@rc-component/async-validator` | +| 发布 | `@rc-component/np` / `rc-np` | + +## 安装 + +```bash +npm install @rc-component/async-validator +``` + +## 用法 + +```tsx | pure +import Schema from '@rc-component/async-validator'; + +const validator = new Schema({ + name: { type: 'string', required: true }, +}); + +validator.validate({ name: 'Ant Design' }).then(() => { + // validation passed +}); +``` + +## API + +| 名称 | 说明 | +| --------------------------------------- | -------------------------- | +| `new Schema(descriptor)` | 根据规则描述创建校验器。 | +| `validate(source, options?, callback?)` | 校验源数据并返回 Promise。 | +| `messages` | 自定义校验提示。 | + +## 本地开发 + +```bash +npm install +npm start +npm test +npm run coverage +npm run lint +npm run tsc +npm run compile +``` + +本地 dumi 站点默认运行在 `http://localhost:8000`. + +## 发布 + +```bash +npm run prepublishOnly +``` + +发布流程通过 `@rc-component/np` 提供的 `rc-np` 命令处理。 + +## 许可证 + +@rc-component/async-validator 基于 [MIT](./LICENSE.md) 协议发布。 diff --git a/eslint.config.mjs b/eslint.config.mjs new file mode 100644 index 0000000..40f08ec --- /dev/null +++ b/eslint.config.mjs @@ -0,0 +1,115 @@ +import js from '@eslint/js'; +import { defineConfig } from 'eslint/config'; +import { dirname } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import prettier from 'eslint-config-prettier'; +import jest from 'eslint-plugin-jest'; +import react from 'eslint-plugin-react'; +import reactHooks from 'eslint-plugin-react-hooks'; +import globals from 'globals'; +import tseslint from 'typescript-eslint'; + +const tsconfigRootDir = dirname(fileURLToPath(import.meta.url)); + +export default defineConfig([ + { + plugins: { + '@typescript-eslint': tseslint.plugin, + }, + }, + { + linterOptions: { + reportUnusedDisableDirectives: 'warn', + }, + }, + { + ignores: [ + 'node_modules/', + 'coverage/', + 'es/', + 'lib/', + 'dist/', + 'docs-dist/', + '.docs-dist/', + '.dumi/', + '.doc/', + '.vercel/', + ], + }, + { + files: ['**/*.{js,jsx,ts,tsx}'], + extends: [ + js.configs.recommended, + react.configs.flat.recommended, + react.configs.flat['jsx-runtime'], + prettier, + ], + plugins: { + 'react-hooks': reactHooks, + }, + languageOptions: { + globals: { + ...globals.browser, + ...globals.node, + }, + }, + settings: { + react: { + version: 'detect', + }, + }, + rules: { + 'no-async-promise-executor': 'off', + 'no-empty-pattern': 'off', + 'no-irregular-whitespace': 'off', + 'no-prototype-builtins': 'off', + 'no-useless-escape': 'off', + 'no-extra-boolean-cast': 'off', + 'no-undef': 'off', + 'no-unused-vars': 'off', + 'react/no-find-dom-node': 'off', + 'react/display-name': 'off', + 'react/no-unknown-property': 'off', + 'react/prop-types': 'off', + 'react-hooks/exhaustive-deps': 'warn', + 'react-hooks/rules-of-hooks': 'error', + }, + }, + { + files: ['**/*.{ts,tsx}'], + extends: [...tseslint.configs.recommended], + rules: { + '@typescript-eslint/ban-ts-comment': 'off', + '@typescript-eslint/no-empty-object-type': 'off', + '@typescript-eslint/no-explicit-any': 'off', + '@typescript-eslint/no-unsafe-function-type': 'off', + '@typescript-eslint/no-unnecessary-type-constraint': 'off', + '@typescript-eslint/no-unused-vars': 'off', + }, + }, + { + files: ['src/**/*.{ts,tsx}'], + languageOptions: { + parserOptions: { + projectService: true, + tsconfigRootDir, + }, + }, + }, + { + files: ['tests/**/*.{js,jsx,ts,tsx}', '**/*.{test,spec}.{js,jsx,ts,tsx}'], + extends: [jest.configs['flat/recommended']], + rules: { + 'jest/no-disabled-tests': 'off', + 'jest/no-done-callback': 'off', + 'jest/no-identical-title': 'off', + 'jest/expect-expect': 'off', + 'jest/no-alias-methods': 'off', + 'jest/no-conditional-expect': 'off', + 'jest/no-export': 'off', + 'jest/no-standalone-expect': 'off', + 'jest/valid-expect': 'off', + 'jest/valid-title': 'off', + }, + }, +]); diff --git a/package.json b/package.json index 7c03918..c8b9677 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@rc-component/async-validator", "version": "6.0.0", - "description": "Validate form asynchronous", + "description": "✅ Async validation engine for schema-based form rules.", "keywords": [ "async-validator", "form" @@ -28,14 +28,14 @@ "docs:deploy": "gh-pages -d .docs-dist", "gh-pages": "npm run docs:build && npm run docs:deploy", "lint": "eslint src --ext .tsx,.ts,.jsx,.js", - "now-build": "npm run docs:build", "prepublishOnly": "npm run compile && rc-np", "prettier": "prettier . --write", "postpublish": "npm run gh-pages", "start": "dumi dev", "test": "rc-test", "coverage": "rc-test --coverage", - "tsc": "tsc --noEmit" + "tsc": "tsc --noEmit", + "build": "dumi build" }, "pre-commit": [ "lint-staged" @@ -46,25 +46,31 @@ ] }, "dependencies": { - "@babel/runtime": "^7.24.4" + "@babel/runtime": "^7.29.7" }, "devDependencies": { + "@eslint/js": "^9.39.4", "@rc-component/father-plugin": "^2.2.0", - "@rc-component/np": "^1.0.3", - "@types/jest": "^29.5.12", - "@types/react": "18.x", - "@types/react-dom": "18.x", - "@umijs/fabric": "^4.0.1", - "dumi": "^2.2.17", - "eslint": "^8.0.0", - "father": "^4.4.0", + "@rc-component/np": "^1.0.4", + "@types/jest": "^30.0.0", + "@types/react": "^19.2.17", + "@types/react-dom": "^19.2.3", + "dumi": "^2.4.38", + "eslint": "^9.39.4", + "eslint-config-prettier": "^10.1.8", + "eslint-plugin-jest": "^29.15.4", + "eslint-plugin-react": "^7.37.5", + "eslint-plugin-react-hooks": "^7.1.1", + "father": "^4.6.24", "gh-pages": "^6.1.1", - "lint-staged": "^15.2.2", - "prettier": "^3.2.5", - "rc-test": "^7.0.15", - "react": "^18.2.0", - "react-dom": "^18.2.0", - "typescript": "5.x" + "globals": "^17.7.0", + "lint-staged": "^17.0.8", + "prettier": "^3.9.4", + "rc-test": "^7.1.3", + "react": "^19.2.7", + "react-dom": "^19.2.7", + "typescript": "^6.0.3", + "typescript-eslint": "^8.62.1" }, "engines": { "node": ">=14.x" diff --git a/src/index.ts b/src/index.ts index 57e2e17..9738645 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,7 +19,7 @@ import { messages as defaultMessages, newMessages } from './messages'; import { asyncMap, complementError, convertFieldsError, deepMerge, format, warning } from './util'; import validators from './validator/index'; -export * from './interface'; +export type * from './interface'; /** * Encapsulates a validation schema. diff --git a/tsconfig.json b/tsconfig.json index 7f6ba30..1eb98ca 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,16 +1,18 @@ { "compilerOptions": { "target": "esnext", - "moduleResolution": "node", - "baseUrl": "./", + "strict": false, + "types": ["node", "jest"], + "moduleResolution": "bundler", "jsx": "react", "declaration": true, "skipLibCheck": true, "esModuleInterop": true, "paths": { - "@/*": ["src/*"], - "@@/*": ["src/.umi/*"], - "@rc-component/async-validator": ["src/index.ts"] + "@/*": ["./src/*"], + "@@/*": ["./src/.umi/*"], + "@rc-component/async-validator": ["./src/index.ts"] } - } + }, + "include": ["**/*.ts", "**/*.tsx", ".dumirc.ts"] } diff --git a/vercel.json b/vercel.json new file mode 100644 index 0000000..9a78ae0 --- /dev/null +++ b/vercel.json @@ -0,0 +1,6 @@ +{ + "framework": "umijs", + "installCommand": "npm install", + "buildCommand": "npm run build", + "outputDirectory": ".docs-dist" +}