@aomex/core
Advanced tools
Comparing version 3.3.0 to 3.4.0
@@ -6,2 +6,13 @@ # Change Log | ||
# [3.4.0](https://github.com/aomex/aomex/compare/v3.3.0...v3.4.0) (2024-10-04) | ||
### Features | ||
* **core:** date-time规则增加解析格式 ([3b2ab20](https://github.com/aomex/aomex/commit/3b2ab20daccbd0a637f9e3950035d4e3bea018b5)) | ||
# [3.3.0](https://github.com/aomex/aomex/compare/v3.2.4...v3.3.0) (2024-09-26) | ||
@@ -8,0 +19,0 @@ |
@@ -55,15 +55,15 @@ import { OpenAPIV3 } from 'openapi-types'; | ||
declare namespace magistrate { | ||
type Result<T> = Ok<T> | Fail; | ||
interface Ok<T> { | ||
ok: T; | ||
declare namespace ValidateResult { | ||
type Any<T> = Accepted<T> | Denied; | ||
interface Accepted<T> { | ||
data: T; | ||
} | ||
interface Fail { | ||
interface Denied { | ||
errors: string[]; | ||
} | ||
} | ||
declare const magistrate: { | ||
ok: <T>(result: T) => magistrate.Ok<T>; | ||
fail: (message: string) => magistrate.Fail; | ||
noError: <T>(result: magistrate.Result<T>) => result is magistrate.Ok<T>; | ||
declare const ValidateResult: { | ||
accept: <T>(data: T) => ValidateResult.Accepted<T>; | ||
deny: (message: string) => ValidateResult.Denied; | ||
noError: <T>(result: ValidateResult.Any<T>) => result is ValidateResult.Accepted<T>; | ||
}; | ||
@@ -151,7 +151,7 @@ | ||
protected default(value: any): Validator; | ||
protected validate(value: any, key?: string, label?: string): Promise<magistrate.Result<any>>; | ||
protected validate(value: any, key?: string, label?: string): Promise<ValidateResult.Any<any>>; | ||
protected isEmpty(value: any): boolean; | ||
protected isValidNull(value: any): boolean; | ||
protected getDefaultValue<T = any>(value: any): T | undefined; | ||
protected abstract validateValue(value: any, key: string, label: string): Promise<magistrate.Result<any>> | magistrate.Result<any>; | ||
protected abstract validateValue(value: any, key: string, label: string): Promise<ValidateResult.Any<any>> | ValidateResult.Any<any>; | ||
protected copy(): Validator; | ||
@@ -184,4 +184,4 @@ protected copyConfig(prev: Validator): this; | ||
protected getTrimValue(value: string): string; | ||
protected validateValue(value: any, key: string, label: string): magistrate.Result<any> | Promise<magistrate.Result<any>>; | ||
protected abstract validateString(value: string, key: string, label: string): magistrate.Result<any> | Promise<magistrate.Result<any>>; | ||
protected validateValue(value: any, key: string, label: string): ValidateResult.Any<any> | Promise<ValidateResult.Any<any>>; | ||
protected abstract validateString(value: string, key: string, label: string): ValidateResult.Any<any> | Promise<ValidateResult.Any<any>>; | ||
protected copy: () => BaseStringValidator<T>; | ||
@@ -224,4 +224,4 @@ protected toDocument(): OpenAPIV3.SchemaObject; | ||
protected precision(decimals: number): this; | ||
protected validateValue(num: number, key: string, label: string): magistrate.Result<number>; | ||
protected abstract validateNumber(num: number, key: string, label: string): magistrate.Result<number>; | ||
protected validateValue(num: number, key: string, label: string): ValidateResult.Any<number>; | ||
protected abstract validateNumber(num: number, key: string, label: string): ValidateResult.Any<number>; | ||
protected copy: () => BaseNumberValidator<T>; | ||
@@ -231,5 +231,5 @@ protected toDocument(): OpenAPIV3.SchemaObject; | ||
declare class ValidatorError extends Error { | ||
protected readonly errors: magistrate.Fail['errors']; | ||
constructor(message: string | undefined, errors?: magistrate.Fail['errors']); | ||
declare class ValidateDeniedError extends Error { | ||
protected readonly errors: ValidateResult.Denied['errors']; | ||
constructor(message: string | undefined, errors?: ValidateResult.Denied['errors']); | ||
getValidateErrors(): string[]; | ||
@@ -256,3 +256,3 @@ } | ||
protected isEmpty(_: any): boolean; | ||
protected validateValue(value: any, key: string, label: string): Promise<magistrate.Result<any>>; | ||
protected validateValue(value: any, key: string, label: string): Promise<ValidateResult.Any<any>>; | ||
protected copyConfig(prev: AllOfValidator): this; | ||
@@ -271,3 +271,3 @@ protected toDocument(): OpenAPIV3.SchemaObject; | ||
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>; | ||
protected validateValue(value: any): magistrate.Result<any>; | ||
protected validateValue(value: any): ValidateResult.Any<any>; | ||
protected toDocument(): OpenAPIV3.SchemaObject; | ||
@@ -287,3 +287,3 @@ } | ||
protected isEmpty(_: any): boolean; | ||
protected validateValue(value: any, key: string, label: string): Promise<magistrate.Result<any>>; | ||
protected validateValue(value: any, key: string, label: string): Promise<ValidateResult.Any<any>>; | ||
protected copyConfig(prev: AnyOfValidator): this; | ||
@@ -333,3 +333,3 @@ protected toDocument(): OpenAPIV3.SchemaObject; | ||
length(range: ArrayValidator.LengthRange): this; | ||
protected validateValue(value: any, key: string, label: string): Promise<magistrate.Result<any[]>>; | ||
protected validateValue(value: any, key: string, label: string): Promise<ValidateResult.Any<any[]>>; | ||
protected copy: () => ArrayValidator<T>; | ||
@@ -361,3 +361,3 @@ protected toDocument(): OpenAPIV3.SchemaObject; | ||
max(max: bigint, inclusive?: boolean): this; | ||
protected validateValue(value: bigint, _key: string, label: string): magistrate.Result<bigint>; | ||
protected validateValue(value: bigint, _key: string, label: string): ValidateResult.Any<bigint>; | ||
protected copy: () => BigIntValidator<T>; | ||
@@ -398,3 +398,3 @@ protected toDocument(): OpenAPIV3.SchemaObject; | ||
setFalsyValues(values: any[]): BooleanValidator<T>; | ||
protected validateValue(value: any, _key: string, label: string): magistrate.Result<boolean>; | ||
protected validateValue(value: any, _key: string, label: string): ValidateResult.Any<boolean>; | ||
protected copy: () => BooleanValidator<T>; | ||
@@ -424,3 +424,3 @@ protected toDocument(): OpenAPIV3.SchemaObject; | ||
parseFrom(...encodings: BufferValidator.Encodings[]): this; | ||
protected validateValue(value: any, _key: string, label: string): magistrate.Result<Buffer>; | ||
protected validateValue(value: any, _key: string, label: string): ValidateResult.Any<Buffer>; | ||
protected copy: () => this; | ||
@@ -437,2 +437,3 @@ protected toDocument(): OpenAPIV3.SchemaObject; | ||
parseFromTimestamp?: boolean; | ||
formats?: string[]; | ||
} | ||
@@ -442,2 +443,3 @@ } | ||
protected config: DateTimeValidator.Options<T>; | ||
constructor(formats?: [string, ...string[]]); | ||
docs: (docs: Validator.PartialOpenAPISchema, mode?: Validator.DocumentMergeMode) => this; | ||
@@ -451,3 +453,5 @@ optional: () => DateTimeValidator<T | Validator.TOptional>; | ||
/** | ||
* 尝试把时间戳数字解析成时间对象。支持如下格式: | ||
* 尝试把时间戳数字解析成时间对象。默认已开启 | ||
* | ||
* 支持如下格式: | ||
* - 13位:1711257956199 | ||
@@ -458,3 +462,3 @@ * - 14位:1711257956.199 | ||
parseFromTimestamp(is?: boolean): this; | ||
protected validateValue(value: any, _key: string, label: string): magistrate.Result<Date>; | ||
protected validateValue(value: any, _key: string, label: string): ValidateResult.Any<Date>; | ||
protected toDate(value: any): false | Date; | ||
@@ -478,3 +482,3 @@ protected compare(date: Date): boolean; | ||
match: (pattern: RegExp) => this; | ||
protected validateString(email: string, _key: string, label: string): magistrate.Result<string>; | ||
protected validateString(email: string, _key: string, label: string): ValidateResult.Any<string>; | ||
protected toDocument(): OpenAPIV3.SchemaObject; | ||
@@ -501,3 +505,3 @@ } | ||
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>; | ||
protected validateValue(value: any, _key: string, label: string): magistrate.Result<any>; | ||
protected validateValue(value: any, _key: string, label: string): ValidateResult.Any<any>; | ||
protected toDocument(): OpenAPIV3.SchemaObject; | ||
@@ -539,3 +543,3 @@ } | ||
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>; | ||
protected validateString(hash: string, _key: string, label: string): magistrate.Result<string>; | ||
protected validateString(hash: string, _key: string, label: string): ValidateResult.Any<string>; | ||
protected toDocument(): OpenAPIV3.SchemaObject; | ||
@@ -550,3 +554,3 @@ } | ||
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>; | ||
protected validateNumber(num: number, _key: string, label: string): magistrate.Result<number>; | ||
protected validateNumber(num: number, _key: string, label: string): ValidateResult.Any<number>; | ||
protected toDocument(): OpenAPIV3.SchemaObject; | ||
@@ -570,3 +574,3 @@ } | ||
match: (pattern: RegExp) => this; | ||
protected validateString(ip: string, _key: string, label: string): magistrate.Result<string>; | ||
protected validateString(ip: string, _key: string, label: string): ValidateResult.Any<string>; | ||
protected toDocument(): OpenAPIV3.SchemaObject; | ||
@@ -586,3 +590,3 @@ } | ||
precision: (decimals: number) => this; | ||
protected validateNumber(num: number): magistrate.Result<number>; | ||
protected validateNumber(num: number): ValidateResult.Any<number>; | ||
protected copy: () => this; | ||
@@ -618,3 +622,3 @@ protected toDocument(): OpenAPIV3.SchemaObject; | ||
protected isPlainObject(value: any): value is object; | ||
protected validateValue(origin: Record<string, any>, key: string, label: string): Promise<magistrate.Result<object>>; | ||
protected validateValue(origin: Record<string, any>, key: string, label: string): Promise<ValidateResult.Any<object>>; | ||
protected copy: () => ObjectValidator<T>; | ||
@@ -636,3 +640,3 @@ protected copyConfig(prev: ObjectValidator): this; | ||
protected isEmpty(_: any): boolean; | ||
protected validateValue(value: any, key: string, label: string): Promise<magistrate.Result<any>>; | ||
protected validateValue(value: any, key: string, label: string): Promise<ValidateResult.Any<any>>; | ||
protected copyConfig(prev: OneOfValidator): this; | ||
@@ -652,3 +656,3 @@ protected toDocument(): OpenAPIV3.SchemaObject; | ||
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>; | ||
protected validateValue(value: stream$1.Stream, _key: string, label: string): magistrate.Result<stream$1.Stream>; | ||
protected validateValue(value: stream$1.Stream, _key: string, label: string): ValidateResult.Any<stream$1.Stream>; | ||
protected copy: () => this; | ||
@@ -678,3 +682,3 @@ protected toDocument(): OpenAPIV3.SchemaObject; | ||
protected isEmpty(value: any): boolean; | ||
protected validateString(value: string): magistrate.Result<string>; | ||
protected validateString(value: string): ValidateResult.Any<string>; | ||
protected copy: () => StringValidator<T>; | ||
@@ -695,3 +699,3 @@ protected toDocument(): OpenAPIV3.SchemaObject; | ||
transform: <T1>(fn: Validator.TransformFn<T, T1>) => TransformedValidator<T1>; | ||
protected validateString(ulid: string, _key: string, label: string): magistrate.Result<string>; | ||
protected validateString(ulid: string, _key: string, label: string): ValidateResult.Any<string>; | ||
protected toDocument(): OpenAPIV3.SchemaObject; | ||
@@ -717,3 +721,3 @@ } | ||
scheme(scheme: string[]): UrlValidator<T>; | ||
protected validateString(url: string, _key: string, label: string): magistrate.Result<string>; | ||
protected validateString(url: string, _key: string, label: string): ValidateResult.Any<string>; | ||
protected copy: () => UrlValidator<T>; | ||
@@ -742,3 +746,3 @@ protected toDocument(): OpenAPIV3.SchemaObject; | ||
match: (pattern: RegExp) => this; | ||
protected validateString(uuid: string, _key: string, label: string): magistrate.Result<string>; | ||
protected validateString(uuid: string, _key: string, label: string): ValidateResult.Any<string>; | ||
protected toDocument(): OpenAPIV3.SchemaObject; | ||
@@ -800,2 +804,8 @@ } | ||
dateTime(): DateTimeValidator<Date>; | ||
/** | ||
* @param format 设置解析字符串的格式,支持同时指定不同的格式并从左到右依次尝试。如果是标准的ISO时间格式或者时间戳则无需指定。 | ||
* | ||
* @link https://moment.github.io/luxon/#/formatting?id=table-of-tokens | ||
*/ | ||
dateTime(format: string, ...formats: string[]): DateTimeValidator<Date>; | ||
email(): EmailValidator<string>; | ||
@@ -873,3 +883,3 @@ enum<const T extends string | number | boolean>(ranges: T[]): EnumValidator<T>; | ||
declare const validate: <T extends ValidatorToken>(untrusted: any, validators: T, options?: { | ||
errorFormatter?: (errors: magistrate.Fail["errors"]) => string | Promise<string>; | ||
errorFormatter?: (errors: ValidateResult.Denied["errors"]) => string | Promise<string>; | ||
}) => Promise<Validator.Infer<T>>; | ||
@@ -906,2 +916,2 @@ | ||
export { AllOfValidator, AnyOfValidator, AnyValidator, ArrayValidator, BaseNumberValidator, BaseStringValidator, BigIntValidator, BooleanValidator, BufferValidator, type CombinedServices, type ComposeFn, DateTimeValidator, EmailValidator, EnumValidator, HashValidator, IntValidator, IpValidator, Middleware, type MiddlewarePlatform, MixinMiddleware, type Next, NumberValidator, ObjectValidator, OneOfValidator, Rule, Service, StreamValidator, StringValidator, type TransformedValidator, UlidValidator, UrlValidator, UuidValidator, Validator, ValidatorError, type ValidatorToken, combineServices, compose, magistrate, middleware, rule, toValidator, validate }; | ||
export { AllOfValidator, AnyOfValidator, AnyValidator, ArrayValidator, BaseNumberValidator, BaseStringValidator, BigIntValidator, BooleanValidator, BufferValidator, type CombinedServices, type ComposeFn, DateTimeValidator, EmailValidator, EnumValidator, HashValidator, IntValidator, IpValidator, Middleware, type MiddlewarePlatform, MixinMiddleware, type Next, NumberValidator, ObjectValidator, OneOfValidator, Rule, Service, StreamValidator, StringValidator, type TransformedValidator, UlidValidator, UrlValidator, UuidValidator, ValidateDeniedError, ValidateResult, Validator, type ValidatorToken, combineServices, compose, middleware, rule, toValidator, validate }; |
@@ -209,8 +209,8 @@ // src/interface/open-api.ts | ||
// src/validator/base/magistrate.ts | ||
var magistrate = { | ||
ok: (result) => { | ||
return { ok: result }; | ||
// src/validator/base/validate-result.ts | ||
var ValidateResult = { | ||
accept: (data) => { | ||
return { data }; | ||
}, | ||
fail: (message) => { | ||
deny: (message) => { | ||
return { | ||
@@ -221,3 +221,3 @@ errors: [message] | ||
noError: (result) => { | ||
return "ok" in result; | ||
return "data" in result; | ||
} | ||
@@ -330,10 +330,10 @@ }; | ||
if (!required) | ||
return magistrate.ok( | ||
return ValidateResult.accept( | ||
this.config.transform ? await this.config.transform(value) : value | ||
); | ||
return magistrate.fail(i18n.t("validator.required", { label })); | ||
return ValidateResult.deny(i18n.t("validator.required", { label })); | ||
} | ||
} | ||
if (this.isValidNull(value)) { | ||
return magistrate.ok( | ||
return ValidateResult.accept( | ||
this.config.transform ? await this.config.transform(value) : value | ||
@@ -343,4 +343,4 @@ ); | ||
const result = await this.validateValue(value, key, label); | ||
if (magistrate.noError(result) && this.config.transform) { | ||
result.ok = await this.config.transform(result.ok); | ||
if (ValidateResult.noError(result) && this.config.transform) { | ||
result.data = await this.config.transform(result.data); | ||
} | ||
@@ -402,3 +402,3 @@ return result; | ||
if (typeof value !== "string") { | ||
return magistrate.fail(i18n.t("validator.string.must_be_string", { label })); | ||
return ValidateResult.deny(i18n.t("validator.string.must_be_string", { label })); | ||
} | ||
@@ -410,7 +410,9 @@ value = this.getTrimValue(value); | ||
if (length < min || length > max) { | ||
return magistrate.fail(i18n.t("validator.string.length_not_in_range", { label })); | ||
return ValidateResult.deny( | ||
i18n.t("validator.string.length_not_in_range", { label }) | ||
); | ||
} | ||
} | ||
if (pattern2 && !pattern2.test(value)) { | ||
return magistrate.fail(i18n.t("validator.string.not_match_pattern", { label })); | ||
return ValidateResult.deny(i18n.t("validator.string.not_match_pattern", { label })); | ||
} | ||
@@ -476,3 +478,3 @@ return this.validateString(value, key, label); | ||
if (!Number.isFinite(num)) { | ||
return magistrate.fail(i18n.t("validator.number.must_be_number", { label })); | ||
return ValidateResult.deny(i18n.t("validator.number.must_be_number", { label })); | ||
} | ||
@@ -483,3 +485,3 @@ if (precision !== void 0) { | ||
if ((minInclusive ? num < min : num <= min) || (maxInclusive ? num > max : num >= max)) { | ||
return magistrate.fail(i18n.t("validator.number.not_in_range", { label })); | ||
return ValidateResult.deny(i18n.t("validator.number.not_in_range", { label })); | ||
} | ||
@@ -500,4 +502,4 @@ return this.validateNumber(num, key, label); | ||
// src/validator/base/validator-error.ts | ||
var ValidatorError = class extends Error { | ||
// src/validator/base/validate-denied-error.ts | ||
var ValidateDeniedError = class extends Error { | ||
constructor(message, errors = []) { | ||
@@ -526,9 +528,9 @@ super(message); | ||
const result = await validator.validate(value, key, label); | ||
if (magistrate.noError(result)) { | ||
value = result.ok; | ||
if (ValidateResult.noError(result)) { | ||
value = result.data; | ||
} else { | ||
return magistrate.fail(i18n.t("validator.all_of.not_match_all", { label })); | ||
return ValidateResult.deny(i18n.t("validator.all_of.not_match_all", { label })); | ||
} | ||
} | ||
return magistrate.ok(value); | ||
return ValidateResult.accept(value); | ||
} | ||
@@ -551,3 +553,3 @@ copyConfig(prev) { | ||
validateValue(value) { | ||
return magistrate.ok(value); | ||
return ValidateResult.accept(value); | ||
} | ||
@@ -583,8 +585,8 @@ toDocument() { | ||
const result = await validator.validate(value, key, label); | ||
if (magistrate.noError(result)) { | ||
value = result.ok; | ||
if (ValidateResult.noError(result)) { | ||
value = result.data; | ||
matched = true; | ||
} | ||
} | ||
return matched ? magistrate.ok(value) : magistrate.fail(i18n.t("validator.any_of.not_match_rule", { label })); | ||
return matched ? ValidateResult.accept(value) : ValidateResult.deny(i18n.t("validator.any_of.not_match_rule", { label })); | ||
} | ||
@@ -644,7 +646,9 @@ copyConfig(prev) { | ||
if (!Array.isArray(items)) { | ||
return magistrate.fail(i18n.t("validator.array.must_be_array", { label })); | ||
return ValidateResult.deny(i18n.t("validator.array.must_be_array", { label })); | ||
} | ||
const length = items.length; | ||
if (length < min || length > max) { | ||
return magistrate.fail(i18n.t("validator.array.length_not_in_range", { label })); | ||
return ValidateResult.deny( | ||
i18n.t("validator.array.length_not_in_range", { label }) | ||
); | ||
} | ||
@@ -662,4 +666,4 @@ if (itemValidator) { | ||
); | ||
if (magistrate.noError(result)) { | ||
arr[index] = result.ok; | ||
if (ValidateResult.noError(result)) { | ||
arr[index] = result.data; | ||
} else { | ||
@@ -672,3 +676,3 @@ error.errors = error.errors.concat(result.errors); | ||
} | ||
return magistrate.ok(items); | ||
return ValidateResult.accept(items); | ||
} | ||
@@ -718,3 +722,3 @@ toDocument() { | ||
if (num === void 0) { | ||
return magistrate.fail(i18n.t("validator.number.must_be_bigint", { label })); | ||
return ValidateResult.deny(i18n.t("validator.number.must_be_bigint", { label })); | ||
} | ||
@@ -725,5 +729,5 @@ } else { | ||
if ((minInclusive ? num < min : num <= min) || (maxInclusive ? num > max : num >= max)) { | ||
return magistrate.fail(i18n.t("validator.number.not_in_range", { label })); | ||
return ValidateResult.deny(i18n.t("validator.number.not_in_range", { label })); | ||
} | ||
return magistrate.ok(num); | ||
return ValidateResult.accept(num); | ||
} | ||
@@ -780,8 +784,8 @@ toDocument() { | ||
if (trueValues.includes(value)) { | ||
return magistrate.ok(true); | ||
return ValidateResult.accept(true); | ||
} | ||
if (falseValues.includes(value)) { | ||
return magistrate.ok(false); | ||
return ValidateResult.accept(false); | ||
} | ||
return magistrate.fail(i18n.t("validator.boolean.must_be_boolean", { label })); | ||
return ValidateResult.deny(i18n.t("validator.boolean.must_be_boolean", { label })); | ||
} | ||
@@ -839,5 +843,5 @@ toDocument() { | ||
if (!buffer) { | ||
return magistrate.fail(i18n.t("validator.buffer.must_be_buffer", { label })); | ||
return ValidateResult.deny(i18n.t("validator.buffer.must_be_buffer", { label })); | ||
} | ||
return magistrate.ok(buffer); | ||
return ValidateResult.accept(buffer); | ||
} | ||
@@ -858,4 +862,10 @@ toDocument() { | ||
// src/validator/validators/date-time.ts | ||
var unixTimeWithMS = /^[0-9]{10}\.[0-9]{3}$/; | ||
import { DateTime } from "luxon"; | ||
var regTimestamp = /^[0-9.]+$/; | ||
var regUnixTimeWithMS = /^[0-9]{10}\.[0-9]{3}$/; | ||
var DateTimeValidator = class extends Validator { | ||
constructor(formats) { | ||
super(); | ||
this.config.formats = formats; | ||
} | ||
min(freshDate, inclusive = true) { | ||
@@ -874,3 +884,5 @@ const validator = this.copy(); | ||
/** | ||
* 尝试把时间戳数字解析成时间对象。支持如下格式: | ||
* 尝试把时间戳数字解析成时间对象。默认已开启 | ||
* | ||
* 支持如下格式: | ||
* - 13位:1711257956199 | ||
@@ -888,21 +900,32 @@ * - 14位:1711257956.199 | ||
if (date === false || date.toString() === "Invalid Date") { | ||
return magistrate.fail(i18n.t("validator.dateTime.must_be_date", { label })); | ||
return ValidateResult.deny(i18n.t("validator.dateTime.must_be_date", { label })); | ||
} | ||
if (!this.compare(date)) { | ||
return magistrate.fail(i18n.t("validator.dateTime.not_in_range", { label })); | ||
return ValidateResult.deny(i18n.t("validator.dateTime.not_in_range", { label })); | ||
} | ||
return magistrate.ok(date); | ||
return ValidateResult.accept(date); | ||
} | ||
toDate(value) { | ||
const { parseFromTimestamp } = this.config; | ||
const { parseFromTimestamp = true, formats } = this.config; | ||
if (value instanceof Date) return value; | ||
if (typeof value === "string") return new Date(value); | ||
if (parseFromTimestamp && typeof value === "number") { | ||
if (parseFromTimestamp && (typeof value === "number" || typeof value === "string" && regTimestamp.test(value))) { | ||
const timestamp = value.toString(); | ||
if (timestamp.length === 10) return new Date(Number(value + "000")); | ||
if (timestamp.length === 13) return new Date(value); | ||
if (timestamp.length === 14 && unixTimeWithMS.test(timestamp)) { | ||
return new Date(Number(timestamp.replace(".", ""))); | ||
if (timestamp.length === 10) | ||
return DateTime.fromSeconds(Number(timestamp)).toJSDate(); | ||
if (timestamp.length === 13) | ||
return DateTime.fromMillis(Number(timestamp)).toJSDate(); | ||
if (timestamp.length === 14 && regUnixTimeWithMS.test(timestamp)) { | ||
return DateTime.fromSeconds(Number(timestamp)).toJSDate(); | ||
} | ||
} | ||
if (typeof value === "string") { | ||
if (formats && formats.length) { | ||
for (const format of formats) { | ||
const date = DateTime.fromFormat(value, format, { setZone: true }); | ||
if (date.isValid) return date.toJSDate(); | ||
} | ||
} else { | ||
return DateTime.fromISO(value).toJSDate(); | ||
} | ||
} | ||
return false; | ||
@@ -936,5 +959,5 @@ } | ||
if (!emailValidator.validate(email)) { | ||
return magistrate.fail(i18n.t("validator.string.must_be_email", { label })); | ||
return ValidateResult.deny(i18n.t("validator.string.must_be_email", { label })); | ||
} | ||
return magistrate.ok(email); | ||
return ValidateResult.accept(email); | ||
} | ||
@@ -965,3 +988,3 @@ toDocument() { | ||
const { ranges, strict } = this.config; | ||
if (ranges.includes(value)) return magistrate.ok(value); | ||
if (ranges.includes(value)) return ValidateResult.accept(value); | ||
if (!strict && typeof value === "string") { | ||
@@ -973,6 +996,6 @@ const num = Number(value); | ||
); | ||
if (matched) return magistrate.ok(matched); | ||
if (matched) return ValidateResult.accept(matched); | ||
} | ||
} | ||
return magistrate.fail(i18n.t("validator.enum.not_in_range", { label })); | ||
return ValidateResult.deny(i18n.t("validator.enum.not_in_range", { label })); | ||
} | ||
@@ -1027,5 +1050,5 @@ toDocument() { | ||
if (!_HashValidator.algorithmPattern[length].test(hash)) { | ||
return magistrate.fail(i18n.t("validator.string.must_be_hash", { label })); | ||
return ValidateResult.deny(i18n.t("validator.string.must_be_hash", { label })); | ||
} | ||
return magistrate.ok(hash); | ||
return ValidateResult.accept(hash); | ||
} | ||
@@ -1048,5 +1071,5 @@ toDocument() { | ||
if (!Number.isInteger(num)) { | ||
return magistrate.fail(i18n.t("validator.number.must_be_integer", { label })); | ||
return ValidateResult.deny(i18n.t("validator.number.must_be_integer", { label })); | ||
} | ||
return magistrate.ok(num); | ||
return ValidateResult.accept(num); | ||
} | ||
@@ -1081,3 +1104,3 @@ toDocument() { | ||
if (!valid) { | ||
return magistrate.fail( | ||
return ValidateResult.deny( | ||
i18n.t("validator.string.must_be_ip", { | ||
@@ -1089,3 +1112,3 @@ label, | ||
} | ||
return magistrate.ok(ip); | ||
return ValidateResult.accept(ip); | ||
} | ||
@@ -1104,3 +1127,3 @@ toDocument() { | ||
validateNumber(num) { | ||
return magistrate.ok(num); | ||
return ValidateResult.accept(num); | ||
} | ||
@@ -1145,3 +1168,3 @@ toDocument() { | ||
if (!valid) { | ||
return magistrate.fail(i18n.t("validator.object.must_be_object", { label })); | ||
return ValidateResult.deny(i18n.t("validator.object.must_be_object", { label })); | ||
} | ||
@@ -1161,4 +1184,4 @@ } | ||
); | ||
if (magistrate.noError(result)) { | ||
obj[propKey] = result.ok; | ||
if (ValidateResult.noError(result)) { | ||
obj[propKey] = result.data; | ||
} else { | ||
@@ -1173,3 +1196,3 @@ error.errors = error.errors.concat(result.errors); | ||
} | ||
return magistrate.ok(obj); | ||
return ValidateResult.accept(obj); | ||
} | ||
@@ -1215,7 +1238,7 @@ copyConfig(prev) { | ||
const result = await validator.validate(value, key, label); | ||
if (magistrate.noError(result)) { | ||
if (ValidateResult.noError(result)) { | ||
if (!matched) { | ||
matched = result; | ||
} else { | ||
return magistrate.fail( | ||
return ValidateResult.deny( | ||
i18n.t("validator.one_of.match_multiple_rule", { label }) | ||
@@ -1226,3 +1249,3 @@ ); | ||
} | ||
return matched || magistrate.fail(i18n.t("validator.one_of.not_match_rule", { label })); | ||
return matched || ValidateResult.deny(i18n.t("validator.one_of.not_match_rule", { label })); | ||
} | ||
@@ -1247,5 +1270,5 @@ copyConfig(prev) { | ||
if (!(value instanceof stream.Stream)) { | ||
return magistrate.fail(i18n.t("validator.stream.must_be_stream", { label })); | ||
return ValidateResult.deny(i18n.t("validator.stream.must_be_stream", { label })); | ||
} | ||
return magistrate.ok(value); | ||
return ValidateResult.accept(value); | ||
} | ||
@@ -1275,3 +1298,3 @@ toDocument() { | ||
validateString(value) { | ||
return magistrate.ok(value); | ||
return ValidateResult.accept(value); | ||
} | ||
@@ -1288,5 +1311,5 @@ toDocument() { | ||
if (!pattern.test(ulid)) { | ||
return magistrate.fail(i18n.t("validator.string.must_be_ulid", { label })); | ||
return ValidateResult.deny(i18n.t("validator.string.must_be_ulid", { label })); | ||
} | ||
return magistrate.ok(ulid); | ||
return ValidateResult.accept(ulid); | ||
} | ||
@@ -1319,3 +1342,3 @@ toDocument() { | ||
if (!/^[a-z0-9]+:\/\/.+/.test(url) || !URL.canParse(url)) { | ||
return magistrate.fail(i18n.t("validator.url.must_be_url", { label })); | ||
return ValidateResult.deny(i18n.t("validator.url.must_be_url", { label })); | ||
} | ||
@@ -1325,7 +1348,7 @@ const parsedUrl = new URL(url); | ||
if (!allowedScheme.includes(scheme)) { | ||
return magistrate.fail( | ||
return ValidateResult.deny( | ||
i18n.t("validator.url.unsupported_scheme", { label, scheme }) | ||
); | ||
} | ||
return magistrate.ok(url); | ||
return ValidateResult.accept(url); | ||
} | ||
@@ -1380,3 +1403,3 @@ toDocument() { | ||
if (!valid) { | ||
return magistrate.fail( | ||
return ValidateResult.deny( | ||
i18n.t("validator.string.must_be_uuid", { | ||
@@ -1388,3 +1411,3 @@ label, | ||
} | ||
return magistrate.ok(uuid); | ||
return ValidateResult.accept(uuid); | ||
} | ||
@@ -1463,4 +1486,4 @@ toDocument() { | ||
} | ||
dateTime() { | ||
return new DateTimeValidator(); | ||
dateTime(format, ...formats) { | ||
return new DateTimeValidator(format ? [format, ...formats] : void 0); | ||
} | ||
@@ -1548,5 +1571,5 @@ email() { | ||
const trusted = await validator["validate"](source); | ||
if (magistrate.noError(trusted)) return trusted.ok; | ||
if (ValidateResult.noError(trusted)) return trusted.data; | ||
const msg = await errorFormatter(trusted.errors); | ||
throw new ValidatorError(msg, trusted.errors); | ||
throw new ValidateDeniedError(msg, trusted.errors); | ||
}; | ||
@@ -1629,7 +1652,7 @@ var defaultErrorFormatter = (errors) => { | ||
UuidValidator, | ||
ValidateDeniedError, | ||
ValidateResult, | ||
Validator, | ||
ValidatorError, | ||
combineServices, | ||
compose, | ||
magistrate, | ||
middleware, | ||
@@ -1636,0 +1659,0 @@ rule, |
{ | ||
"name": "@aomex/core", | ||
"version": "3.3.0", | ||
"version": "3.4.0", | ||
"description": "aomex核心库", | ||
@@ -38,6 +38,10 @@ "type": "module", | ||
"email-validator": "^2.0.4", | ||
"luxon": "^3.5.0", | ||
"openapi-types": "^12.1.3", | ||
"@aomex/internal-tools": "^3.3.0" | ||
"@aomex/internal-tools": "^3.4.0" | ||
}, | ||
"devDependencies": { | ||
"@types/luxon": "^3.4.2" | ||
}, | ||
"scripts": {} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
201803
2428
7
1
+ Addedluxon@^3.5.0
+ Addedluxon@3.5.0(transitive)
Updated@aomex/internal-tools@^3.4.0