Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove set and use lodash's clone #109

Merged
merged 2 commits into from
Aug 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,13 @@ json separate from validating it, via the `cast` method.
npm install -S yup
```

**Yup always relies on the `Promise` global object to handle asynchronous values.
If your environment doesn't have Promise, you'll need to include a polyfill.**
Yup always relies on the `Promise` global object to handle asynchronous values as well `Set`.
For browsers that do not support these, you'll need to include a polyfill, such as core-js:

```js
import 'core-js/es6/promise';
import 'core-js/es6/set';
```

## Usage

Expand Down Expand Up @@ -734,7 +739,7 @@ v.isValid('nope').should.eventually().equal(false)

#### `string.matches(regex: Regex, options: { message: string, excludeEmptyString: bool }): Schema`

An alternate signature for `string.matches` with an options object. `excludeEmptyString`, when true,
An alternate signature for `string.matches` with an options object. `excludeEmptyString`, when true,
short circuits the regex test when the value is an empty string

```javascript
Expand Down
30 changes: 18 additions & 12 deletions src/mixed.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import has from 'lodash/has';
import cloneDeepWith from 'lodash/cloneDeepWith';
import toArray from 'lodash/toArray';

import { mixed as locale } from './locale';
import Condition from './Condition';
import runValidations from './util/runValidations';
import merge from './util/merge';
import isSchema from './util/isSchema';
import isAbsent from './util/isAbsent';
import cloneDeep from './util/clone';
import createValidation from './util/createValidation';
import printValue from './util/printValue';
import BadSet from './util/set';
import Ref from './Reference';

let notEmpty = value => !isAbsent(value);
Expand Down Expand Up @@ -41,8 +42,8 @@ export default function SchemaType(options = {}){
this._conditions = []
this._options = { abortEarly: true, recursive: true }
this._exclusive = Object.create(null)
this._whitelist = new BadSet()
this._blacklist = new BadSet()
this._whitelist = new Set()
this._blacklist = new Set()
this.tests = []
this.transforms = []

Expand All @@ -66,7 +67,12 @@ SchemaType.prototype = {
if (this._mutate)
return this;

return cloneDeep(this);
// if the nested value is a schema we can skip cloning, since
// they are already immutable
return cloneDeepWith(this, (value) => {
if (isSchema(value) && value !== this)
return value
});
},

label(label) {
Expand Down Expand Up @@ -233,7 +239,7 @@ SchemaType.prototype = {

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

var next = this.clone()
Expand Down Expand Up @@ -348,7 +354,8 @@ SchemaType.prototype = {
var next = this.clone();

enums.forEach(val => {
next._blacklist.delete(val)
if (next._blacklist.has(val))
next._blacklist.delete(val)
next._whitelist.add(val)
})

Expand All @@ -357,10 +364,10 @@ SchemaType.prototype = {
name: 'oneOf',
test(value) {
let valids = this.schema._whitelist
if (valids.length && !(value === undefined || valids.has(value)))
if (valids.size && !(value === undefined || valids.has(value)))
return this.createError({
params: {
values: valids.values().join(', ')
values: toArray(valids).join(', ')
}
})
return true
Expand All @@ -383,10 +390,10 @@ SchemaType.prototype = {
name: 'notOneOf',
test(value) {
let invalids = this.schema._blacklist
if (invalids.length && invalids.has(value))
if (invalids.size && invalids.has(value))
return this.createError({
params: {
values: invalids.values().join(', ')
values: toArray(invalids).join(', ')
}
})
return true
Expand Down Expand Up @@ -430,4 +437,3 @@ Object.keys(aliases).forEach(method => {
SchemaType.prototype[alias] = SchemaType.prototype[method]
)
})

69 changes: 0 additions & 69 deletions src/util/clone.js

This file was deleted.

33 changes: 0 additions & 33 deletions src/util/set.js

This file was deleted.

3 changes: 2 additions & 1 deletion src/util/sortFields.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import toposort from 'toposort';
import has from 'lodash/has';
import toposort from 'toposort';
import { split } from 'property-expr';

import Ref from '../Reference';
import isSchema from './isSchema';

Expand Down
2 changes: 1 addition & 1 deletion test/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import reach from '../src/util/reach';

let noop = () => {}

describe( 'Mixed Types ', function(){
describe('Mixed Types ', () => {

it('should be immutable', () => {
let inst = mixed(), next;
Expand Down
129 changes: 0 additions & 129 deletions test/yup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import reach from '../src/util/reach';
import BadSet from '../src/util/set';
import merge from '../src/util/merge';
import { settled } from '../src/util/runValidations';

Expand Down Expand Up @@ -150,132 +149,4 @@ describe('Yup', function(){
err.message.should.match(/must be a `number` type/)
})

describe('BadSet', function(){
it('should preserve primitive types', function(){
var set = new BadSet()

set.add(2)
set.has(2).should.be.true()
set.has('2').should.be.false()
set.values().should.eql([2])

set.add('3')
set.has('3').should.be.true()
set.has(3).should.be.false()
set.values().should.eql([2, '3'])

set.add(false)
set.has(false).should.be.true()
set.has('false').should.be.false()
set.values().should.eql([2, '3', false])

set.add('true')
set.has('true').should.be.true()
set.has(true).should.be.false()
set.values().should.eql([2, '3', false, 'true'])

set.add(null)
set.has(null).should.be.true()
set.has('null').should.be.false()
set.values().should.eql([2, '3', false, 'true', null])

set.add(undefined)
set.has(undefined).should.be.true()
set.has('undefined').should.be.false()
set.values().should.eql([2, '3', false, 'true', null, undefined])
})

it('should perform value equality for arrays and objects', function(){
var set = new BadSet()

var oneTwoThree = [1, '2', 3]
set.add(oneTwoThree)
set.has(oneTwoThree).should.be.true()
set.has([1, '2', 3]).should.be.true()
set.values().should.eql([[1, '2', 3]])
set.length.should.equal(1)

set.add([1, '2', 3])
set.has(oneTwoThree).should.be.true()
set.has([1, '2', 3]).should.be.true()
set.values().should.eql([[1, '2', 3]])
set.length.should.equal(1)

var aOnebTwo = { a: 1, b: '2'}
set.add(aOnebTwo)
set.has(aOnebTwo).should.be.true()
set.has({ a: 1, b: '2'}).should.be.true()
set.values().should.eql([[1, '2', 3], { a: 1, b: '2'}])
set.length.should.equal(2)

set.add({ a: 1, b: '2'})
set.has(aOnebTwo).should.be.true()
set.has({ a: 1, b: '2'}).should.be.true()
set.values().should.eql([[1, '2', 3], { a: 1, b: '2'}])
set.length.should.equal(2)
})

it('should perform value equality for dates', function(){
var set = new BadSet()

var someDate = new Date('12-12-12')
set.add(someDate)
set.has(someDate).should.be.true()
set.has(new Date('12-12-12')).should.be.true()
set.values().should.eql([new Date('12-12-12')])
set.length.should.equal(1)

set.add(new Date('12-12-12'))
set.has(someDate).should.be.true()
set.has(new Date('12-12-12')).should.be.true()
set.values().should.eql([new Date('12-12-12')])
set.length.should.equal(1)
})

it('should not contain the same value twice', function(){
var set = new BadSet()

var arrayWithDuplicates = [
1,
2,
3,
'2',
3,
'abc',
new Date('12-12-12'),
4,
new Date('12-12-12')
]

arrayWithDuplicates.forEach(item => set.add(item))
set.values().sort().should.eql(
[1, 2, 3, '2', 'abc', new Date('12-12-12'), 4].sort())
})

it('should delete values', function(){
var set = new BadSet()

set.add(2)
set.has(2).should.be.true()
set.length.should.equal(1)
set.values().should.eql([2])

set.delete('2')
set.has(2).should.be.true()
set.length.should.equal(1)
set.values().should.eql([2])

set.add('3')
set.has(2).should.be.true()
set.has('3').should.be.true()
set.length.should.equal(2)
set.values().should.eql([2, '3'])

set.delete('3')
set.has(2).should.be.true()
set.has('3').should.be.false()
set.length.should.equal(1)
set.values().should.eql([2])
})
})
})