-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
35 changed files
with
2,527 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
node_modules | ||
lib | ||
dist | ||
/src/validator/yupSource/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import './yupTypes' | ||
import * as yup from 'yup/es' | ||
|
||
export { yup } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import * as yup from './yupSource' | ||
import { | ||
isChineseIDCardNumber, | ||
isPossibleChineseMobilePhoneNumber, | ||
} from '../utils' | ||
import { zhCN } from './locale/zhCN' | ||
|
||
// 实现 chineseMobilePhoneNumber 验证器 | ||
yup.addMethod( | ||
yup.string, | ||
'chineseMobilePhoneNumber', | ||
function (message = yup.getLocale().string.chineseMobilePhoneNumber) { | ||
return this.test( | ||
'chineseMobilePhoneNumber', | ||
message, | ||
isPossibleChineseMobilePhoneNumber, | ||
) | ||
}, | ||
) | ||
|
||
// 实现 chineseIDCardNumber 验证器 | ||
yup.addMethod( | ||
yup.string, | ||
'chineseIDCardNumber', | ||
function (message = yup.getLocale().string.chineseIDCardNumber) { | ||
return this.test('chineseIDCardNumber', message, isChineseIDCardNumber) | ||
}, | ||
) | ||
|
||
// 实现 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 } | ||
} | ||
}) | ||
|
||
// 设置中文为默认语言 | ||
yup.setLocale(zhCN) | ||
|
||
export { yup } |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { has } from '../../utils' | ||
import isSchema from './util/isSchema' | ||
|
||
class Condition { | ||
constructor(refs, options) { | ||
this.refs = refs | ||
|
||
if (typeof options === 'function') { | ||
this.fn = options | ||
return | ||
} | ||
|
||
if (!has(options, 'is')) | ||
throw new TypeError('`is:` is required for `when()` conditions') | ||
|
||
if (!options.then && !options.otherwise) | ||
throw new TypeError( | ||
'either `then:` or `otherwise:` is required for `when()` conditions', | ||
) | ||
|
||
let { is, then, otherwise } = options | ||
|
||
let check = | ||
typeof is === 'function' | ||
? is | ||
: (...values) => values.every(value => value === is) | ||
|
||
this.fn = function (...args) { | ||
let options = args.pop() | ||
let schema = args.pop() | ||
let branch = check(...args) ? then : otherwise | ||
|
||
if (!branch) return undefined | ||
if (typeof branch === 'function') return branch(schema) | ||
return schema.concat(branch.resolve(options)) | ||
} | ||
} | ||
|
||
resolve(base, options) { | ||
let values = this.refs.map(ref => | ||
ref.getValue(options?.value, options?.parent, options?.context), | ||
) | ||
|
||
let schema = this.fn.apply(base, values.concat(base, options)) | ||
|
||
if (schema === undefined || schema === base) return base | ||
|
||
if (!isSchema(schema)) | ||
throw new TypeError('conditions must return a schema object') | ||
|
||
return schema.resolve(options) | ||
} | ||
} | ||
|
||
export default Condition |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import isSchema from './util/isSchema' | ||
|
||
class Lazy { | ||
constructor(mapFn) { | ||
this._resolve = (value, options) => { | ||
let schema = mapFn(value, options) | ||
|
||
if (!isSchema(schema)) | ||
throw new TypeError('lazy() functions must return a valid schema') | ||
|
||
return schema.resolve(options) | ||
} | ||
} | ||
resolve(options) { | ||
return this._resolve(options.value, options) | ||
} | ||
cast(value, options) { | ||
return this._resolve(value, options).cast(value, options) | ||
} | ||
validate(value, options, maybeCb) { | ||
return this._resolve(value, options).validate(value, options, maybeCb) | ||
} | ||
validateSync(value, options) { | ||
return this._resolve(value, options).validateSync(value, options) | ||
} | ||
validateAt(path, value, options) { | ||
return this._resolve(value, options).validateAt(path, value, options) | ||
} | ||
validateSyncAt(path, value, options) { | ||
return this._resolve(value, options).validateSyncAt(path, value, options) | ||
} | ||
} | ||
|
||
Lazy.prototype.__isYupSchema__ = true | ||
|
||
export default Lazy |
Oops, something went wrong.