Skip to content

Commit

Permalink
[chore] clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Jul 20, 2016
1 parent 73858fe commit 0f43122
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 77 deletions.
15 changes: 3 additions & 12 deletions src/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import typeOf from 'type-name';
import inherits from './util/inherits';
import isAbsent from './util/isAbsent';
import isSchema from './util/isSchema';
import makePath from './util/makePath';
import MixedSchema from './mixed';
import { mixed, array as locale } from './locale.js';
import runValidations, { propagateErrors } from './util/runValidations';
Expand Down Expand Up @@ -70,7 +71,7 @@ inherits(ArraySchema, MixedSchema, {
}

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

// object._validate note for isStrict explanation
var innerOptions = {
Expand All @@ -96,23 +97,13 @@ inherits(ArraySchema, MixedSchema, {
})
},

// concat(schema) {
// var next = MixedSchema.prototype.concat.call(this, schema)
//
// next._subType = schema._subType === undefined
// ? this._subType
// : schema._subType;
//
// return next
// },

of(schema) {
var next = this.clone()

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

next._subType = schema;
Expand Down
45 changes: 22 additions & 23 deletions src/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ SchemaType.prototype = {
return result;
},

_cast(_value) {
let value = _value === undefined ? _value
_cast(rawValue) {
let value = rawValue === undefined ? rawValue
: this.transforms.reduce(
(value, transform) => transform.call(this, value, _value), _value)
(value, fn) => fn.call(this, value, rawValue), rawValue)

if (value === undefined && (has(this, '_default'))) {
if (value === undefined && has(this, '_default')) {
value = this.default()
}

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

//-- tests
_validate(_value, options = {}) {
let value = _value
, schema, endEarly, isStrict;
let value = _value;

schema = this
isStrict = this._option('strict', options)
endEarly = this._option('abortEarly', options)
let isStrict = this._option('strict', options)
let endEarly = this._option('abortEarly', options)

let path = options.path
let label = this._label

if (!isStrict) {

value = this._cast(value, { assert: false, ...options })
}
// value is cast, we can check if it meets type requirements
let validationParams = { value, path, schema: this, options, label }
let initialTests = []

if (schema._typeError)
if (this._typeError)
initialTests.push(this._typeError(validationParams));

if (this._whitelistError)
Expand Down Expand Up @@ -235,9 +233,13 @@ SchemaType.prototype = {

default(def) {
if (arguments.length === 0) {
var dflt = has(this, '_default') ? this._default : this._defaultDefault
return typeof dflt === 'function'
? dflt.call(this) : cloneDeep(dflt)
var defaultValue = has(this, '_default')
? this._default
: this._defaultDefault

return typeof defaultValue === 'function'
? defaultValue.call(this)
: cloneDeep(defaultValue)
}

var next = this.clone()
Expand Down Expand Up @@ -285,18 +287,18 @@ SchemaType.prototype = {
* the previous tests are removed and further tests of the same name will replace each other.
*/
test(name, message, test, useCallback) {
var opts = extractTestParams(name, message, test, useCallback)
let opts = extractTestParams(name, message, test, useCallback)
, next = this.clone();

var validate = createValidation(opts);
let validate = createValidation(opts);

var isExclusive = (
let isExclusive = (
opts.exclusive ||
(opts.name && next._exclusive[opts.name] === true)
)

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

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

next._typeError = createValidation({
message,
name: 'typeError',
message,
test(value) {
if (value !== undefined && !this.schema.isType(value))
return this.createError({
Expand All @@ -351,9 +353,6 @@ SchemaType.prototype = {
oneOf(enums, message = locale.oneOf) {
var next = this.clone();

if (next.tests.length)
throw new TypeError('Cannot specify values when there are validation rules specified')

enums.forEach(val => {
next._blacklist.delete(val)
next._whitelist.add(val)
Expand Down Expand Up @@ -439,7 +438,7 @@ Object.keys(aliases).forEach(method => {
})

function nodeify(promise, cb){
if(typeof cb !== 'function') return promise
if (typeof cb !== 'function') return promise

promise.then(
val => cb(null, val),
Expand Down
9 changes: 9 additions & 0 deletions src/util/makePath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

export default function makePath(strings, ...values) {
let path = strings.reduce((str, next) => {
let value = values.shift();
return str + (value == null ? '' : value) + next
})

return path.replace(/^\./, '');
}
26 changes: 15 additions & 11 deletions test/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ describe( 'Mixed Types ', function(){
])
})

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

return inst.cast().should.equal('hello')
return inst.cast(undefined).should.equal('hello')
})

it('should check types', async function(){
Expand Down Expand Up @@ -235,7 +235,8 @@ describe( 'Mixed Types ', function(){
.isValid(8).should.eventually.become(true)
})

it('tests should be called with the correct `this`', function(done){
it('tests should be called with the correct `this`', async () => {
let called = false;
var inst = object({
other: mixed(),
test: mixed().test({
Expand All @@ -246,12 +247,15 @@ describe( 'Mixed Types ', function(){
this.path.should.equal('test')
this.parent.should.eql({ other: 5, test: 'hi' })
this.options.context.should.eql({ user: 'jason' })
done()
called = true;
return true;
}
})
})

inst.validate({ other: 5, test: 'hi' }, { context: { user: 'jason' } })
await inst.validate({ other: 5, test: 'hi' }, { context: { user: 'jason' } })

called.should.equal(true)
})

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

it('should allow custom validation of either style', function(){
var inst = string()
.test('name', 'test a', function(val){
return Promise.resolve(val === 'jim')
})
.test('name', 'test b', function(val, done){
process.nextTick(function(){
.test('name', 'test a', val =>
Promise.resolve(val === 'jim')
)
.test('name', 'test b', (val, done) => {
process.nextTick(() =>
done(null, val !== 'jim')
})
)
}, true)

return Promise.all([
Expand Down
2 changes: 1 addition & 1 deletion test/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ describe('Object types', function(){

it('should camelCase with leading underscore', function(){
var inst = object().camelcase()

inst
.cast({ CON_STAT: 5, __isNew: true, __IS_FUN: true })
.should
Expand Down
30 changes: 0 additions & 30 deletions test/yup.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,34 +281,4 @@ describe('Yup', function(){
set.values().should.eql([2])
})
})

// it.only('should REACH with conditions', function(){
// var num = number()
// var altShape = {
// next: object().shape({
// greet: bool(),
// prop: number().when('greet', { is: true, then: number().max(5) })
// })
// }

// var inst = object().shape({
// num: number().max(4),
// nested: object()
// .when('num', { is: number().min(3), then: object(altShape) })
// .shape({
// next: object().shape({ prop: bool() })
// })
// })

// reach(inst, 'nested.arr[].num', { num: 1 }).should.equal(num)

// // reach(inst, 'nested.arr[1].num').should.equal(num)
// // reach(inst, 'nested.arr[1].num').should.not.equal(number())

// // reach(inst, 'nested.arr[].num').isValid(5, function(err, valid){
// // valid.should.equal(true)
// // done()
// // })
// })

})

0 comments on commit 0f43122

Please sign in to comment.