Skip to content

Commit

Permalink
chore: improve for pr review
Browse files Browse the repository at this point in the history
  • Loading branch information
YSMJ1994 committed Jan 31, 2024
1 parent 432894f commit 272ff21
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 27 deletions.
44 changes: 20 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from './util';
import defaultMessages from './messages';
import { getValidationMethod } from './validator';
import {
import type {
Rule,
RuleMap,
SerializedRule,
Expand All @@ -19,13 +19,15 @@ import {
ValidatorCallback,
ValidatorCallbackErrors,
NormalizedValidateError,
ValidationNoErrorResult,
ValidationErrorResult,
} from './types';

function noop() {}
/**
* @param source - {name: value, name2: value2}
* @param rules - {name: [rule1, rule2]}
* @returns - {name:[{value,rule1},{value, rule2}]}
* @param source - 校验对象,如:{name: value, name2: value2}
* @param rules - 对象内 key 的校验规则集合,如:{name: [rule1, rule2]}
* @returns 标准的校验项集合,如:{name:[{value,rule1},{value, rule2}]}
*/
function serializeRules(source: ValidateSource, rules: RuleMap) {
// serialize rules
Expand Down Expand Up @@ -94,23 +96,23 @@ class Schema {
}

/**
*
* Validate and use `Promise` to receive results
* @param source - map of field names and values to use in validation
* @param callback - OPTIONAL - callback to run after all
* @returns
* - { null } - if using callbacks
* - { Promise }
* - { errors: null } - if no rules or no errors
* - { errors: Array, fields: Object } - errors from validation and fields that have errors
* @returns Validation results
*/
validate(
source: ValidateSource
): ReturnType<(typeof this)['validatePromise']>;
): Promise<ValidationNoErrorResult | ValidationErrorResult>;
/**
* Validate and use `callback` to receive results
* @param source - map of field names and values to use in validation
* @param callback - callback to run after all
*/
validate(source: ValidateSource, callback: ValidateCallback): void;
validate(
source: ValidateSource,
callback?: ValidateCallback
): Promise<unknown> | void {
): Promise<ValidationNoErrorResult | ValidationErrorResult> | void {
if (!callback) {
return this.validatePromise(source);
}
Expand Down Expand Up @@ -213,19 +215,13 @@ class Schema {
}

/**
*
* Validate and use `Promise` to receive results
* @param source - map of field names and values to use in validation
* @returns
* - { errors: null } if no rules or no errors
* - { errors: Array, fields: Object } - errors from validation and fields that have errors
* @returns Validation results
*/
async validatePromise(source: ValidateSource): Promise<
| { errors: null; fields?: undefined | null }
| {
errors: NormalizedValidateError[];
fields: Record<string, NormalizedValidateError[]>;
}
> {
async validatePromise(
source: ValidateSource
): Promise<ValidationNoErrorResult | ValidationErrorResult> {
if (!this._rules || Object.keys(this._rules).length === 0) {
return { errors: null };
}
Expand Down
2 changes: 0 additions & 2 deletions src/rules/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ const pattern = {
};

/**
* @params {string} idcode
*
* 函数参数必须是字符串,因为二代身份证号码是十八位,而在 javascript 中,十八位的数值会超出计算范围,造成不精确的结果,
* 导致最后两位和计算的值不一致,从而该函数出现错误(详情查看 javascript 的数值范围)。
* 为了避免这一误差,idcode 必须是字符串
Expand Down
9 changes: 9 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,12 @@ export type ErrorCallback = (
export type InnerValidator = (
rule: ValidationItem
) => NormalizedValidateError[] | Promise<NormalizedValidateError[]>;

export type ValidationNoErrorResult = {
errors: null;
fields?: undefined | null;
};
export type ValidationErrorResult = {
errors: NormalizedValidateError[];
fields: Record<string, NormalizedValidateError[]>;
};
2 changes: 1 addition & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {
import type {
NormalizedValidateError,
SerializedRule,
ValidateError,
Expand Down

0 comments on commit 272ff21

Please sign in to comment.