Skip to content

Commit

Permalink
fix(validator): 重写部分实现
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jul 14, 2023
1 parent 36ad3d1 commit 8f68b9b
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 117 deletions.
81 changes: 0 additions & 81 deletions src/validator/yup.js
Original file line number Diff line number Diff line change
@@ -1,87 +1,6 @@
import { zhCN } from './locale/zhCN'
import * as yup from './yupSource'

// 实现 validateInOrder 方法
yup.addMethod(yup.object, 'validateInOrder', function (data, options) {
return Object.keys(data)
.reduce((prev, key) => {
return prev.then(() => {
let schema
try {
schema = yup.reach(this, key)
} catch (e) {}
return schema ? this.validateAt(key, data, options) : undefined
})
}, Promise.resolve())
.then(() => this.cast(data))
})

// 实现 validateInOrderSync 方法
yup.addMethod(yup.object, 'validateInOrderSync', function (data, options) {
for (const key of Object.keys(data)) {
let schema
try {
schema = yup.reach(this, key)
} catch (e) {}
if (schema) {
this.validateSyncAt(key, data, options)
}
}
return this.cast(data)
})

// 实现 validatePlus 方法
yup.addMethod(yup.mixed, 'validatePlus', function (data, options) {
return (
this.type === 'object'
? this.validateInOrder(data, options)
: this.validate(data, options)
)
.then(data => ({ data }))
.catch(error => ({ error, data }))
})

// 实现 validatePlusSync 方法
yup.addMethod(yup.mixed, 'validatePlusSync', function (data, options) {
try {
const _data =
this.type === 'object'
? this.validateInOrderSync(data, options)
: this.validateSync(data, options)
return { data: _data }
} catch (error) {
return { error, data }
}
})

// 支持函数参数增强类型支持
Object.keys(yup).forEach(key => {
if (
key === 'mixed' ||
key === 'string' ||
key === 'number' ||
key === 'bool' ||
key === 'boolean' ||
key === 'date' ||
key === 'object' ||
key === 'array'
) {
const raw = yup[key]
// eslint-disable-next-line no-import-assign
Object.defineProperty(yup, key, {
enumerable: true,
writable: false,
configurable: false,
value: payload => {
if (typeof payload === 'function') {
return payload(raw())
}
return raw(payload)
},
})
}
})

// 设置中文为默认语言
yup.setLocale(zhCN)

Expand Down
9 changes: 6 additions & 3 deletions src/validator/yupSource/array.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { array as locale } from './locale'
import MixedSchema from './mixed'
import inherits from './util/inherits'
import isAbsent from './util/isAbsent'
import isSchema from './util/isSchema'
import printValue from './util/printValue'
import MixedSchema from './mixed'
import { array as locale } from './locale'
import runTests from './util/runTests'

export default ArraySchema

function ArraySchema(type) {
if (!(this instanceof ArraySchema)) return new ArraySchema(type)
if (!(this instanceof ArraySchema))
return typeof type === 'function'
? type(new ArraySchema())
: new ArraySchema(type)

MixedSchema.call(this, { type: 'array' })

Expand Down
11 changes: 7 additions & 4 deletions src/validator/yupSource/boolean.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import inherits from './util/inherits'
import MixedSchema from './mixed'
import { boolean as locale } from './locale'
import MixedSchema from './mixed'
import inherits from './util/inherits'
import isAbsent from './util/isAbsent'

export default BooleanSchema

function BooleanSchema() {
if (!(this instanceof BooleanSchema)) return new BooleanSchema()
function BooleanSchema(payload) {
if (!(this instanceof BooleanSchema))
return typeof payload === 'function'
? payload(new BooleanSchema())
: new BooleanSchema()

MixedSchema.call(this, { type: 'boolean' })

Expand Down
13 changes: 8 additions & 5 deletions src/validator/yupSource/date.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import Ref from './Reference'
import { date as locale } from './locale'
import MixedSchema from './mixed'
import inherits from './util/inherits'
import isoParse from './util/isodate'
import { date as locale } from './locale'
import isAbsent from './util/isAbsent'
import Ref from './Reference'
import isoParse from './util/isodate'

let invalidDate = new Date('')

let isDate = obj => Object.prototype.toString.call(obj) === '[object Date]'

export default DateSchema

function DateSchema() {
if (!(this instanceof DateSchema)) return new DateSchema()
function DateSchema(payload) {
if (!(this instanceof DateSchema))
return typeof payload === 'function'
? payload(new DateSchema())
: new DateSchema()

MixedSchema.call(this, { type: 'date' })

Expand Down
20 changes: 10 additions & 10 deletions src/validator/yupSource/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import mixed from './mixed'
import Lazy from './Lazy'
import Ref from './Reference'
import ValidationError from './ValidationError'
import array from './array'
import bool from './boolean'
import string from './string'
import number from './number'
import date from './date'
import getLocale from './getLocale'
import mixed from './mixed'
import number from './number'
import object from './object'
import array from './array'
import Ref from './Reference'
import Lazy from './Lazy'
import ValidationError from './ValidationError'
import reach from './util/reach'
import isSchema from './util/isSchema'
import setLocale from './setLocale'
import getLocale from './getLocale'
import string from './string'
import isSchema from './util/isSchema'
import printValue from './util/printValue'
import reach from './util/reach'

let boolean = bool
let ref = (key, options) => new Ref(key, options)
Expand Down
27 changes: 26 additions & 1 deletion src/validator/yupSource/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ class RefSet {
}

export default function SchemaType(options = {}) {
if (!(this instanceof SchemaType)) return new SchemaType()
if (!(this instanceof SchemaType))
return typeof options === 'function'
? options(new SchemaType())
: new SchemaType()

this._deps = []
this._conditions = []
Expand Down Expand Up @@ -629,6 +632,28 @@ const proto = (SchemaType.prototype = {
},
})
},

// 新增
validatePlus(data, options) {
return (
this.type === 'object'
? this.validateInOrder(data, options)
: this.validate(data, options)
)
.then(data => ({ data }))
.catch(error => ({ error, data }))
},
validatePlusSync(data, options) {
try {
const _data =
this.type === 'object'
? this.validateInOrderSync(data, options)
: this.validateSync(data, options)
return { data: _data }
} catch (error) {
return { error, data }
}
},
})

for (const method of ['validate', 'validateSync'])
Expand Down
11 changes: 7 additions & 4 deletions src/validator/yupSource/number.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import inherits from './util/inherits'
import MixedSchema from './mixed'
import { number as locale } from './locale'
import MixedSchema from './mixed'
import inherits from './util/inherits'
import isAbsent from './util/isAbsent'

let isNaN = value => value != +value

export default function NumberSchema() {
if (!(this instanceof NumberSchema)) return new NumberSchema()
export default function NumberSchema(payload) {
if (!(this instanceof NumberSchema))
return typeof payload === 'function'
? payload(new NumberSchema())
: new NumberSchema()

MixedSchema.call(this, { type: 'number' })

Expand Down
35 changes: 33 additions & 2 deletions src/validator/yupSource/object.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { getter } from 'property-expr'
import { camelCase, has, mapKeys, mapValues, snakeCase } from '../../utils'

import { getter } from 'property-expr'
import { object as locale } from './locale.js'
import MixedSchema from './mixed'
import inherits from './util/inherits'
import reach from './util/reach'
import runTests from './util/runTests'
import sortByKeyOrder from './util/sortByKeyOrder'
import sortFields from './util/sortFields'
Expand All @@ -16,7 +17,10 @@ function unknown(ctx, value) {
}

export default function ObjectSchema(spec) {
if (!(this instanceof ObjectSchema)) return new ObjectSchema(spec)
if (!(this instanceof ObjectSchema))
return typeof spec === 'function'
? spec(new ObjectSchema())
: new ObjectSchema(spec)

MixedSchema.call(this, {
type: 'object',
Expand Down Expand Up @@ -347,4 +351,31 @@ inherits(ObjectSchema, MixedSchema, {
base.fields = mapValues(this.fields, value => value.describe())
return base
},

// 新增
validateInOrder(data, options) {
return Object.keys(data)
.reduce((prev, key) => {
return prev.then(() => {
let schema
try {
schema = reach(this, key)
} catch (e) {}
return schema ? this.validateAt(key, data, options) : undefined
})
}, Promise.resolve())
.then(() => this.cast(data))
},
validateInOrderSync(data, options) {
for (const key of Object.keys(data)) {
let schema
try {
schema = reach(this, key)
} catch (e) {}
if (schema) {
this.validateSyncAt(key, data, options)
}
}
return this.cast(data)
},
})
15 changes: 9 additions & 6 deletions src/validator/yupSource/string.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import inherits from './util/inherits'
import MixedSchema from './mixed'
import { string as locale } from './locale'
import isAbsent from './util/isAbsent'
import {
isChineseIDCardNumber,
isPossibleChineseMobilePhoneNumber,
} from '../../utils'
import { string as locale } from './locale'
import MixedSchema from './mixed'
import inherits from './util/inherits'
import isAbsent from './util/isAbsent'

// eslint-disable-next-line
let rEmail =
Expand All @@ -21,8 +21,11 @@ let isTrimmed = value => isAbsent(value) || value === value.trim()

let objStringTag = {}.toString()

export default function StringSchema() {
if (!(this instanceof StringSchema)) return new StringSchema()
export default function StringSchema(payload) {
if (!(this instanceof StringSchema))
return typeof payload === 'function'
? payload(new StringSchema())
: new StringSchema()

MixedSchema.call(this, { type: 'string' })

Expand Down
2 changes: 1 addition & 1 deletion src/validator/yupTypes/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ export interface ArraySchema<T extends any = any> extends MixedSchema<T[]> {
}

export declare function array<T extends any = any>(
payload?: (schema: ArraySchema<T>) => ArraySchema<T>,
type?: GetSchema<T> | ((schema: ArraySchema<T>) => ArraySchema<T>),
): ArraySchema<T>

0 comments on commit 8f68b9b

Please sign in to comment.