Skip to content

Commit

Permalink
fix: from()
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Sep 11, 2018
1 parent 01189ee commit 298484f
Show file tree
Hide file tree
Showing 10 changed files with 450 additions and 275 deletions.
29 changes: 26 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@

parser: babel-eslint
extends: jason
{
"parser": "babel-eslint",
"extends": ["jason", "prettier"],
"overrides": [
{
"files": ["test/**"],
"plugins": ["jest"],
"env": {
"jest/globals": true
},
"globals": {
"TestHelpers": false,
"sinon": true
},
"rules": {
"global-require": "off",
"no-await-in-loop": "off",
"jest/no-disabled-tests": "warn",
"jest/no-focused-tests": "error",
"jest/no-identical-title": "error",
"jest/prefer-to-have-length": "warn",
"jest/valid-expect": "off"
}
}
]
}
9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
]
},
"devDependencies": {
"@4c/rollout": "^1.1.0",
"@babel/cli": "7.0.0",
"@babel/core": "7.0.0",
"babel-core": "^7.0.0-bridge.0",
Expand All @@ -66,15 +67,14 @@
"doctoc": "^1.3.1",
"eslint": "^4.19.1",
"eslint-config-jason": "^4.0.1",
"eslint-config-prettier": "^3.0.1",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-jest": "^21.22.0",
"eslint-plugin-react": "^7.11.1",
"husky": "^0.14.3",
"jest": "^22.4.3",
"lint-staged": "^7.2.2",
"mt-changelog": "^0.6.2",
"prettier": "^1.13.7",
"promises-aplus-tests": "^2.1.2",
"release-script": "^1.0.2",
"rollup": "^0.65.0",
"rollup-plugin-babel": "^4.0.2",
"rollup-plugin-filesize": "^4.0.1",
Expand All @@ -90,8 +90,5 @@
"property-expr": "^1.5.0",
"synchronous-promise": "^2.0.5",
"toposort": "^2.0.2"
},
"release-script": {
"defaultDryRun": "false"
}
}
17 changes: 8 additions & 9 deletions src/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import runValidations, { propagateErrors } from './util/runValidations';
let isObject = obj => Object.prototype.toString.call(obj) === '[object Object]';

function unknown(ctx, value) {
var known = Object.keys(ctx.fields);
let known = Object.keys(ctx.fields);
return Object.keys(value).filter(key => known.indexOf(key) === -1);
}

Expand Down Expand Up @@ -67,14 +67,15 @@ inherits(ObjectSchema, MixedSchema, {
},

_cast(_value, options = {}) {
var value = MixedSchema.prototype._cast.call(this, _value, options);
let value = MixedSchema.prototype._cast.call(this, _value, options);

//should ignore nulls here
if (value === undefined) return this.default();

if (!this._typeCheck(value)) return value;

let fields = this.fields;

let strip = this._option('stripUnknown', options) === true;
let props = this._nodes.concat(
Object.keys(value).filter(v => this._nodes.indexOf(v) === -1),
Expand Down Expand Up @@ -182,8 +183,8 @@ inherits(ObjectSchema, MixedSchema, {
},

shape(schema, excludes = []) {
var next = this.clone(),
fields = Object.assign(next.fields, schema);
let next = this.clone();
let fields = Object.assign(next.fields, schema);

next.fields = fields;

Expand All @@ -204,13 +205,11 @@ inherits(ObjectSchema, MixedSchema, {
let fromGetter = getter(from, true);

return this.transform(obj => {
var newObj = obj;

if (obj == null) return obj;

let newObj = obj;
if (has(obj, from)) {
newObj = { ...obj };
if (!alias) delete obj[from];
if (!alias) delete newObj[from];

newObj[to] = fromGetter(obj);
}
Expand All @@ -225,7 +224,7 @@ inherits(ObjectSchema, MixedSchema, {
noAllow = true;
}

var next = this.test({
let next = this.test({
name: 'noUnknown',
exclusive: true,
message: message,
Expand Down
7 changes: 0 additions & 7 deletions test/.eslintrc

This file was deleted.

2 changes: 1 addition & 1 deletion test/bool.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('Boolean types', () => {
.should.equal(true);
});

it.only('bool should VALIDATE correctly', () => {
it('bool should VALIDATE correctly', () => {
let inst = bool().required();

return Promise.all([
Expand Down
2 changes: 1 addition & 1 deletion test/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('Mixed Types ', () => {
inst.getDefault().should.equal('hi');
});

it('getDefault should return the default value', function() {
it('getDefault should return the default value using context', function() {
let inst = string().when('$foo', {
is: 'greet',
then: string().default('hi'),
Expand Down
24 changes: 11 additions & 13 deletions test/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ describe('Object types', () => {
});
});

it.only('should call shape with constructed with an arg', () => {
it('should call shape with constructed with an arg', () => {
let inst = object({
prop: mixed(),
});
Expand Down Expand Up @@ -567,18 +567,6 @@ describe('Object types', () => {
.should.eql({ myProp: 5, other: 6, Other: 6 });
});

it('should move nested keys', () => {
let inst = object({
foo: object({
bar: string(),
}),
}).from('foo.bar', 'foobar');

inst
.cast({ foo: { bar: 'quz', foof: 5 } })
.should.eql({ foobar: 'quz', foo: { foof: 5 } });
});

it('should alias nested keys', () => {
let inst = object({
foo: object({
Expand Down Expand Up @@ -776,4 +764,14 @@ describe('Object types', () => {

expect(inst.nullable().cast(null)).to.equal(null);
});

xit('should handle invalid shapes better', async () => {
var schema = object().shape({
permissions: undefined,
});

expect(
await schema.isValid({ permissions: [] }, { abortEarly: false }),
).to.equal(true);
});
});
3 changes: 2 additions & 1 deletion test/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ describe('String types', () => {
.cast('HellO JohN')
.should.equal('hello john');
});
it('should transform to lowercase', () => {

it('should transform to uppercase', () => {
schema
.uppercase()
.cast('HellO JohN')
Expand Down
1 change: 0 additions & 1 deletion test/yup.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ describe('Yup', function() {
it('should do settled', function() {
return Promise.all([
settled([Promise.resolve('hi'), Promise.reject('error')])
.catch(err => console.log(err))
.should.be.fulfilled()
.then(function(results) {
results.length.should.equal(2);
Expand Down
Loading

0 comments on commit 298484f

Please sign in to comment.