Skip to content

Commit 0f43122

Browse files
committed
[chore] clean up code
1 parent 73858fe commit 0f43122

File tree

6 files changed

+50
-77
lines changed

6 files changed

+50
-77
lines changed

src/array.js

+3-12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import typeOf from 'type-name';
33
import inherits from './util/inherits';
44
import isAbsent from './util/isAbsent';
55
import isSchema from './util/isSchema';
6+
import makePath from './util/makePath';
67
import MixedSchema from './mixed';
78
import { mixed, array as locale } from './locale.js';
89
import runValidations, { propagateErrors } from './util/runValidations';
@@ -70,7 +71,7 @@ inherits(ArraySchema, MixedSchema, {
7071
}
7172

7273
let validations = value.map((item, idx) => {
73-
var path = (options.path || '') + '[' + idx + ']'
74+
var path = makePath`${options.path}[${idx}]`
7475

7576
// object._validate note for isStrict explanation
7677
var innerOptions = {
@@ -96,23 +97,13 @@ inherits(ArraySchema, MixedSchema, {
9697
})
9798
},
9899

99-
// concat(schema) {
100-
// var next = MixedSchema.prototype.concat.call(this, schema)
101-
//
102-
// next._subType = schema._subType === undefined
103-
// ? this._subType
104-
// : schema._subType;
105-
//
106-
// return next
107-
// },
108-
109100
of(schema) {
110101
var next = this.clone()
111102

112103
if (schema !== false && !isSchema(schema))
113104
throw new TypeError(
114105
'`array.of()` sub-schema must be a valid yup schema, or `false` to negate a current sub-schema. ' +
115-
'got: ' + typeOf(schema) + ' instead'
106+
'not: ' + typeOf(schema)
116107
)
117108

118109
next._subType = schema;

src/mixed.js

+22-23
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,12 @@ SchemaType.prototype = {
155155
return result;
156156
},
157157

158-
_cast(_value) {
159-
let value = _value === undefined ? _value
158+
_cast(rawValue) {
159+
let value = rawValue === undefined ? rawValue
160160
: this.transforms.reduce(
161-
(value, transform) => transform.call(this, value, _value), _value)
161+
(value, fn) => fn.call(this, value, rawValue), rawValue)
162162

163-
if (value === undefined && (has(this, '_default'))) {
163+
if (value === undefined && has(this, '_default')) {
164164
value = this.default()
165165
}
166166

@@ -176,26 +176,24 @@ SchemaType.prototype = {
176176
return nodeify(schema._validate(value, options), cb)
177177
},
178178

179-
//-- tests
180179
_validate(_value, options = {}) {
181-
let value = _value
182-
, schema, endEarly, isStrict;
180+
let value = _value;
183181

184-
schema = this
185-
isStrict = this._option('strict', options)
186-
endEarly = this._option('abortEarly', options)
182+
let isStrict = this._option('strict', options)
183+
let endEarly = this._option('abortEarly', options)
187184

188185
let path = options.path
189186
let label = this._label
190187

191188
if (!isStrict) {
189+
192190
value = this._cast(value, { assert: false, ...options })
193191
}
194192
// value is cast, we can check if it meets type requirements
195193
let validationParams = { value, path, schema: this, options, label }
196194
let initialTests = []
197195

198-
if (schema._typeError)
196+
if (this._typeError)
199197
initialTests.push(this._typeError(validationParams));
200198

201199
if (this._whitelistError)
@@ -235,9 +233,13 @@ SchemaType.prototype = {
235233

236234
default(def) {
237235
if (arguments.length === 0) {
238-
var dflt = has(this, '_default') ? this._default : this._defaultDefault
239-
return typeof dflt === 'function'
240-
? dflt.call(this) : cloneDeep(dflt)
236+
var defaultValue = has(this, '_default')
237+
? this._default
238+
: this._defaultDefault
239+
240+
return typeof defaultValue === 'function'
241+
? defaultValue.call(this)
242+
: cloneDeep(defaultValue)
241243
}
242244

243245
var next = this.clone()
@@ -285,18 +287,18 @@ SchemaType.prototype = {
285287
* the previous tests are removed and further tests of the same name will replace each other.
286288
*/
287289
test(name, message, test, useCallback) {
288-
var opts = extractTestParams(name, message, test, useCallback)
290+
let opts = extractTestParams(name, message, test, useCallback)
289291
, next = this.clone();
290292

291-
var validate = createValidation(opts);
293+
let validate = createValidation(opts);
292294

293-
var isExclusive = (
295+
let isExclusive = (
294296
opts.exclusive ||
295297
(opts.name && next._exclusive[opts.name] === true)
296298
)
297299

298300
if (opts.exclusive && !opts.name) {
299-
throw new TypeError('You cannot have an exclusive validation without a `name`')
301+
throw new TypeError('Exclusive tests must provide a unique `name` identifying the test')
300302
}
301303

302304
next._exclusive[opts.name] = !!opts.exclusive
@@ -333,8 +335,8 @@ SchemaType.prototype = {
333335
var next = this.clone()
334336

335337
next._typeError = createValidation({
336-
message,
337338
name: 'typeError',
339+
message,
338340
test(value) {
339341
if (value !== undefined && !this.schema.isType(value))
340342
return this.createError({
@@ -351,9 +353,6 @@ SchemaType.prototype = {
351353
oneOf(enums, message = locale.oneOf) {
352354
var next = this.clone();
353355

354-
if (next.tests.length)
355-
throw new TypeError('Cannot specify values when there are validation rules specified')
356-
357356
enums.forEach(val => {
358357
next._blacklist.delete(val)
359358
next._whitelist.add(val)
@@ -439,7 +438,7 @@ Object.keys(aliases).forEach(method => {
439438
})
440439

441440
function nodeify(promise, cb){
442-
if(typeof cb !== 'function') return promise
441+
if (typeof cb !== 'function') return promise
443442

444443
promise.then(
445444
val => cb(null, val),

src/util/makePath.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
export default function makePath(strings, ...values) {
3+
let path = strings.reduce((str, next) => {
4+
let value = values.shift();
5+
return str + (value == null ? '' : value) + next
6+
})
7+
8+
return path.replace(/^\./, '');
9+
}

test/mixed.js

+15-11
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ describe( 'Mixed Types ', function(){
2828
])
2929
})
3030

31-
it('cast should return a default is empty', function(){
31+
it('cast should return a default when undefined', function(){
3232
var inst = mixed().default('hello')
3333

34-
return inst.cast().should.equal('hello')
34+
return inst.cast(undefined).should.equal('hello')
3535
})
3636

3737
it('should check types', async function(){
@@ -235,7 +235,8 @@ describe( 'Mixed Types ', function(){
235235
.isValid(8).should.eventually.become(true)
236236
})
237237

238-
it('tests should be called with the correct `this`', function(done){
238+
it('tests should be called with the correct `this`', async () => {
239+
let called = false;
239240
var inst = object({
240241
other: mixed(),
241242
test: mixed().test({
@@ -246,12 +247,15 @@ describe( 'Mixed Types ', function(){
246247
this.path.should.equal('test')
247248
this.parent.should.eql({ other: 5, test: 'hi' })
248249
this.options.context.should.eql({ user: 'jason' })
249-
done()
250+
called = true;
251+
return true;
250252
}
251253
})
252254
})
253255

254-
inst.validate({ other: 5, test: 'hi' }, { context: { user: 'jason' } })
256+
await inst.validate({ other: 5, test: 'hi' }, { context: { user: 'jason' } })
257+
258+
called.should.equal(true)
255259
})
256260

257261
it('tests can return an error', function(){
@@ -290,13 +294,13 @@ describe( 'Mixed Types ', function(){
290294

291295
it('should allow custom validation of either style', function(){
292296
var inst = string()
293-
.test('name', 'test a', function(val){
294-
return Promise.resolve(val === 'jim')
295-
})
296-
.test('name', 'test b', function(val, done){
297-
process.nextTick(function(){
297+
.test('name', 'test a', val =>
298+
Promise.resolve(val === 'jim')
299+
)
300+
.test('name', 'test b', (val, done) => {
301+
process.nextTick(() =>
298302
done(null, val !== 'jim')
299-
})
303+
)
300304
}, true)
301305

302306
return Promise.all([

test/object.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ describe('Object types', function(){
663663

664664
it('should camelCase with leading underscore', function(){
665665
var inst = object().camelcase()
666-
666+
667667
inst
668668
.cast({ CON_STAT: 5, __isNew: true, __IS_FUN: true })
669669
.should

test/yup.js

-30
Original file line numberDiff line numberDiff line change
@@ -281,34 +281,4 @@ describe('Yup', function(){
281281
set.values().should.eql([2])
282282
})
283283
})
284-
285-
// it.only('should REACH with conditions', function(){
286-
// var num = number()
287-
// var altShape = {
288-
// next: object().shape({
289-
// greet: bool(),
290-
// prop: number().when('greet', { is: true, then: number().max(5) })
291-
// })
292-
// }
293-
294-
// var inst = object().shape({
295-
// num: number().max(4),
296-
// nested: object()
297-
// .when('num', { is: number().min(3), then: object(altShape) })
298-
// .shape({
299-
// next: object().shape({ prop: bool() })
300-
// })
301-
// })
302-
303-
// reach(inst, 'nested.arr[].num', { num: 1 }).should.equal(num)
304-
305-
// // reach(inst, 'nested.arr[1].num').should.equal(num)
306-
// // reach(inst, 'nested.arr[1].num').should.not.equal(number())
307-
308-
// // reach(inst, 'nested.arr[].num').isValid(5, function(err, valid){
309-
// // valid.should.equal(true)
310-
// // done()
311-
// // })
312-
// })
313-
314284
})

0 commit comments

Comments
 (0)