schema-typed
Advanced tools
Comparing version 2.0.0-alpha.2 to 2.0.0-alpha.3
@@ -1,3 +0,4 @@ | ||
# 2.0.0 | ||
# 2.0.0 next | ||
- feat(locales): add default error messages for all checks ([#27](https://github.com/rsuite/schema-typed/issues/27)) ([03e21d7](https://github.com/rsuite/schema-typed/commit/03e21d77e9a6e0cd4fddcb1adfe8c485025f246b)) | ||
- refactor: refactor the project through typescript. | ||
@@ -4,0 +5,0 @@ - feat(MixedType): Added support for `when` method on all types |
import { MixedType } from './MixedType'; | ||
import { CheckType } from './types'; | ||
export declare class ArrayType<DataType = any, ErrorMsgType = string> extends MixedType<any[], DataType, ErrorMsgType> { | ||
constructor(errorMessage?: ErrorMsgType); | ||
rangeLength(minLength: number, maxLength: number, errorMessage?: ErrorMsgType): this; | ||
minLength(minLength: number, errorMessage?: ErrorMsgType): this; | ||
maxLength(maxLength: number, errorMessage?: ErrorMsgType): this; | ||
unrepeatable(errorMessage?: ErrorMsgType): this; | ||
/** | ||
* @example | ||
* ArrayType().of(StringType().isOneOf(['数码','体育','游戏','旅途','其他'],'Can only be the value of a predefined option') | ||
*/ | ||
of(type: CheckType<any[], DataType, ErrorMsgType>, errorMessage?: ErrorMsgType): this; | ||
import { CheckType, ErrorMessageType } from './types'; | ||
import { ArrayTypeLocale } from './locales'; | ||
export declare class ArrayType<DataType = any, E = ErrorMessageType> extends MixedType<any[], DataType, E, ArrayTypeLocale> { | ||
constructor(errorMessage?: E | string); | ||
rangeLength(minLength: number, maxLength: number, errorMessage?: E | string): this; | ||
minLength(minLength: number, errorMessage?: E | string): this; | ||
maxLength(maxLength: number, errorMessage?: E | string): this; | ||
unrepeatable(errorMessage?: E | string): this; | ||
of(type: CheckType<any[], DataType, E>): this; | ||
} | ||
export default function getArrayType<DataType = any, ErrorMsgType = string>(errorMessage?: ErrorMsgType): ArrayType<DataType, ErrorMsgType>; | ||
export default function getArrayType<DataType = any, E = string>(errorMessage?: E): ArrayType<DataType, E>; |
@@ -12,5 +12,8 @@ import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized"; | ||
_MixedType.prototype.pushRule.call(_assertThisInitialized(_this), function (v) { | ||
return Array.isArray(v); | ||
}, errorMessage || 'Please enter a valid array'); | ||
_MixedType.prototype.pushRule.call(_assertThisInitialized(_this), { | ||
onValid: function onValid(v) { | ||
return Array.isArray(v); | ||
}, | ||
errorMessage: errorMessage || _this.locale.type | ||
}); | ||
@@ -23,6 +26,17 @@ return _this; | ||
_proto.rangeLength = function rangeLength(minLength, maxLength, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return value.length >= minLength && value.length <= maxLength; | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.rangeLength; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(value) { | ||
return value.length >= minLength && value.length <= maxLength; | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
minLength: minLength, | ||
maxLength: maxLength | ||
} | ||
}); | ||
return this; | ||
@@ -32,6 +46,16 @@ }; | ||
_proto.minLength = function minLength(_minLength, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return value.length >= _minLength; | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.minLength; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(value) { | ||
return value.length >= _minLength; | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
minLength: _minLength | ||
} | ||
}); | ||
return this; | ||
@@ -41,6 +65,16 @@ }; | ||
_proto.maxLength = function maxLength(_maxLength, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return value.length <= _maxLength; | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.maxLength; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(value) { | ||
return value.length <= _maxLength; | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
maxLength: _maxLength | ||
} | ||
}); | ||
return this; | ||
@@ -50,37 +84,42 @@ }; | ||
_proto.unrepeatable = function unrepeatable(errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (items) { | ||
var hash = {}; | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.unrepeatable; | ||
} | ||
for (var i in items) { | ||
if (hash[items[i]]) { | ||
return false; | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(items) { | ||
var hash = {}; | ||
for (var i in items) { | ||
if (hash[items[i]]) { | ||
return false; | ||
} | ||
hash[items[i]] = true; | ||
} | ||
hash[items[i]] = true; | ||
} | ||
return true; | ||
}, | ||
errorMessage: errorMessage | ||
}); | ||
return true; | ||
}, errorMessage); | ||
return this; | ||
} | ||
/** | ||
* @example | ||
* ArrayType().of(StringType().isOneOf(['数码','体育','游戏','旅途','其他'],'Can only be the value of a predefined option') | ||
*/ | ||
; | ||
}; | ||
_proto.of = function of(type, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (items) { | ||
var checkResults = items.map(function (value) { | ||
return type.check(value); | ||
}); | ||
var hasError = !!checkResults.find(function (item) { | ||
return item === null || item === void 0 ? void 0 : item.hasError; | ||
}); | ||
return { | ||
hasError: hasError, | ||
array: checkResults | ||
}; | ||
}, errorMessage); | ||
_proto.of = function of(type) { | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(items, data, filedName) { | ||
var checkResults = items.map(function (value, index) { | ||
var name = Array.isArray(filedName) ? [].concat(filedName, ["[" + index + "]"]) : [filedName, "[" + index + "]"]; | ||
return type.check(value, data, name); | ||
}); | ||
var hasError = !!checkResults.find(function (item) { | ||
return item === null || item === void 0 ? void 0 : item.hasError; | ||
}); | ||
return { | ||
hasError: hasError, | ||
array: checkResults | ||
}; | ||
} | ||
}); | ||
@@ -87,0 +126,0 @@ return this; |
import { MixedType } from './MixedType'; | ||
export declare class BooleanType<DataType = any, ErrorMsgType = string> extends MixedType<boolean, DataType, ErrorMsgType> { | ||
constructor(errorMessage?: ErrorMsgType); | ||
import { ErrorMessageType } from './types'; | ||
import { BooleanTypeLocale } from './locales'; | ||
export declare class BooleanType<DataType = any, E = ErrorMessageType> extends MixedType<boolean, DataType, E, BooleanTypeLocale> { | ||
constructor(errorMessage?: E | string); | ||
} | ||
export default function getBooleanType<DataType = any, ErrorMsgType = string>(errorMessage?: ErrorMsgType): BooleanType<DataType, ErrorMsgType>; | ||
export default function getBooleanType<DataType = any, E = string>(errorMessage?: E): BooleanType<DataType, E>; |
@@ -12,5 +12,8 @@ import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized"; | ||
_MixedType.prototype.pushRule.call(_assertThisInitialized(_this), function (v) { | ||
return typeof v === 'boolean'; | ||
}, errorMessage || 'Please enter a valid `boolean`'); | ||
_MixedType.prototype.pushRule.call(_assertThisInitialized(_this), { | ||
onValid: function onValid(v) { | ||
return typeof v === 'boolean'; | ||
}, | ||
errorMessage: errorMessage || _this.locale.type | ||
}); | ||
@@ -17,0 +20,0 @@ return _this; |
import { MixedType } from './MixedType'; | ||
export declare class DateType<DataType = any, ErrorMsgType = string> extends MixedType<string | Date, DataType, ErrorMsgType> { | ||
constructor(errorMessage?: ErrorMsgType); | ||
range(min: string | Date, max: string | Date, errorMessage?: ErrorMsgType): this; | ||
min(min: string | Date, errorMessage?: ErrorMsgType): this; | ||
max(max: string | Date, errorMessage?: ErrorMsgType): this; | ||
import { ErrorMessageType } from './types'; | ||
import { DateTypeLocale } from './locales'; | ||
export declare class DateType<DataType = any, E = ErrorMessageType> extends MixedType<string | Date, DataType, E, DateTypeLocale> { | ||
constructor(errorMessage?: E | string); | ||
range(min: string | Date, max: string | Date, errorMessage?: E | string): this; | ||
min(min: string | Date, errorMessage?: E | string): this; | ||
max(max: string | Date, errorMessage?: E | string): this; | ||
} | ||
export default function getDateType<DataType = any, ErrorMsgType = string>(errorMessage?: ErrorMsgType): DateType<DataType, ErrorMsgType>; | ||
export default function getDateType<DataType = any, E = string>(errorMessage?: E): DateType<DataType, E>; |
@@ -12,5 +12,8 @@ import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized"; | ||
_MixedType.prototype.pushRule.call(_assertThisInitialized(_this), function (value) { | ||
return !/Invalid|NaN/.test(new Date(value).toString()); | ||
}, errorMessage || 'Please enter a valid date'); | ||
_MixedType.prototype.pushRule.call(_assertThisInitialized(_this), { | ||
onValid: function onValid(value) { | ||
return !/Invalid|NaN/.test(new Date(value).toString()); | ||
}, | ||
errorMessage: errorMessage || _this.locale.type | ||
}); | ||
@@ -23,6 +26,17 @@ return _this; | ||
_proto.range = function range(min, max, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return new Date(value) >= new Date(min) && new Date(value) <= new Date(max); | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.range; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(value) { | ||
return new Date(value) >= new Date(min) && new Date(value) <= new Date(max); | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
min: min, | ||
max: max | ||
} | ||
}); | ||
return this; | ||
@@ -32,6 +46,16 @@ }; | ||
_proto.min = function min(_min, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return new Date(value) >= new Date(_min); | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.min; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(value) { | ||
return new Date(value) >= new Date(_min); | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
min: _min | ||
} | ||
}); | ||
return this; | ||
@@ -41,6 +65,16 @@ }; | ||
_proto.max = function max(_max, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return new Date(value) <= new Date(_max); | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.max; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(value) { | ||
return new Date(value) <= new Date(_max); | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
max: _max | ||
} | ||
}); | ||
return this; | ||
@@ -47,0 +81,0 @@ }; |
@@ -9,3 +9,15 @@ import { SchemaModel, Schema } from './Schema'; | ||
import { default as BooleanType } from './BooleanType'; | ||
export type { CheckResult, SchemaCheckResult, SchemaDeclaration, CheckType, RuleType, ValidCallbackType } from './types'; | ||
export { SchemaModel, Schema, MixedType, StringType, NumberType, ArrayType, DateType, ObjectType, BooleanType }; | ||
declare const _default: { | ||
Model: typeof SchemaModel; | ||
Types: { | ||
MixedType: typeof MixedType; | ||
StringType: typeof StringType; | ||
NumberType: typeof NumberType; | ||
ArrayType: typeof ArrayType; | ||
DateType: typeof DateType; | ||
ObjectType: typeof ObjectType; | ||
BooleanType: typeof BooleanType; | ||
}; | ||
}; | ||
export default _default; |
@@ -9,2 +9,14 @@ import { SchemaModel, Schema } from './Schema'; | ||
import { default as BooleanType } from './BooleanType'; | ||
export { SchemaModel, Schema, MixedType, StringType, NumberType, ArrayType, DateType, ObjectType, BooleanType }; | ||
export { SchemaModel, Schema, MixedType, StringType, NumberType, ArrayType, DateType, ObjectType, BooleanType }; | ||
export default { | ||
Model: SchemaModel, | ||
Types: { | ||
MixedType: MixedType, | ||
StringType: StringType, | ||
NumberType: NumberType, | ||
ArrayType: ArrayType, | ||
DateType: DateType, | ||
ObjectType: ObjectType, | ||
BooleanType: BooleanType | ||
} | ||
}; |
@@ -1,20 +0,22 @@ | ||
import { SchemaDeclaration, CheckResult, ValidCallbackType, RuleType } from './types'; | ||
export declare class MixedType<ValueType = any, DataType = any, ErrorMsgType = string> { | ||
readonly name?: string; | ||
import { SchemaDeclaration, CheckResult, ValidCallbackType, RuleType, ErrorMessageType, TypeName } from './types'; | ||
import { MixedTypeLocale } from './locales'; | ||
export declare class MixedType<ValueType = any, DataType = any, E = ErrorMessageType, L = any> { | ||
readonly typeName?: string; | ||
protected required: boolean; | ||
protected requiredMessage: ErrorMsgType | string; | ||
protected requiredMessage: E | string; | ||
protected trim: boolean; | ||
protected emptyAllowed: boolean; | ||
protected rules: RuleType<ValueType, DataType, ErrorMsgType | string>[]; | ||
protected priorityRules: RuleType<ValueType, DataType, ErrorMsgType | string>[]; | ||
schemaSpec: SchemaDeclaration<DataType, ErrorMsgType>; | ||
protected rules: RuleType<ValueType, DataType, E | string>[]; | ||
protected priorityRules: RuleType<ValueType, DataType, E | string>[]; | ||
schemaSpec: SchemaDeclaration<DataType, E>; | ||
value: any; | ||
constructor(name?: string); | ||
setSchemaOptions(schemaSpec: SchemaDeclaration<DataType, ErrorMsgType>, value: any): void; | ||
check(value?: ValueType, data?: DataType): CheckResult<string | ErrorMsgType>; | ||
checkAsync(value?: ValueType, data?: DataType): Promise<CheckResult<ErrorMsgType | string>>; | ||
protected pushRule(onValid: ValidCallbackType<ValueType, DataType, ErrorMsgType | string>, errorMessage?: ErrorMsgType | string, priority?: boolean): void; | ||
addRule(onValid: ValidCallbackType<ValueType, DataType, ErrorMsgType | string>, errorMessage?: ErrorMsgType | string, priority?: boolean): this; | ||
isRequired(errorMessage: ErrorMsgType | string, trim?: boolean): this; | ||
isRequiredOrEmpty(errorMessage: ErrorMsgType | string, trim?: boolean): this; | ||
locale: L & MixedTypeLocale; | ||
constructor(name?: TypeName); | ||
setSchemaOptions(schemaSpec: SchemaDeclaration<DataType, E>, value: any): void; | ||
check(value?: ValueType, data?: DataType, fieldName?: string | string[]): CheckResult<string | E>; | ||
checkAsync(value?: ValueType, data?: DataType, fieldName?: string | string[]): Promise<CheckResult<E | string>>; | ||
protected pushRule(rule: RuleType<ValueType, DataType, E | string>): void; | ||
addRule(onValid: ValidCallbackType<ValueType, DataType, E | string>, errorMessage?: E | string, priority?: boolean): this; | ||
isRequired(errorMessage?: E | string, trim?: boolean): this; | ||
isRequiredOrEmpty(errorMessage?: E | string, trim?: boolean): this; | ||
/** | ||
@@ -28,4 +30,4 @@ * Define data verification rules based on conditions. | ||
*/ | ||
when(condition: (schemaSpec: SchemaDeclaration<DataType, ErrorMsgType>) => MixedType): this; | ||
when(condition: (schemaSpec: SchemaDeclaration<DataType, E>) => MixedType): this; | ||
} | ||
export default function getMixedType<DataType = any, ErrorMsgType = string>(): MixedType<DataType, ErrorMsgType, string>; | ||
export default function getMixedType<DataType = any, E = ErrorMessageType>(): MixedType<DataType, E, string, any>; |
@@ -1,5 +0,6 @@ | ||
import { checkRequired, createValidator, createValidatorAsync, isEmpty } from './utils'; | ||
import { checkRequired, createValidator, createValidatorAsync, isEmpty, formatErrorMessage } from './utils'; | ||
import locales from './locales'; | ||
export var MixedType = /*#__PURE__*/function () { | ||
function MixedType(name) { | ||
this.name = void 0; | ||
this.typeName = void 0; | ||
this.required = false; | ||
@@ -13,3 +14,5 @@ this.requiredMessage = ''; | ||
this.value = void 0; | ||
this.name = name; | ||
this.locale = void 0; | ||
this.typeName = name; | ||
this.locale = Object.assign(name ? locales[name] : {}, locales.mixed); | ||
} | ||
@@ -24,3 +27,3 @@ | ||
_proto.check = function check(value, data) { | ||
_proto.check = function check(value, data, fieldName) { | ||
if (value === void 0) { | ||
@@ -33,7 +36,9 @@ value = this.value; | ||
hasError: true, | ||
errorMessage: this.requiredMessage | ||
errorMessage: formatErrorMessage(this.requiredMessage, { | ||
name: fieldName | ||
}) | ||
}; | ||
} | ||
var validator = createValidator(data); | ||
var validator = createValidator(data, fieldName); | ||
var checkStatus = validator(value, this.priorityRules); | ||
@@ -56,3 +61,3 @@ | ||
_proto.checkAsync = function checkAsync(value, data) { | ||
_proto.checkAsync = function checkAsync(value, data, fieldName) { | ||
var _this = this; | ||
@@ -67,7 +72,9 @@ | ||
hasError: true, | ||
errorMessage: this.requiredMessage | ||
errorMessage: formatErrorMessage(this.requiredMessage, { | ||
name: fieldName | ||
}) | ||
}); | ||
} | ||
var validator = createValidatorAsync(data); | ||
var validator = createValidatorAsync(data, fieldName); | ||
return new Promise(function (resolve) { | ||
@@ -98,17 +105,19 @@ return validator(value, _this.priorityRules).then(function (checkStatus) { | ||
_proto.pushRule = function pushRule(onValid, errorMessage, priority) { | ||
_proto.pushRule = function pushRule(rule) { | ||
var _this$rules, _this$rules$; | ||
errorMessage = errorMessage || ((_this$rules = this.rules) === null || _this$rules === void 0 ? void 0 : (_this$rules$ = _this$rules[0]) === null || _this$rules$ === void 0 ? void 0 : _this$rules$.errorMessage); | ||
var onValid = rule.onValid, | ||
errorMessage = rule.errorMessage, | ||
priority = rule.priority, | ||
params = rule.params; | ||
var nextRule = { | ||
onValid: onValid, | ||
params: params, | ||
errorMessage: errorMessage || ((_this$rules = this.rules) === null || _this$rules === void 0 ? void 0 : (_this$rules$ = _this$rules[0]) === null || _this$rules$ === void 0 ? void 0 : _this$rules$.errorMessage) | ||
}; | ||
if (priority) { | ||
this.priorityRules.push({ | ||
onValid: onValid, | ||
errorMessage: errorMessage | ||
}); | ||
this.priorityRules.push(nextRule); | ||
} else { | ||
this.rules.push({ | ||
onValid: onValid, | ||
errorMessage: errorMessage | ||
}); | ||
this.rules.push(nextRule); | ||
} | ||
@@ -118,3 +127,7 @@ }; | ||
_proto.addRule = function addRule(onValid, errorMessage, priority) { | ||
this.pushRule(onValid, errorMessage, priority); | ||
this.pushRule({ | ||
onValid: onValid, | ||
errorMessage: errorMessage, | ||
priority: priority | ||
}); | ||
return this; | ||
@@ -124,2 +137,6 @@ }; | ||
_proto.isRequired = function isRequired(errorMessage, trim) { | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.isRequired; | ||
} | ||
if (trim === void 0) { | ||
@@ -136,2 +153,6 @@ trim = true; | ||
_proto.isRequiredOrEmpty = function isRequiredOrEmpty(errorMessage, trim) { | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.isRequiredOrEmpty; | ||
} | ||
if (trim === void 0) { | ||
@@ -160,5 +181,5 @@ trim = true; | ||
this.addRule(function (value, data) { | ||
return condition(_this2.schemaSpec).check(value, data); | ||
}, 'error', true); | ||
this.addRule(function (value, data, filedName) { | ||
return condition(_this2.schemaSpec).check(value, data, filedName); | ||
}, undefined, true); | ||
return this; | ||
@@ -165,0 +186,0 @@ }; |
import { MixedType } from './MixedType'; | ||
export declare class NumberType<DataType = any, ErrorMsgType = string> extends MixedType<number | string, DataType, ErrorMsgType> { | ||
constructor(errorMessage?: ErrorMsgType); | ||
isInteger(errorMessage?: ErrorMsgType): this; | ||
pattern(regexp: RegExp, errorMessage?: ErrorMsgType): this; | ||
isOneOf(numLst: number[], errorMessage?: ErrorMsgType): this; | ||
range(min: number, max: number, errorMessage?: ErrorMsgType): this; | ||
min(min: number, errorMessage?: ErrorMsgType): this; | ||
max(max: number, errorMessage?: ErrorMsgType): this; | ||
import { ErrorMessageType } from './types'; | ||
import { NumberTypeLocale } from './locales'; | ||
export declare class NumberType<DataType = any, E = ErrorMessageType> extends MixedType<number | string, DataType, E, NumberTypeLocale> { | ||
constructor(errorMessage?: E | string); | ||
isInteger(errorMessage?: E | string): this; | ||
pattern(regexp: RegExp, errorMessage?: E | string): this; | ||
isOneOf(values: number[], errorMessage?: E | string): this; | ||
range(min: number, max: number, errorMessage?: E | string): this; | ||
min(min: number, errorMessage?: E | string): this; | ||
max(max: number, errorMessage?: E | string): this; | ||
} | ||
export default function getNumberType<DataType = any, ErrorMsgType = string>(errorMessage?: ErrorMsgType): NumberType<DataType, ErrorMsgType>; | ||
export default function getNumberType<DataType = any, E = string>(errorMessage?: E): NumberType<DataType, E>; |
@@ -5,3 +5,3 @@ import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized"; | ||
function FN(value) { | ||
function toNumber(value) { | ||
return +value; | ||
@@ -18,5 +18,8 @@ } | ||
_MixedType.prototype.pushRule.call(_assertThisInitialized(_this), function (value) { | ||
return /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value + ''); | ||
}, errorMessage || 'Please enter a valid number'); | ||
_MixedType.prototype.pushRule.call(_assertThisInitialized(_this), { | ||
onValid: function onValid(value) { | ||
return /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value + ''); | ||
}, | ||
errorMessage: errorMessage || _this.locale.type | ||
}); | ||
@@ -29,6 +32,13 @@ return _this; | ||
_proto.isInteger = function isInteger(errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return /^-?\d+$/.test(value + ''); | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.isInteger; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(value) { | ||
return /^-?\d+$/.test(value + ''); | ||
}, | ||
errorMessage: errorMessage | ||
}); | ||
return this; | ||
@@ -38,14 +48,34 @@ }; | ||
_proto.pattern = function pattern(regexp, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return regexp.test(value + ''); | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.pattern; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(value) { | ||
return regexp.test(value + ''); | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
regexp: regexp | ||
} | ||
}); | ||
return this; | ||
}; | ||
_proto.isOneOf = function isOneOf(numLst, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return numLst.includes(FN(value)); | ||
}, errorMessage); | ||
_proto.isOneOf = function isOneOf(values, errorMessage) { | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.isOneOf; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(value) { | ||
return values.includes(toNumber(value)); | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
values: values | ||
} | ||
}); | ||
return this; | ||
@@ -55,6 +85,17 @@ }; | ||
_proto.range = function range(min, max, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return FN(value) >= min && FN(value) <= max; | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.range; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(value) { | ||
return toNumber(value) >= min && toNumber(value) <= max; | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
min: min, | ||
max: max | ||
} | ||
}); | ||
return this; | ||
@@ -64,6 +105,16 @@ }; | ||
_proto.min = function min(_min, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return FN(value) >= _min; | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.min; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(value) { | ||
return toNumber(value) >= _min; | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
min: _min | ||
} | ||
}); | ||
return this; | ||
@@ -73,6 +124,16 @@ }; | ||
_proto.max = function max(_max, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return FN(value) <= _max; | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.max; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(value) { | ||
return toNumber(value) <= _max; | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
max: _max | ||
} | ||
}); | ||
return this; | ||
@@ -79,0 +140,0 @@ }; |
import { MixedType } from './MixedType'; | ||
import { PlainObject, SchemaDeclaration, CheckResult } from './types'; | ||
export declare class ObjectType<DataType = any, ErrorMsgType = string> extends MixedType<PlainObject, DataType, ErrorMsgType> { | ||
objectTypeSchemaSpec: SchemaDeclaration<DataType, ErrorMsgType>; | ||
constructor(errorMessage?: ErrorMsgType); | ||
check(value?: PlainObject, data?: DataType): CheckResult<string | ErrorMsgType>; | ||
checkAsync(value?: PlainObject, data?: DataType): Promise<CheckResult<string | ErrorMsgType>>; | ||
import { PlainObject, SchemaDeclaration, CheckResult, ErrorMessageType } from './types'; | ||
import { ObjectTypeLocale } from './locales'; | ||
export declare class ObjectType<DataType = any, E = ErrorMessageType> extends MixedType<PlainObject, DataType, E, ObjectTypeLocale> { | ||
objectTypeSchemaSpec: SchemaDeclaration<DataType, E>; | ||
constructor(errorMessage?: E | string); | ||
check(value?: PlainObject, data?: DataType, fieldName?: string | string[]): CheckResult<string | E>; | ||
checkAsync(value?: PlainObject, data?: DataType, fieldName?: string | string[]): Promise<CheckResult<string | E>>; | ||
/** | ||
@@ -15,4 +16,4 @@ * @example | ||
*/ | ||
shape(fields: SchemaDeclaration<DataType, ErrorMsgType>): this; | ||
shape(fields: SchemaDeclaration<DataType, E>): this; | ||
} | ||
export default function getObjectType<DataType = any, ErrorMsgType = string>(errorMessage?: ErrorMsgType): ObjectType<DataType, ErrorMsgType>; | ||
export default function getObjectType<DataType = any, E = string>(errorMessage?: E): ObjectType<DataType, E>; |
@@ -14,5 +14,8 @@ import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized"; | ||
_MixedType.prototype.pushRule.call(_assertThisInitialized(_this), function (v) { | ||
return typeof v === 'object'; | ||
}, errorMessage || 'Please enter a valid `object`'); | ||
_MixedType.prototype.pushRule.call(_assertThisInitialized(_this), { | ||
onValid: function onValid(v) { | ||
return typeof v === 'object'; | ||
}, | ||
errorMessage: errorMessage || _this.locale.type | ||
}); | ||
@@ -24,3 +27,3 @@ return _this; | ||
_proto.check = function check(value, data) { | ||
_proto.check = function check(value, data, fieldName) { | ||
if (value === void 0) { | ||
@@ -58,3 +61,3 @@ value = this.value; | ||
var validator = createValidator(data); | ||
var validator = createValidator(data, fieldName); | ||
var checkStatus = validator(value, type.priorityRules); | ||
@@ -80,3 +83,3 @@ | ||
_proto.checkAsync = function checkAsync(value, data) { | ||
_proto.checkAsync = function checkAsync(value, data, fieldName) { | ||
var _this2 = this; | ||
@@ -96,3 +99,3 @@ | ||
var validator = createValidatorAsync(data); | ||
var validator = createValidatorAsync(data, fieldName); | ||
return new Promise(function (resolve) { | ||
@@ -99,0 +102,0 @@ if (type.objectTypeSchemaSpec && typeof value === 'object') { |
@@ -46,3 +46,3 @@ export var Schema = /*#__PURE__*/function () { | ||
return fieldChecker.check(data[fieldName], data); | ||
return fieldChecker.check(data[fieldName], data, fieldName); | ||
}; | ||
@@ -61,3 +61,3 @@ | ||
return fieldChecker.checkAsync(data[fieldName], data); | ||
return fieldChecker.checkAsync(data[fieldName], data, fieldName); | ||
}; | ||
@@ -64,0 +64,0 @@ |
import { MixedType } from './MixedType'; | ||
export declare class StringType<DataType = any, ErrorMsgType = string> extends MixedType<string, DataType, ErrorMsgType> { | ||
constructor(errorMessage?: ErrorMsgType); | ||
containsLetter(errorMessage?: ErrorMsgType): this; | ||
containsUppercaseLetter(errorMessage?: ErrorMsgType): this; | ||
containsLowercaseLetter(errorMessage?: ErrorMsgType): this; | ||
containsLetterOnly(errorMessage?: ErrorMsgType): this; | ||
containsNumber(errorMessage?: ErrorMsgType): this; | ||
isOneOf(strArr: string[], errorMessage?: ErrorMsgType): this; | ||
isEmail(errorMessage?: ErrorMsgType): this; | ||
isURL(errorMessage?: ErrorMsgType): this; | ||
isHex(errorMessage?: ErrorMsgType): this; | ||
pattern(regexp: RegExp, errorMessage?: ErrorMsgType): this; | ||
rangeLength(minLength: number, maxLength: number, errorMessage?: ErrorMsgType): this; | ||
minLength(minLength: number, errorMessage?: ErrorMsgType): this; | ||
maxLength(maxLength: number, errorMessage?: ErrorMsgType): this; | ||
import { ErrorMessageType } from './types'; | ||
import { StringTypeLocale } from './locales'; | ||
export declare class StringType<DataType = any, E = ErrorMessageType> extends MixedType<string, DataType, E, StringTypeLocale> { | ||
constructor(errorMessage?: E | string); | ||
containsLetter(errorMessage?: E | string): this; | ||
containsUppercaseLetter(errorMessage?: E | string): this; | ||
containsLowercaseLetter(errorMessage?: E | string): this; | ||
containsLetterOnly(errorMessage?: E | string): this; | ||
containsNumber(errorMessage?: E | string): this; | ||
isOneOf(values: string[], errorMessage?: E | string): this; | ||
isEmail(errorMessage?: E | string): this; | ||
isURL(errorMessage?: E | string): this; | ||
isHex(errorMessage?: E | string): this; | ||
pattern(regexp: RegExp, errorMessage?: E | string): this; | ||
rangeLength(minLength: number, maxLength: number, errorMessage?: E | string): this; | ||
minLength(minLength: number, errorMessage?: E | string): this; | ||
maxLength(maxLength: number, errorMessage?: E | string): this; | ||
} | ||
export default function getStringType<DataType = any, ErrorMsgType = string>(errorMessage?: ErrorMsgType): StringType<DataType, ErrorMsgType>; | ||
export default function getStringType<DataType = any, E = string>(errorMessage?: E): StringType<DataType, E>; |
@@ -12,5 +12,8 @@ import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized"; | ||
_MixedType.prototype.pushRule.call(_assertThisInitialized(_this), function (v) { | ||
return typeof v === 'string'; | ||
}, errorMessage || 'Please enter a valid string'); | ||
_MixedType.prototype.pushRule.call(_assertThisInitialized(_this), { | ||
onValid: function onValid(v) { | ||
return typeof v === 'string'; | ||
}, | ||
errorMessage: errorMessage || _this.locale.type | ||
}); | ||
@@ -23,6 +26,13 @@ return _this; | ||
_proto.containsLetter = function containsLetter(errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (v) { | ||
return /[a-zA-Z]/.test(v); | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.containsLetter; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(v) { | ||
return /[a-zA-Z]/.test(v); | ||
}, | ||
errorMessage: errorMessage | ||
}); | ||
return this; | ||
@@ -32,6 +42,13 @@ }; | ||
_proto.containsUppercaseLetter = function containsUppercaseLetter(errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (v) { | ||
return /[A-Z]/.test(v); | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.containsUppercaseLetter; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(v) { | ||
return /[A-Z]/.test(v); | ||
}, | ||
errorMessage: errorMessage | ||
}); | ||
return this; | ||
@@ -41,6 +58,13 @@ }; | ||
_proto.containsLowercaseLetter = function containsLowercaseLetter(errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (v) { | ||
return /[a-z]/.test(v); | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.containsLowercaseLetter; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(v) { | ||
return /[a-z]/.test(v); | ||
}, | ||
errorMessage: errorMessage | ||
}); | ||
return this; | ||
@@ -50,6 +74,13 @@ }; | ||
_proto.containsLetterOnly = function containsLetterOnly(errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (v) { | ||
return /^[a-zA-Z]+$/.test(v); | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.containsLetterOnly; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(v) { | ||
return /^[a-zA-Z]+$/.test(v); | ||
}, | ||
errorMessage: errorMessage | ||
}); | ||
return this; | ||
@@ -59,14 +90,31 @@ }; | ||
_proto.containsNumber = function containsNumber(errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (v) { | ||
return /[0-9]/.test(v); | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.containsNumber; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(v) { | ||
return /[0-9]/.test(v); | ||
}, | ||
errorMessage: errorMessage | ||
}); | ||
return this; | ||
}; | ||
_proto.isOneOf = function isOneOf(strArr, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (v) { | ||
return !!~strArr.indexOf(v); | ||
}, errorMessage); | ||
_proto.isOneOf = function isOneOf(values, errorMessage) { | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.isOneOf; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(v) { | ||
return !!~values.indexOf(v); | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
values: values | ||
} | ||
}); | ||
return this; | ||
@@ -76,8 +124,15 @@ }; | ||
_proto.isEmail = function isEmail(errorMessage) { | ||
//http://emailregex.com/ | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.isEmail; | ||
} | ||
// http://emailregex.com/ | ||
var regexp = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | ||
_MixedType.prototype.pushRule.call(this, function (v) { | ||
return regexp.test(v); | ||
}, errorMessage); | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(v) { | ||
return regexp.test(v); | ||
}, | ||
errorMessage: errorMessage | ||
}); | ||
@@ -88,7 +143,14 @@ return this; | ||
_proto.isURL = function isURL(errorMessage) { | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.isURL; | ||
} | ||
var regexp = new RegExp("^(?!mailto:)(?:(?:http|https|ftp)://|//)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$", 'i'); | ||
_MixedType.prototype.pushRule.call(this, function (v) { | ||
return regexp.test(v); | ||
}, errorMessage); | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(v) { | ||
return regexp.test(v); | ||
}, | ||
errorMessage: errorMessage | ||
}); | ||
@@ -99,7 +161,14 @@ return this; | ||
_proto.isHex = function isHex(errorMessage) { | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.isHex; | ||
} | ||
var regexp = /^#?([a-f0-9]{6}|[a-f0-9]{3})$/i; | ||
_MixedType.prototype.pushRule.call(this, function (v) { | ||
return regexp.test(v); | ||
}, errorMessage); | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(v) { | ||
return regexp.test(v); | ||
}, | ||
errorMessage: errorMessage | ||
}); | ||
@@ -110,6 +179,16 @@ return this; | ||
_proto.pattern = function pattern(regexp, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return regexp.test(value); | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.pattern; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(v) { | ||
return regexp.test(v); | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
regexp: regexp | ||
} | ||
}); | ||
return this; | ||
@@ -119,6 +198,17 @@ }; | ||
_proto.rangeLength = function rangeLength(minLength, maxLength, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return value.length >= minLength && value.length <= maxLength; | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.rangeLength; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(value) { | ||
return value.length >= minLength && value.length <= maxLength; | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
minLength: minLength, | ||
maxLength: maxLength | ||
} | ||
}); | ||
return this; | ||
@@ -128,6 +218,16 @@ }; | ||
_proto.minLength = function minLength(_minLength, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return Array.from(value).length >= _minLength; | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.minLength; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(value) { | ||
return Array.from(value).length >= _minLength; | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
minLength: _minLength | ||
} | ||
}); | ||
return this; | ||
@@ -137,6 +237,16 @@ }; | ||
_proto.maxLength = function maxLength(_maxLength, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return Array.from(value).length <= _maxLength; | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.maxLength; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(value) { | ||
return Array.from(value).length <= _maxLength; | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
maxLength: _maxLength | ||
} | ||
}); | ||
return this; | ||
@@ -143,0 +253,0 @@ }; |
@@ -7,2 +7,3 @@ import { ArrayType } from './ArrayType'; | ||
import { ObjectType } from './ObjectType'; | ||
export declare type TypeName = 'array' | 'string' | 'boolean' | 'number' | 'object' | 'date'; | ||
export interface CheckResult<E = string> { | ||
@@ -14,3 +15,4 @@ hasError?: boolean; | ||
} | ||
export declare type ValidCallbackType<V, D, E> = (value: V, data?: D) => CheckResult<E> | boolean; | ||
export declare type ErrorMessageType = string; | ||
export declare type ValidCallbackType<V, D, E> = (value: V, data?: D, filedName?: string | string[]) => CheckResult<E> | boolean; | ||
export declare type PlainObject<T extends Record<string, unknown> = any> = { | ||
@@ -21,10 +23,12 @@ [P in keyof T]: T; | ||
onValid: ValidCallbackType<V, D, E>; | ||
errorMessage: E; | ||
errorMessage?: E; | ||
priority?: boolean; | ||
params?: any; | ||
} | ||
export declare type CheckType<X, T, ErrorMsgType = string> = X extends string ? StringType<T, ErrorMsgType> | DateType<T, ErrorMsgType> | NumberType<T, ErrorMsgType> : X extends number ? NumberType<T, ErrorMsgType> : X extends boolean ? BooleanType<T, ErrorMsgType> : X extends Date ? DateType<T, ErrorMsgType> : X extends Array<any> ? ArrayType<T, ErrorMsgType> : X extends Record<string, unknown> ? ObjectType<T, ErrorMsgType> : StringType<T, ErrorMsgType> | NumberType<T, ErrorMsgType> | BooleanType<T, ErrorMsgType> | ArrayType<T, ErrorMsgType> | DateType<T, ErrorMsgType> | ObjectType<T, ErrorMsgType>; | ||
export declare type SchemaDeclaration<T, ErrorMsgType = string> = { | ||
[P in keyof T]: CheckType<T[P], T, ErrorMsgType>; | ||
export declare type CheckType<X, T, E = ErrorMessageType> = X extends string ? StringType<T, E> | DateType<T, E> | NumberType<T, E> : X extends number ? NumberType<T, E> : X extends boolean ? BooleanType<T, E> : X extends Date ? DateType<T, E> : X extends Array<any> ? ArrayType<T, E> : X extends Record<string, unknown> ? ObjectType<T, E> : StringType<T, E> | NumberType<T, E> | BooleanType<T, E> | ArrayType<T, E> | DateType<T, E> | ObjectType<T, E>; | ||
export declare type SchemaDeclaration<T, E = string> = { | ||
[P in keyof T]: CheckType<T[P], T, E>; | ||
}; | ||
export declare type SchemaCheckResult<T, ErrorMsgType> = { | ||
[P in keyof T]: CheckResult<ErrorMsgType>; | ||
export declare type SchemaCheckResult<T, E> = { | ||
[P in keyof T]: CheckResult<E>; | ||
}; |
@@ -6,3 +6,3 @@ import { CheckResult, RuleType } from '../types'; | ||
*/ | ||
export declare function createValidator<V, D, E>(data?: D): (value: V, rules: RuleType<V, D, E>[]) => CheckResult<E> | null; | ||
export declare function createValidator<V, D, E>(data?: D, name?: string | string[]): (value: V, rules: RuleType<V, D, E>[]) => CheckResult<E> | null; | ||
export default createValidator; |
@@ -0,1 +1,3 @@ | ||
import _extends from "@babel/runtime/helpers/esm/extends"; | ||
import formatErrorMessage from './formatErrorMessage'; | ||
/** | ||
@@ -5,3 +7,4 @@ * Create a data validator | ||
*/ | ||
export function createValidator(data) { | ||
export function createValidator(data, name) { | ||
return function (value, rules) { | ||
@@ -11,4 +14,5 @@ for (var i = 0; i < rules.length; i += 1) { | ||
onValid = _rules$i.onValid, | ||
errorMessage = _rules$i.errorMessage; | ||
var checkResult = onValid(value, data); | ||
errorMessage = _rules$i.errorMessage, | ||
params = _rules$i.params; | ||
var checkResult = onValid(value, data, name); | ||
@@ -18,3 +22,5 @@ if (checkResult === false) { | ||
hasError: true, | ||
errorMessage: errorMessage | ||
errorMessage: formatErrorMessage(errorMessage, _extends({}, params, { | ||
name: Array.isArray(name) ? name.join('.') : name | ||
})) | ||
}; | ||
@@ -21,0 +27,0 @@ } else if (typeof checkResult === 'object' && (checkResult.hasError || checkResult.array)) { |
@@ -6,3 +6,3 @@ import { CheckResult, RuleType } from '../types'; | ||
*/ | ||
export declare function createValidatorAsync<V, D, E>(data?: D): (value: V, rules: RuleType<V, D, E>[]) => Promise<CheckResult<E>>; | ||
export declare function createValidatorAsync<V, D, E>(data?: D, name?: string | string[]): (value: V, rules: RuleType<V, D, E>[]) => Promise<CheckResult<E>>; | ||
export default createValidatorAsync; |
@@ -0,1 +1,3 @@ | ||
import _extends from "@babel/runtime/helpers/esm/extends"; | ||
import formatErrorMessage from './formatErrorMessage'; | ||
/** | ||
@@ -5,3 +7,4 @@ * Create a data asynchronous validator | ||
*/ | ||
export function createValidatorAsync(data) { | ||
export function createValidatorAsync(data, name) { | ||
function check(errorMessage) { | ||
@@ -25,4 +28,7 @@ return function (checkResult) { | ||
var onValid = rule.onValid, | ||
errorMessage = rule.errorMessage; | ||
return Promise.resolve(onValid(value, data)).then(check(errorMessage)); | ||
errorMessage = rule.errorMessage, | ||
params = rule.params; | ||
return Promise.resolve(onValid(value, data, name)).then(check(formatErrorMessage(errorMessage, _extends({}, params, { | ||
name: Array.isArray(name) ? name.join('.') : name | ||
})))); | ||
}); | ||
@@ -29,0 +35,0 @@ return Promise.all(promises).then(function (results) { |
@@ -6,1 +6,2 @@ export { default as basicEmptyCheck } from './basicEmptyCheck'; | ||
export { default as isEmpty } from './isEmpty'; | ||
export { default as formatErrorMessage } from './formatErrorMessage'; |
@@ -5,2 +5,3 @@ export { default as basicEmptyCheck } from './basicEmptyCheck'; | ||
export { default as createValidatorAsync } from './createValidatorAsync'; | ||
export { default as isEmpty } from './isEmpty'; | ||
export { default as isEmpty } from './isEmpty'; | ||
export { default as formatErrorMessage } from './formatErrorMessage'; |
import { MixedType } from './MixedType'; | ||
import { CheckType } from './types'; | ||
export declare class ArrayType<DataType = any, ErrorMsgType = string> extends MixedType<any[], DataType, ErrorMsgType> { | ||
constructor(errorMessage?: ErrorMsgType); | ||
rangeLength(minLength: number, maxLength: number, errorMessage?: ErrorMsgType): this; | ||
minLength(minLength: number, errorMessage?: ErrorMsgType): this; | ||
maxLength(maxLength: number, errorMessage?: ErrorMsgType): this; | ||
unrepeatable(errorMessage?: ErrorMsgType): this; | ||
/** | ||
* @example | ||
* ArrayType().of(StringType().isOneOf(['数码','体育','游戏','旅途','其他'],'Can only be the value of a predefined option') | ||
*/ | ||
of(type: CheckType<any[], DataType, ErrorMsgType>, errorMessage?: ErrorMsgType): this; | ||
import { CheckType, ErrorMessageType } from './types'; | ||
import { ArrayTypeLocale } from './locales'; | ||
export declare class ArrayType<DataType = any, E = ErrorMessageType> extends MixedType<any[], DataType, E, ArrayTypeLocale> { | ||
constructor(errorMessage?: E | string); | ||
rangeLength(minLength: number, maxLength: number, errorMessage?: E | string): this; | ||
minLength(minLength: number, errorMessage?: E | string): this; | ||
maxLength(maxLength: number, errorMessage?: E | string): this; | ||
unrepeatable(errorMessage?: E | string): this; | ||
of(type: CheckType<any[], DataType, E>): this; | ||
} | ||
export default function getArrayType<DataType = any, ErrorMsgType = string>(errorMessage?: ErrorMsgType): ArrayType<DataType, ErrorMsgType>; | ||
export default function getArrayType<DataType = any, E = string>(errorMessage?: E): ArrayType<DataType, E>; |
@@ -23,5 +23,8 @@ "use strict"; | ||
_MixedType.prototype.pushRule.call((0, _assertThisInitialized2["default"])(_this), function (v) { | ||
return Array.isArray(v); | ||
}, errorMessage || 'Please enter a valid array'); | ||
_MixedType.prototype.pushRule.call((0, _assertThisInitialized2["default"])(_this), { | ||
onValid: function onValid(v) { | ||
return Array.isArray(v); | ||
}, | ||
errorMessage: errorMessage || _this.locale.type | ||
}); | ||
@@ -34,6 +37,17 @@ return _this; | ||
_proto.rangeLength = function rangeLength(minLength, maxLength, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return value.length >= minLength && value.length <= maxLength; | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.rangeLength; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(value) { | ||
return value.length >= minLength && value.length <= maxLength; | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
minLength: minLength, | ||
maxLength: maxLength | ||
} | ||
}); | ||
return this; | ||
@@ -43,6 +57,16 @@ }; | ||
_proto.minLength = function minLength(_minLength, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return value.length >= _minLength; | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.minLength; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(value) { | ||
return value.length >= _minLength; | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
minLength: _minLength | ||
} | ||
}); | ||
return this; | ||
@@ -52,6 +76,16 @@ }; | ||
_proto.maxLength = function maxLength(_maxLength, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return value.length <= _maxLength; | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.maxLength; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(value) { | ||
return value.length <= _maxLength; | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
maxLength: _maxLength | ||
} | ||
}); | ||
return this; | ||
@@ -61,37 +95,42 @@ }; | ||
_proto.unrepeatable = function unrepeatable(errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (items) { | ||
var hash = {}; | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.unrepeatable; | ||
} | ||
for (var i in items) { | ||
if (hash[items[i]]) { | ||
return false; | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(items) { | ||
var hash = {}; | ||
for (var i in items) { | ||
if (hash[items[i]]) { | ||
return false; | ||
} | ||
hash[items[i]] = true; | ||
} | ||
hash[items[i]] = true; | ||
} | ||
return true; | ||
}, | ||
errorMessage: errorMessage | ||
}); | ||
return true; | ||
}, errorMessage); | ||
return this; | ||
} | ||
/** | ||
* @example | ||
* ArrayType().of(StringType().isOneOf(['数码','体育','游戏','旅途','其他'],'Can only be the value of a predefined option') | ||
*/ | ||
; | ||
}; | ||
_proto.of = function of(type, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (items) { | ||
var checkResults = items.map(function (value) { | ||
return type.check(value); | ||
}); | ||
var hasError = !!checkResults.find(function (item) { | ||
return item === null || item === void 0 ? void 0 : item.hasError; | ||
}); | ||
return { | ||
hasError: hasError, | ||
array: checkResults | ||
}; | ||
}, errorMessage); | ||
_proto.of = function of(type) { | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(items, data, filedName) { | ||
var checkResults = items.map(function (value, index) { | ||
var name = Array.isArray(filedName) ? [].concat(filedName, ["[" + index + "]"]) : [filedName, "[" + index + "]"]; | ||
return type.check(value, data, name); | ||
}); | ||
var hasError = !!checkResults.find(function (item) { | ||
return item === null || item === void 0 ? void 0 : item.hasError; | ||
}); | ||
return { | ||
hasError: hasError, | ||
array: checkResults | ||
}; | ||
} | ||
}); | ||
@@ -98,0 +137,0 @@ return this; |
import { MixedType } from './MixedType'; | ||
export declare class BooleanType<DataType = any, ErrorMsgType = string> extends MixedType<boolean, DataType, ErrorMsgType> { | ||
constructor(errorMessage?: ErrorMsgType); | ||
import { ErrorMessageType } from './types'; | ||
import { BooleanTypeLocale } from './locales'; | ||
export declare class BooleanType<DataType = any, E = ErrorMessageType> extends MixedType<boolean, DataType, E, BooleanTypeLocale> { | ||
constructor(errorMessage?: E | string); | ||
} | ||
export default function getBooleanType<DataType = any, ErrorMsgType = string>(errorMessage?: ErrorMsgType): BooleanType<DataType, ErrorMsgType>; | ||
export default function getBooleanType<DataType = any, E = string>(errorMessage?: E): BooleanType<DataType, E>; |
@@ -23,5 +23,8 @@ "use strict"; | ||
_MixedType.prototype.pushRule.call((0, _assertThisInitialized2["default"])(_this), function (v) { | ||
return typeof v === 'boolean'; | ||
}, errorMessage || 'Please enter a valid `boolean`'); | ||
_MixedType.prototype.pushRule.call((0, _assertThisInitialized2["default"])(_this), { | ||
onValid: function onValid(v) { | ||
return typeof v === 'boolean'; | ||
}, | ||
errorMessage: errorMessage || _this.locale.type | ||
}); | ||
@@ -28,0 +31,0 @@ return _this; |
import { MixedType } from './MixedType'; | ||
export declare class DateType<DataType = any, ErrorMsgType = string> extends MixedType<string | Date, DataType, ErrorMsgType> { | ||
constructor(errorMessage?: ErrorMsgType); | ||
range(min: string | Date, max: string | Date, errorMessage?: ErrorMsgType): this; | ||
min(min: string | Date, errorMessage?: ErrorMsgType): this; | ||
max(max: string | Date, errorMessage?: ErrorMsgType): this; | ||
import { ErrorMessageType } from './types'; | ||
import { DateTypeLocale } from './locales'; | ||
export declare class DateType<DataType = any, E = ErrorMessageType> extends MixedType<string | Date, DataType, E, DateTypeLocale> { | ||
constructor(errorMessage?: E | string); | ||
range(min: string | Date, max: string | Date, errorMessage?: E | string): this; | ||
min(min: string | Date, errorMessage?: E | string): this; | ||
max(max: string | Date, errorMessage?: E | string): this; | ||
} | ||
export default function getDateType<DataType = any, ErrorMsgType = string>(errorMessage?: ErrorMsgType): DateType<DataType, ErrorMsgType>; | ||
export default function getDateType<DataType = any, E = string>(errorMessage?: E): DateType<DataType, E>; |
@@ -23,5 +23,8 @@ "use strict"; | ||
_MixedType.prototype.pushRule.call((0, _assertThisInitialized2["default"])(_this), function (value) { | ||
return !/Invalid|NaN/.test(new Date(value).toString()); | ||
}, errorMessage || 'Please enter a valid date'); | ||
_MixedType.prototype.pushRule.call((0, _assertThisInitialized2["default"])(_this), { | ||
onValid: function onValid(value) { | ||
return !/Invalid|NaN/.test(new Date(value).toString()); | ||
}, | ||
errorMessage: errorMessage || _this.locale.type | ||
}); | ||
@@ -34,6 +37,17 @@ return _this; | ||
_proto.range = function range(min, max, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return new Date(value) >= new Date(min) && new Date(value) <= new Date(max); | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.range; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(value) { | ||
return new Date(value) >= new Date(min) && new Date(value) <= new Date(max); | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
min: min, | ||
max: max | ||
} | ||
}); | ||
return this; | ||
@@ -43,6 +57,16 @@ }; | ||
_proto.min = function min(_min, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return new Date(value) >= new Date(_min); | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.min; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(value) { | ||
return new Date(value) >= new Date(_min); | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
min: _min | ||
} | ||
}); | ||
return this; | ||
@@ -52,6 +76,16 @@ }; | ||
_proto.max = function max(_max, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return new Date(value) <= new Date(_max); | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.max; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(value) { | ||
return new Date(value) <= new Date(_max); | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
max: _max | ||
} | ||
}); | ||
return this; | ||
@@ -58,0 +92,0 @@ }; |
@@ -9,3 +9,15 @@ import { SchemaModel, Schema } from './Schema'; | ||
import { default as BooleanType } from './BooleanType'; | ||
export type { CheckResult, SchemaCheckResult, SchemaDeclaration, CheckType, RuleType, ValidCallbackType } from './types'; | ||
export { SchemaModel, Schema, MixedType, StringType, NumberType, ArrayType, DateType, ObjectType, BooleanType }; | ||
declare const _default: { | ||
Model: typeof SchemaModel; | ||
Types: { | ||
MixedType: typeof MixedType; | ||
StringType: typeof StringType; | ||
NumberType: typeof NumberType; | ||
ArrayType: typeof ArrayType; | ||
DateType: typeof DateType; | ||
ObjectType: typeof ObjectType; | ||
BooleanType: typeof BooleanType; | ||
}; | ||
}; | ||
export default _default; |
@@ -6,2 +6,3 @@ "use strict"; | ||
exports.__esModule = true; | ||
exports["default"] = void 0; | ||
@@ -39,2 +40,15 @@ var _Schema = require("./Schema"); | ||
exports.BooleanType = _BooleanType["default"]; | ||
exports.BooleanType = _BooleanType["default"]; | ||
var _default = { | ||
Model: _Schema.SchemaModel, | ||
Types: { | ||
MixedType: _MixedType["default"], | ||
StringType: _StringType["default"], | ||
NumberType: _NumberType["default"], | ||
ArrayType: _ArrayType["default"], | ||
DateType: _DateType["default"], | ||
ObjectType: _ObjectType["default"], | ||
BooleanType: _BooleanType["default"] | ||
} | ||
}; | ||
exports["default"] = _default; |
@@ -1,20 +0,22 @@ | ||
import { SchemaDeclaration, CheckResult, ValidCallbackType, RuleType } from './types'; | ||
export declare class MixedType<ValueType = any, DataType = any, ErrorMsgType = string> { | ||
readonly name?: string; | ||
import { SchemaDeclaration, CheckResult, ValidCallbackType, RuleType, ErrorMessageType, TypeName } from './types'; | ||
import { MixedTypeLocale } from './locales'; | ||
export declare class MixedType<ValueType = any, DataType = any, E = ErrorMessageType, L = any> { | ||
readonly typeName?: string; | ||
protected required: boolean; | ||
protected requiredMessage: ErrorMsgType | string; | ||
protected requiredMessage: E | string; | ||
protected trim: boolean; | ||
protected emptyAllowed: boolean; | ||
protected rules: RuleType<ValueType, DataType, ErrorMsgType | string>[]; | ||
protected priorityRules: RuleType<ValueType, DataType, ErrorMsgType | string>[]; | ||
schemaSpec: SchemaDeclaration<DataType, ErrorMsgType>; | ||
protected rules: RuleType<ValueType, DataType, E | string>[]; | ||
protected priorityRules: RuleType<ValueType, DataType, E | string>[]; | ||
schemaSpec: SchemaDeclaration<DataType, E>; | ||
value: any; | ||
constructor(name?: string); | ||
setSchemaOptions(schemaSpec: SchemaDeclaration<DataType, ErrorMsgType>, value: any): void; | ||
check(value?: ValueType, data?: DataType): CheckResult<string | ErrorMsgType>; | ||
checkAsync(value?: ValueType, data?: DataType): Promise<CheckResult<ErrorMsgType | string>>; | ||
protected pushRule(onValid: ValidCallbackType<ValueType, DataType, ErrorMsgType | string>, errorMessage?: ErrorMsgType | string, priority?: boolean): void; | ||
addRule(onValid: ValidCallbackType<ValueType, DataType, ErrorMsgType | string>, errorMessage?: ErrorMsgType | string, priority?: boolean): this; | ||
isRequired(errorMessage: ErrorMsgType | string, trim?: boolean): this; | ||
isRequiredOrEmpty(errorMessage: ErrorMsgType | string, trim?: boolean): this; | ||
locale: L & MixedTypeLocale; | ||
constructor(name?: TypeName); | ||
setSchemaOptions(schemaSpec: SchemaDeclaration<DataType, E>, value: any): void; | ||
check(value?: ValueType, data?: DataType, fieldName?: string | string[]): CheckResult<string | E>; | ||
checkAsync(value?: ValueType, data?: DataType, fieldName?: string | string[]): Promise<CheckResult<E | string>>; | ||
protected pushRule(rule: RuleType<ValueType, DataType, E | string>): void; | ||
addRule(onValid: ValidCallbackType<ValueType, DataType, E | string>, errorMessage?: E | string, priority?: boolean): this; | ||
isRequired(errorMessage?: E | string, trim?: boolean): this; | ||
isRequiredOrEmpty(errorMessage?: E | string, trim?: boolean): this; | ||
/** | ||
@@ -28,4 +30,4 @@ * Define data verification rules based on conditions. | ||
*/ | ||
when(condition: (schemaSpec: SchemaDeclaration<DataType, ErrorMsgType>) => MixedType): this; | ||
when(condition: (schemaSpec: SchemaDeclaration<DataType, E>) => MixedType): this; | ||
} | ||
export default function getMixedType<DataType = any, ErrorMsgType = string>(): MixedType<DataType, ErrorMsgType, string>; | ||
export default function getMixedType<DataType = any, E = ErrorMessageType>(): MixedType<DataType, E, string, any>; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
exports.__esModule = true; | ||
@@ -9,5 +11,7 @@ exports["default"] = getMixedType; | ||
var _locales = _interopRequireDefault(require("./locales")); | ||
var MixedType = /*#__PURE__*/function () { | ||
function MixedType(name) { | ||
this.name = void 0; | ||
this.typeName = void 0; | ||
this.required = false; | ||
@@ -21,3 +25,5 @@ this.requiredMessage = ''; | ||
this.value = void 0; | ||
this.name = name; | ||
this.locale = void 0; | ||
this.typeName = name; | ||
this.locale = Object.assign(name ? _locales["default"][name] : {}, _locales["default"].mixed); | ||
} | ||
@@ -32,3 +38,3 @@ | ||
_proto.check = function check(value, data) { | ||
_proto.check = function check(value, data, fieldName) { | ||
if (value === void 0) { | ||
@@ -41,7 +47,9 @@ value = this.value; | ||
hasError: true, | ||
errorMessage: this.requiredMessage | ||
errorMessage: (0, _utils.formatErrorMessage)(this.requiredMessage, { | ||
name: fieldName | ||
}) | ||
}; | ||
} | ||
var validator = (0, _utils.createValidator)(data); | ||
var validator = (0, _utils.createValidator)(data, fieldName); | ||
var checkStatus = validator(value, this.priorityRules); | ||
@@ -64,3 +72,3 @@ | ||
_proto.checkAsync = function checkAsync(value, data) { | ||
_proto.checkAsync = function checkAsync(value, data, fieldName) { | ||
var _this = this; | ||
@@ -75,7 +83,9 @@ | ||
hasError: true, | ||
errorMessage: this.requiredMessage | ||
errorMessage: (0, _utils.formatErrorMessage)(this.requiredMessage, { | ||
name: fieldName | ||
}) | ||
}); | ||
} | ||
var validator = (0, _utils.createValidatorAsync)(data); | ||
var validator = (0, _utils.createValidatorAsync)(data, fieldName); | ||
return new Promise(function (resolve) { | ||
@@ -106,17 +116,19 @@ return validator(value, _this.priorityRules).then(function (checkStatus) { | ||
_proto.pushRule = function pushRule(onValid, errorMessage, priority) { | ||
_proto.pushRule = function pushRule(rule) { | ||
var _this$rules, _this$rules$; | ||
errorMessage = errorMessage || ((_this$rules = this.rules) === null || _this$rules === void 0 ? void 0 : (_this$rules$ = _this$rules[0]) === null || _this$rules$ === void 0 ? void 0 : _this$rules$.errorMessage); | ||
var onValid = rule.onValid, | ||
errorMessage = rule.errorMessage, | ||
priority = rule.priority, | ||
params = rule.params; | ||
var nextRule = { | ||
onValid: onValid, | ||
params: params, | ||
errorMessage: errorMessage || ((_this$rules = this.rules) === null || _this$rules === void 0 ? void 0 : (_this$rules$ = _this$rules[0]) === null || _this$rules$ === void 0 ? void 0 : _this$rules$.errorMessage) | ||
}; | ||
if (priority) { | ||
this.priorityRules.push({ | ||
onValid: onValid, | ||
errorMessage: errorMessage | ||
}); | ||
this.priorityRules.push(nextRule); | ||
} else { | ||
this.rules.push({ | ||
onValid: onValid, | ||
errorMessage: errorMessage | ||
}); | ||
this.rules.push(nextRule); | ||
} | ||
@@ -126,3 +138,7 @@ }; | ||
_proto.addRule = function addRule(onValid, errorMessage, priority) { | ||
this.pushRule(onValid, errorMessage, priority); | ||
this.pushRule({ | ||
onValid: onValid, | ||
errorMessage: errorMessage, | ||
priority: priority | ||
}); | ||
return this; | ||
@@ -132,2 +148,6 @@ }; | ||
_proto.isRequired = function isRequired(errorMessage, trim) { | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.isRequired; | ||
} | ||
if (trim === void 0) { | ||
@@ -144,2 +164,6 @@ trim = true; | ||
_proto.isRequiredOrEmpty = function isRequiredOrEmpty(errorMessage, trim) { | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.isRequiredOrEmpty; | ||
} | ||
if (trim === void 0) { | ||
@@ -168,5 +192,5 @@ trim = true; | ||
this.addRule(function (value, data) { | ||
return condition(_this2.schemaSpec).check(value, data); | ||
}, 'error', true); | ||
this.addRule(function (value, data, filedName) { | ||
return condition(_this2.schemaSpec).check(value, data, filedName); | ||
}, undefined, true); | ||
return this; | ||
@@ -173,0 +197,0 @@ }; |
import { MixedType } from './MixedType'; | ||
export declare class NumberType<DataType = any, ErrorMsgType = string> extends MixedType<number | string, DataType, ErrorMsgType> { | ||
constructor(errorMessage?: ErrorMsgType); | ||
isInteger(errorMessage?: ErrorMsgType): this; | ||
pattern(regexp: RegExp, errorMessage?: ErrorMsgType): this; | ||
isOneOf(numLst: number[], errorMessage?: ErrorMsgType): this; | ||
range(min: number, max: number, errorMessage?: ErrorMsgType): this; | ||
min(min: number, errorMessage?: ErrorMsgType): this; | ||
max(max: number, errorMessage?: ErrorMsgType): this; | ||
import { ErrorMessageType } from './types'; | ||
import { NumberTypeLocale } from './locales'; | ||
export declare class NumberType<DataType = any, E = ErrorMessageType> extends MixedType<number | string, DataType, E, NumberTypeLocale> { | ||
constructor(errorMessage?: E | string); | ||
isInteger(errorMessage?: E | string): this; | ||
pattern(regexp: RegExp, errorMessage?: E | string): this; | ||
isOneOf(values: number[], errorMessage?: E | string): this; | ||
range(min: number, max: number, errorMessage?: E | string): this; | ||
min(min: number, errorMessage?: E | string): this; | ||
max(max: number, errorMessage?: E | string): this; | ||
} | ||
export default function getNumberType<DataType = any, ErrorMsgType = string>(errorMessage?: ErrorMsgType): NumberType<DataType, ErrorMsgType>; | ||
export default function getNumberType<DataType = any, E = string>(errorMessage?: E): NumberType<DataType, E>; |
@@ -15,3 +15,3 @@ "use strict"; | ||
function FN(value) { | ||
function toNumber(value) { | ||
return +value; | ||
@@ -28,5 +28,8 @@ } | ||
_MixedType.prototype.pushRule.call((0, _assertThisInitialized2["default"])(_this), function (value) { | ||
return /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value + ''); | ||
}, errorMessage || 'Please enter a valid number'); | ||
_MixedType.prototype.pushRule.call((0, _assertThisInitialized2["default"])(_this), { | ||
onValid: function onValid(value) { | ||
return /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value + ''); | ||
}, | ||
errorMessage: errorMessage || _this.locale.type | ||
}); | ||
@@ -39,6 +42,13 @@ return _this; | ||
_proto.isInteger = function isInteger(errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return /^-?\d+$/.test(value + ''); | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.isInteger; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(value) { | ||
return /^-?\d+$/.test(value + ''); | ||
}, | ||
errorMessage: errorMessage | ||
}); | ||
return this; | ||
@@ -48,14 +58,34 @@ }; | ||
_proto.pattern = function pattern(regexp, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return regexp.test(value + ''); | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.pattern; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(value) { | ||
return regexp.test(value + ''); | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
regexp: regexp | ||
} | ||
}); | ||
return this; | ||
}; | ||
_proto.isOneOf = function isOneOf(numLst, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return numLst.includes(FN(value)); | ||
}, errorMessage); | ||
_proto.isOneOf = function isOneOf(values, errorMessage) { | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.isOneOf; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(value) { | ||
return values.includes(toNumber(value)); | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
values: values | ||
} | ||
}); | ||
return this; | ||
@@ -65,6 +95,17 @@ }; | ||
_proto.range = function range(min, max, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return FN(value) >= min && FN(value) <= max; | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.range; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(value) { | ||
return toNumber(value) >= min && toNumber(value) <= max; | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
min: min, | ||
max: max | ||
} | ||
}); | ||
return this; | ||
@@ -74,6 +115,16 @@ }; | ||
_proto.min = function min(_min, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return FN(value) >= _min; | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.min; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(value) { | ||
return toNumber(value) >= _min; | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
min: _min | ||
} | ||
}); | ||
return this; | ||
@@ -83,6 +134,16 @@ }; | ||
_proto.max = function max(_max, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return FN(value) <= _max; | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.max; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(value) { | ||
return toNumber(value) <= _max; | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
max: _max | ||
} | ||
}); | ||
return this; | ||
@@ -89,0 +150,0 @@ }; |
import { MixedType } from './MixedType'; | ||
import { PlainObject, SchemaDeclaration, CheckResult } from './types'; | ||
export declare class ObjectType<DataType = any, ErrorMsgType = string> extends MixedType<PlainObject, DataType, ErrorMsgType> { | ||
objectTypeSchemaSpec: SchemaDeclaration<DataType, ErrorMsgType>; | ||
constructor(errorMessage?: ErrorMsgType); | ||
check(value?: PlainObject, data?: DataType): CheckResult<string | ErrorMsgType>; | ||
checkAsync(value?: PlainObject, data?: DataType): Promise<CheckResult<string | ErrorMsgType>>; | ||
import { PlainObject, SchemaDeclaration, CheckResult, ErrorMessageType } from './types'; | ||
import { ObjectTypeLocale } from './locales'; | ||
export declare class ObjectType<DataType = any, E = ErrorMessageType> extends MixedType<PlainObject, DataType, E, ObjectTypeLocale> { | ||
objectTypeSchemaSpec: SchemaDeclaration<DataType, E>; | ||
constructor(errorMessage?: E | string); | ||
check(value?: PlainObject, data?: DataType, fieldName?: string | string[]): CheckResult<string | E>; | ||
checkAsync(value?: PlainObject, data?: DataType, fieldName?: string | string[]): Promise<CheckResult<string | E>>; | ||
/** | ||
@@ -15,4 +16,4 @@ * @example | ||
*/ | ||
shape(fields: SchemaDeclaration<DataType, ErrorMsgType>): this; | ||
shape(fields: SchemaDeclaration<DataType, E>): this; | ||
} | ||
export default function getObjectType<DataType = any, ErrorMsgType = string>(errorMessage?: ErrorMsgType): ObjectType<DataType, ErrorMsgType>; | ||
export default function getObjectType<DataType = any, E = string>(errorMessage?: E): ObjectType<DataType, E>; |
@@ -26,5 +26,8 @@ "use strict"; | ||
_MixedType.prototype.pushRule.call((0, _assertThisInitialized2["default"])(_this), function (v) { | ||
return typeof v === 'object'; | ||
}, errorMessage || 'Please enter a valid `object`'); | ||
_MixedType.prototype.pushRule.call((0, _assertThisInitialized2["default"])(_this), { | ||
onValid: function onValid(v) { | ||
return typeof v === 'object'; | ||
}, | ||
errorMessage: errorMessage || _this.locale.type | ||
}); | ||
@@ -36,3 +39,3 @@ return _this; | ||
_proto.check = function check(value, data) { | ||
_proto.check = function check(value, data, fieldName) { | ||
if (value === void 0) { | ||
@@ -70,3 +73,3 @@ value = this.value; | ||
var validator = (0, _utils.createValidator)(data); | ||
var validator = (0, _utils.createValidator)(data, fieldName); | ||
var checkStatus = validator(value, type.priorityRules); | ||
@@ -92,3 +95,3 @@ | ||
_proto.checkAsync = function checkAsync(value, data) { | ||
_proto.checkAsync = function checkAsync(value, data, fieldName) { | ||
var _this2 = this; | ||
@@ -108,3 +111,3 @@ | ||
var validator = (0, _utils.createValidatorAsync)(data); | ||
var validator = (0, _utils.createValidatorAsync)(data, fieldName); | ||
return new Promise(function (resolve) { | ||
@@ -111,0 +114,0 @@ if (type.objectTypeSchemaSpec && typeof value === 'object') { |
@@ -52,3 +52,3 @@ "use strict"; | ||
return fieldChecker.check(data[fieldName], data); | ||
return fieldChecker.check(data[fieldName], data, fieldName); | ||
}; | ||
@@ -67,3 +67,3 @@ | ||
return fieldChecker.checkAsync(data[fieldName], data); | ||
return fieldChecker.checkAsync(data[fieldName], data, fieldName); | ||
}; | ||
@@ -70,0 +70,0 @@ |
import { MixedType } from './MixedType'; | ||
export declare class StringType<DataType = any, ErrorMsgType = string> extends MixedType<string, DataType, ErrorMsgType> { | ||
constructor(errorMessage?: ErrorMsgType); | ||
containsLetter(errorMessage?: ErrorMsgType): this; | ||
containsUppercaseLetter(errorMessage?: ErrorMsgType): this; | ||
containsLowercaseLetter(errorMessage?: ErrorMsgType): this; | ||
containsLetterOnly(errorMessage?: ErrorMsgType): this; | ||
containsNumber(errorMessage?: ErrorMsgType): this; | ||
isOneOf(strArr: string[], errorMessage?: ErrorMsgType): this; | ||
isEmail(errorMessage?: ErrorMsgType): this; | ||
isURL(errorMessage?: ErrorMsgType): this; | ||
isHex(errorMessage?: ErrorMsgType): this; | ||
pattern(regexp: RegExp, errorMessage?: ErrorMsgType): this; | ||
rangeLength(minLength: number, maxLength: number, errorMessage?: ErrorMsgType): this; | ||
minLength(minLength: number, errorMessage?: ErrorMsgType): this; | ||
maxLength(maxLength: number, errorMessage?: ErrorMsgType): this; | ||
import { ErrorMessageType } from './types'; | ||
import { StringTypeLocale } from './locales'; | ||
export declare class StringType<DataType = any, E = ErrorMessageType> extends MixedType<string, DataType, E, StringTypeLocale> { | ||
constructor(errorMessage?: E | string); | ||
containsLetter(errorMessage?: E | string): this; | ||
containsUppercaseLetter(errorMessage?: E | string): this; | ||
containsLowercaseLetter(errorMessage?: E | string): this; | ||
containsLetterOnly(errorMessage?: E | string): this; | ||
containsNumber(errorMessage?: E | string): this; | ||
isOneOf(values: string[], errorMessage?: E | string): this; | ||
isEmail(errorMessage?: E | string): this; | ||
isURL(errorMessage?: E | string): this; | ||
isHex(errorMessage?: E | string): this; | ||
pattern(regexp: RegExp, errorMessage?: E | string): this; | ||
rangeLength(minLength: number, maxLength: number, errorMessage?: E | string): this; | ||
minLength(minLength: number, errorMessage?: E | string): this; | ||
maxLength(maxLength: number, errorMessage?: E | string): this; | ||
} | ||
export default function getStringType<DataType = any, ErrorMsgType = string>(errorMessage?: ErrorMsgType): StringType<DataType, ErrorMsgType>; | ||
export default function getStringType<DataType = any, E = string>(errorMessage?: E): StringType<DataType, E>; |
@@ -23,5 +23,8 @@ "use strict"; | ||
_MixedType.prototype.pushRule.call((0, _assertThisInitialized2["default"])(_this), function (v) { | ||
return typeof v === 'string'; | ||
}, errorMessage || 'Please enter a valid string'); | ||
_MixedType.prototype.pushRule.call((0, _assertThisInitialized2["default"])(_this), { | ||
onValid: function onValid(v) { | ||
return typeof v === 'string'; | ||
}, | ||
errorMessage: errorMessage || _this.locale.type | ||
}); | ||
@@ -34,6 +37,13 @@ return _this; | ||
_proto.containsLetter = function containsLetter(errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (v) { | ||
return /[a-zA-Z]/.test(v); | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.containsLetter; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(v) { | ||
return /[a-zA-Z]/.test(v); | ||
}, | ||
errorMessage: errorMessage | ||
}); | ||
return this; | ||
@@ -43,6 +53,13 @@ }; | ||
_proto.containsUppercaseLetter = function containsUppercaseLetter(errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (v) { | ||
return /[A-Z]/.test(v); | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.containsUppercaseLetter; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(v) { | ||
return /[A-Z]/.test(v); | ||
}, | ||
errorMessage: errorMessage | ||
}); | ||
return this; | ||
@@ -52,6 +69,13 @@ }; | ||
_proto.containsLowercaseLetter = function containsLowercaseLetter(errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (v) { | ||
return /[a-z]/.test(v); | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.containsLowercaseLetter; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(v) { | ||
return /[a-z]/.test(v); | ||
}, | ||
errorMessage: errorMessage | ||
}); | ||
return this; | ||
@@ -61,6 +85,13 @@ }; | ||
_proto.containsLetterOnly = function containsLetterOnly(errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (v) { | ||
return /^[a-zA-Z]+$/.test(v); | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.containsLetterOnly; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(v) { | ||
return /^[a-zA-Z]+$/.test(v); | ||
}, | ||
errorMessage: errorMessage | ||
}); | ||
return this; | ||
@@ -70,14 +101,31 @@ }; | ||
_proto.containsNumber = function containsNumber(errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (v) { | ||
return /[0-9]/.test(v); | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.containsNumber; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(v) { | ||
return /[0-9]/.test(v); | ||
}, | ||
errorMessage: errorMessage | ||
}); | ||
return this; | ||
}; | ||
_proto.isOneOf = function isOneOf(strArr, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (v) { | ||
return !!~strArr.indexOf(v); | ||
}, errorMessage); | ||
_proto.isOneOf = function isOneOf(values, errorMessage) { | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.isOneOf; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(v) { | ||
return !!~values.indexOf(v); | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
values: values | ||
} | ||
}); | ||
return this; | ||
@@ -87,8 +135,15 @@ }; | ||
_proto.isEmail = function isEmail(errorMessage) { | ||
//http://emailregex.com/ | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.isEmail; | ||
} | ||
// http://emailregex.com/ | ||
var regexp = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | ||
_MixedType.prototype.pushRule.call(this, function (v) { | ||
return regexp.test(v); | ||
}, errorMessage); | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(v) { | ||
return regexp.test(v); | ||
}, | ||
errorMessage: errorMessage | ||
}); | ||
@@ -99,7 +154,14 @@ return this; | ||
_proto.isURL = function isURL(errorMessage) { | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.isURL; | ||
} | ||
var regexp = new RegExp("^(?!mailto:)(?:(?:http|https|ftp)://|//)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$", 'i'); | ||
_MixedType.prototype.pushRule.call(this, function (v) { | ||
return regexp.test(v); | ||
}, errorMessage); | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(v) { | ||
return regexp.test(v); | ||
}, | ||
errorMessage: errorMessage | ||
}); | ||
@@ -110,7 +172,14 @@ return this; | ||
_proto.isHex = function isHex(errorMessage) { | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.isHex; | ||
} | ||
var regexp = /^#?([a-f0-9]{6}|[a-f0-9]{3})$/i; | ||
_MixedType.prototype.pushRule.call(this, function (v) { | ||
return regexp.test(v); | ||
}, errorMessage); | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(v) { | ||
return regexp.test(v); | ||
}, | ||
errorMessage: errorMessage | ||
}); | ||
@@ -121,6 +190,16 @@ return this; | ||
_proto.pattern = function pattern(regexp, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return regexp.test(value); | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.pattern; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(v) { | ||
return regexp.test(v); | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
regexp: regexp | ||
} | ||
}); | ||
return this; | ||
@@ -130,6 +209,17 @@ }; | ||
_proto.rangeLength = function rangeLength(minLength, maxLength, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return value.length >= minLength && value.length <= maxLength; | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.rangeLength; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(value) { | ||
return value.length >= minLength && value.length <= maxLength; | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
minLength: minLength, | ||
maxLength: maxLength | ||
} | ||
}); | ||
return this; | ||
@@ -139,6 +229,16 @@ }; | ||
_proto.minLength = function minLength(_minLength, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return Array.from(value).length >= _minLength; | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.minLength; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(value) { | ||
return Array.from(value).length >= _minLength; | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
minLength: _minLength | ||
} | ||
}); | ||
return this; | ||
@@ -148,6 +248,16 @@ }; | ||
_proto.maxLength = function maxLength(_maxLength, errorMessage) { | ||
_MixedType.prototype.pushRule.call(this, function (value) { | ||
return Array.from(value).length <= _maxLength; | ||
}, errorMessage); | ||
if (errorMessage === void 0) { | ||
errorMessage = this.locale.maxLength; | ||
} | ||
_MixedType.prototype.pushRule.call(this, { | ||
onValid: function onValid(value) { | ||
return Array.from(value).length <= _maxLength; | ||
}, | ||
errorMessage: errorMessage, | ||
params: { | ||
maxLength: _maxLength | ||
} | ||
}); | ||
return this; | ||
@@ -154,0 +264,0 @@ }; |
@@ -7,2 +7,3 @@ import { ArrayType } from './ArrayType'; | ||
import { ObjectType } from './ObjectType'; | ||
export declare type TypeName = 'array' | 'string' | 'boolean' | 'number' | 'object' | 'date'; | ||
export interface CheckResult<E = string> { | ||
@@ -14,3 +15,4 @@ hasError?: boolean; | ||
} | ||
export declare type ValidCallbackType<V, D, E> = (value: V, data?: D) => CheckResult<E> | boolean; | ||
export declare type ErrorMessageType = string; | ||
export declare type ValidCallbackType<V, D, E> = (value: V, data?: D, filedName?: string | string[]) => CheckResult<E> | boolean; | ||
export declare type PlainObject<T extends Record<string, unknown> = any> = { | ||
@@ -21,10 +23,12 @@ [P in keyof T]: T; | ||
onValid: ValidCallbackType<V, D, E>; | ||
errorMessage: E; | ||
errorMessage?: E; | ||
priority?: boolean; | ||
params?: any; | ||
} | ||
export declare type CheckType<X, T, ErrorMsgType = string> = X extends string ? StringType<T, ErrorMsgType> | DateType<T, ErrorMsgType> | NumberType<T, ErrorMsgType> : X extends number ? NumberType<T, ErrorMsgType> : X extends boolean ? BooleanType<T, ErrorMsgType> : X extends Date ? DateType<T, ErrorMsgType> : X extends Array<any> ? ArrayType<T, ErrorMsgType> : X extends Record<string, unknown> ? ObjectType<T, ErrorMsgType> : StringType<T, ErrorMsgType> | NumberType<T, ErrorMsgType> | BooleanType<T, ErrorMsgType> | ArrayType<T, ErrorMsgType> | DateType<T, ErrorMsgType> | ObjectType<T, ErrorMsgType>; | ||
export declare type SchemaDeclaration<T, ErrorMsgType = string> = { | ||
[P in keyof T]: CheckType<T[P], T, ErrorMsgType>; | ||
export declare type CheckType<X, T, E = ErrorMessageType> = X extends string ? StringType<T, E> | DateType<T, E> | NumberType<T, E> : X extends number ? NumberType<T, E> : X extends boolean ? BooleanType<T, E> : X extends Date ? DateType<T, E> : X extends Array<any> ? ArrayType<T, E> : X extends Record<string, unknown> ? ObjectType<T, E> : StringType<T, E> | NumberType<T, E> | BooleanType<T, E> | ArrayType<T, E> | DateType<T, E> | ObjectType<T, E>; | ||
export declare type SchemaDeclaration<T, E = string> = { | ||
[P in keyof T]: CheckType<T[P], T, E>; | ||
}; | ||
export declare type SchemaCheckResult<T, ErrorMsgType> = { | ||
[P in keyof T]: CheckResult<ErrorMsgType>; | ||
export declare type SchemaCheckResult<T, E> = { | ||
[P in keyof T]: CheckResult<E>; | ||
}; |
@@ -6,3 +6,3 @@ import { CheckResult, RuleType } from '../types'; | ||
*/ | ||
export declare function createValidator<V, D, E>(data?: D): (value: V, rules: RuleType<V, D, E>[]) => CheckResult<E> | null; | ||
export declare function createValidator<V, D, E>(data?: D, name?: string | string[]): (value: V, rules: RuleType<V, D, E>[]) => CheckResult<E> | null; | ||
export default createValidator; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
exports.__esModule = true; | ||
@@ -7,2 +9,6 @@ exports.createValidator = createValidator; | ||
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); | ||
var _formatErrorMessage = _interopRequireDefault(require("./formatErrorMessage")); | ||
/** | ||
@@ -12,3 +18,3 @@ * Create a data validator | ||
*/ | ||
function createValidator(data) { | ||
function createValidator(data, name) { | ||
return function (value, rules) { | ||
@@ -18,4 +24,5 @@ for (var i = 0; i < rules.length; i += 1) { | ||
onValid = _rules$i.onValid, | ||
errorMessage = _rules$i.errorMessage; | ||
var checkResult = onValid(value, data); | ||
errorMessage = _rules$i.errorMessage, | ||
params = _rules$i.params; | ||
var checkResult = onValid(value, data, name); | ||
@@ -25,3 +32,5 @@ if (checkResult === false) { | ||
hasError: true, | ||
errorMessage: errorMessage | ||
errorMessage: (0, _formatErrorMessage["default"])(errorMessage, (0, _extends2["default"])({}, params, { | ||
name: Array.isArray(name) ? name.join('.') : name | ||
})) | ||
}; | ||
@@ -28,0 +37,0 @@ } else if (typeof checkResult === 'object' && (checkResult.hasError || checkResult.array)) { |
@@ -6,3 +6,3 @@ import { CheckResult, RuleType } from '../types'; | ||
*/ | ||
export declare function createValidatorAsync<V, D, E>(data?: D): (value: V, rules: RuleType<V, D, E>[]) => Promise<CheckResult<E>>; | ||
export declare function createValidatorAsync<V, D, E>(data?: D, name?: string | string[]): (value: V, rules: RuleType<V, D, E>[]) => Promise<CheckResult<E>>; | ||
export default createValidatorAsync; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
exports.__esModule = true; | ||
@@ -7,2 +9,6 @@ exports.createValidatorAsync = createValidatorAsync; | ||
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); | ||
var _formatErrorMessage = _interopRequireDefault(require("./formatErrorMessage")); | ||
/** | ||
@@ -12,3 +18,3 @@ * Create a data asynchronous validator | ||
*/ | ||
function createValidatorAsync(data) { | ||
function createValidatorAsync(data, name) { | ||
function check(errorMessage) { | ||
@@ -32,4 +38,7 @@ return function (checkResult) { | ||
var onValid = rule.onValid, | ||
errorMessage = rule.errorMessage; | ||
return Promise.resolve(onValid(value, data)).then(check(errorMessage)); | ||
errorMessage = rule.errorMessage, | ||
params = rule.params; | ||
return Promise.resolve(onValid(value, data, name)).then(check((0, _formatErrorMessage["default"])(errorMessage, (0, _extends2["default"])({}, params, { | ||
name: Array.isArray(name) ? name.join('.') : name | ||
})))); | ||
}); | ||
@@ -36,0 +45,0 @@ return Promise.all(promises).then(function (results) { |
@@ -6,1 +6,2 @@ export { default as basicEmptyCheck } from './basicEmptyCheck'; | ||
export { default as isEmpty } from './isEmpty'; | ||
export { default as formatErrorMessage } from './formatErrorMessage'; |
@@ -6,3 +6,3 @@ "use strict"; | ||
exports.__esModule = true; | ||
exports.isEmpty = exports.createValidatorAsync = exports.createValidator = exports.checkRequired = exports.basicEmptyCheck = void 0; | ||
exports.formatErrorMessage = exports.isEmpty = exports.createValidatorAsync = exports.createValidator = exports.checkRequired = exports.basicEmptyCheck = void 0; | ||
@@ -27,2 +27,6 @@ var _basicEmptyCheck = _interopRequireDefault(require("./basicEmptyCheck")); | ||
exports.isEmpty = _isEmpty["default"]; | ||
exports.isEmpty = _isEmpty["default"]; | ||
var _formatErrorMessage = _interopRequireDefault(require("./formatErrorMessage")); | ||
exports.formatErrorMessage = _formatErrorMessage["default"]; |
{ | ||
"name": "schema-typed", | ||
"version": "2.0.0-alpha.2", | ||
"version": "2.0.0-alpha.3", | ||
"description": "Schema for data modeling & validation", | ||
@@ -15,3 +15,3 @@ "main": "lib/index.js", | ||
"test": "npm run lint && npm run test:once && npm run test:types", | ||
"test:once": "mocha", | ||
"test:once": "nyc --reporter=lcovonly --reporter=html mocha", | ||
"test:types": "dtslint --expectOnly --localTs node_modules/typescript/lib types", | ||
@@ -64,3 +64,3 @@ "doctoc:": "doctoc README.md", | ||
"conventional-changelog-cli": "^2.1.1", | ||
"coveralls": "^2.13.1", | ||
"coveralls": "^3.1.0", | ||
"cross-env": "^6.0.3", | ||
@@ -78,2 +78,3 @@ "del": "^6.0.0", | ||
"mocha": "^8.3.0", | ||
"nyc": "^15.1.0", | ||
"object-flaser": "^0.1.1", | ||
@@ -80,0 +81,0 @@ "prettier": "^2.2.1", |
@@ -5,3 +5,3 @@ # schema-typed | ||
[![npm][npm-badge]][npm] [![GitHub Actions][actions-svg]][actions-home] | ||
[![npm][npm-badge]][npm] [![GitHub Actions][actions-svg]][actions-home] [![Coverage Status][soverage-svg]][soverage] | ||
@@ -62,3 +62,3 @@ ## Table of Contents | ||
- [`unrepeatable(errorMessage?: string)`](#unrepeatableerrormessage-string) | ||
- [`of(type: object, errorMessage?: string)`](#oftype-object-errormessage-string) | ||
- [`of(type: object)`](#oftype-object) | ||
- [DateType(errorMessage?: string)](#datetypeerrormessage-string) | ||
@@ -659,3 +659,3 @@ - [`range(min: Date, max: Date, errorMessage?: string)`](#rangemin-date-max-date-errormessage-string) | ||
#### `of(type: object, errorMessage?: string)` | ||
#### `of(type: object)` | ||
@@ -726,1 +726,3 @@ ```js | ||
[actions-home]: https://github.com/rsuite/schema-typed/actions/workflows/nodejs-ci.yml | ||
[soverage-svg]: https://coveralls.io/repos/github/rsuite/schema-typed/badge.svg?branch=master | ||
[soverage]: https://coveralls.io/github/rsuite/schema-typed?branch=master |
135352
84
2979
725
38