Skip to content

Commit

Permalink
Merge 8d0d7ce into 9279bae
Browse files Browse the repository at this point in the history
  • Loading branch information
loweoj committed Nov 16, 2018
2 parents 9279bae + 8d0d7ce commit 0f07b19
Show file tree
Hide file tree
Showing 9 changed files with 466 additions and 186 deletions.
472 changes: 287 additions & 185 deletions dist/structure.js

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion src/validation/utils.js
@@ -1,9 +1,12 @@
const joi = require('joi');
const { isPlainObject } = require('lodash');

const notRequired = ({ optionName, typeDescriptor }) =>
optionName === 'required' && typeDescriptor[optionName] === false;

exports.mapToJoi = function mapToJoi(typeDescriptor, { initial, mappings }) {
return mappings.reduce((joiSchema, [optionName, joiMethod, passValueToJoi]) => {
if(typeDescriptor[optionName] === undefined) {
if (typeDescriptor[optionName] === undefined || notRequired({ optionName, typeDescriptor})) {
return joiSchema;
}

Expand Down
32 changes: 32 additions & 0 deletions test/unit/validation/array.spec.js
Expand Up @@ -70,6 +70,38 @@ describe('validation', () => {
});
});

describe('not required', () => {
var User;

beforeEach(() => {
User = attributes({
books: {
type: Array,
itemType: String,
required: false
}
})(class User {});
});

context('when value is present', () => {
it('is valid', () => {
const user = new User();

assertValid(user);
});
});

context('when value is not present', () => {
it('is valid', () => {
const user = new User({
books: undefined
});

assertValid(user, 'books');
});
});
});

describe('sparse array', () => {
context('when array can not be sparse', () => {
var User;
Expand Down
21 changes: 21 additions & 0 deletions test/unit/validation/boolean.spec.js
Expand Up @@ -68,6 +68,27 @@ describe('validation', () => {
});
});

describe('not required', () => {
var User;

beforeEach(() => {
User = attributes({
isAdmin: {
type: Boolean,
required: false
}
})(class User {});
});

context('when value is not present', () => {
it('is valid', () => {
const user = new User();

assertValid(user);
});
});
});

describe('equal', () => {
var User;

Expand Down
21 changes: 21 additions & 0 deletions test/unit/validation/date.spec.js
Expand Up @@ -68,6 +68,27 @@ describe('validation', () => {
});
});

describe('not required', () => {
var User;

beforeEach(() => {
User = attributes({
birth: {
type: Date,
required: false
}
})(class User {});
});

context('when value is not present', () => {
it('is valid', () => {
const user = new User();

assertValid(user);
});
});
});

describe('equal', () => {
var now;
var User;
Expand Down
25 changes: 25 additions & 0 deletions test/unit/validation/nestedPojo.spec.js
Expand Up @@ -73,5 +73,30 @@ describe('validation', () => {
});
});
});

describe('not required', () => {
var Location;
var User;

beforeEach(() => {
Location = class Location {};

User = attributes({
lastLocation: {
type: Location,
required: false
}
})(class User {});
});

context('when value is not present', () => {
it('is valid', () => {
const user = new User();

assertValid(user);
});
});
});

});
});
31 changes: 31 additions & 0 deletions test/unit/validation/nestedStructure.spec.js
Expand Up @@ -88,6 +88,37 @@ describe('validation', () => {
});
});

describe('not required', () => {
var Location;
var User;

beforeEach(() => {
Location = attributes({
x: {
type: Number
},
y: {
type: Number
}
})(class Location {});

User = attributes({
lastLocation: {
type: Location,
required: false
}
})(class User {});
});

context('when value is not present', () => {
it('is valid', () => {
const user = new User();

assertValid(user);
});
});
});

describe('nested required', () => {
var Location;
var User;
Expand Down
22 changes: 22 additions & 0 deletions test/unit/validation/number.spec.js
Expand Up @@ -68,6 +68,28 @@ describe('validation', () => {
});
});


describe('not required', () => {
var User;

beforeEach(() => {
User = attributes({
age: {
type: Number,
required: false
}
})(class User {});
});

context('when value is not present', () => {
it('is valid', () => {
const user = new User();

assertValid(user);
});
});
});

describe('equal', () => {
describe('when using a value', () => {
var User;
Expand Down
23 changes: 23 additions & 0 deletions test/unit/validation/string.spec.js
Expand Up @@ -68,6 +68,29 @@ describe('validation', () => {
});
});

describe('not required', () => {
var User;

beforeEach(() => {
User = attributes({
name: {
type: String,
required: false
}
})(class User {});
});

context('when value is not present', () => {
it('is valid', () => {
const user = new User({
name: undefined
});

assertValid(user);
});
});
});

describe('equal', () => {
var User;

Expand Down

0 comments on commit 0f07b19

Please sign in to comment.