Skip to content

Commit

Permalink
Merge baeafac into d2a5ad4
Browse files Browse the repository at this point in the history
  • Loading branch information
talyssonoc committed Feb 22, 2019
2 parents d2a5ad4 + baeafac commit 43784a2
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 8 deletions.
5 changes: 5 additions & 0 deletions docs/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,14 @@ Validations marked with __**__ accept a value or an [attribute reference](#attri
- `lowerCase`: all characters must be lower cased
- `upperCase`: all characters must be upper cased
- `email`: is a valid email (default: `false`)
- `guid`: is a valid guid. You can pass a boolean or the [options object accepted by Joi](https://github.com/hapijs/joi/blob/v10.2.0/API.md#stringguid---aliases-uuid) (default: `false`)

```javascript
const User = attributes({
id: {
type: String,
guid: true
},
initials: {
type: String,
upperCase: true,
Expand Down
5 changes: 5 additions & 0 deletions new-docs/validation/string-validations.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@
- `lowerCase`: all characters must be lower cased
- `upperCase`: all characters must be upper cased
- `email`: is a valid email (default: `false`)
- `guid`: is a valid guid. You can pass a boolean or the [options object accepted by Joi](https://github.com/hapijs/joi/blob/v10.2.0/API.md#stringguid---aliases-uuid) (default: `false`)

```javascript
const User = attributes({
id: {
type: String,
guid: true
},
initials: {
type: String,
upperCase: true,
Expand Down
2 changes: 2 additions & 0 deletions src/validation/string.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const joi = require('joi');
const { isPlainObject } = require('lodash');
const { mapToJoi, equalOption } = require('./utils');

module.exports = {
Expand All @@ -12,6 +13,7 @@ module.exports = {
['lowerCase', 'lowercase'],
['upperCase', 'uppercase'],
['email', 'email'],
['guid', 'guid', isPlainObject],
['required', 'required']
],
createJoiSchema(typeDescriptor) {
Expand Down
22 changes: 14 additions & 8 deletions src/validation/utils.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
const joi = require('joi');
const { isPlainObject } = require('lodash');
const { isPlainObject, isFunction } = require('lodash');

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

if(attributeDescriptor === undefined) {
return joiSchema;
}

if(passValueToJoi) {
return joiSchema[joiMethod](typeDescriptor[optionName]);
if(shouldPassValueToJoi(passValueToJoi, attributeDescriptor)) {
return joiSchema[joiMethod](attributeDescriptor);
}

return joiSchema[joiMethod]();
}, initial);
};

function shouldPassValueToJoi(passValueToJoi, attributeDescriptor) {
return passValueToJoi && (!isFunction(passValueToJoi) || passValueToJoi(attributeDescriptor));
}

function mapValueOrReference(valueOrReference) {
if(isPlainObject(valueOrReference)) {
return joi.ref(valueOrReference.attr);
Expand All @@ -25,15 +31,15 @@ function mapValueOrReference(valueOrReference) {

exports.mapToJoiWithReference = function mapToJoiWithReference(typeDescriptor, { initial, mappings }) {
return mappings.reduce((joiSchema, [optionName, joiMethod]) => {
var optionValue = typeDescriptor[optionName];
var attributeDescriptor = typeDescriptor[optionName];

if(optionValue === undefined) {
if(attributeDescriptor === undefined) {
return joiSchema;
}

optionValue = mapValueOrReference(optionValue);
attributeDescriptor = mapValueOrReference(attributeDescriptor);

return joiSchema[joiMethod](optionValue);
return joiSchema[joiMethod](attributeDescriptor);
}, initial);
};

Expand Down
74 changes: 74 additions & 0 deletions test/unit/validation/string.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,5 +443,79 @@ describe('validation', () => {
});
});
});

describe('guid', () => {
context('when validating as a generic guid', () => {
var User;

beforeEach(() => {
User = attributes({
id: {
type: String,
guid: true
}
})(class User {});
});

context('when value is a valid guid', () => {
it('is valid', () => {
const user = new User({
id: '759535af-3314-4ace-81b9-a519c29d0e17'
});

assertValid(user);
});
});

context('when value is not a valid guid', () => {
it('is not valid and has errors set', () => {
const user = new User({
id: 'Not a valid guid'
});

assertInvalid(user, 'id');
});
});
});

context('when validating a specific guid version', () => {
var User;

beforeEach(() => {
User = attributes({
id: {
type: String,
guid: {
version: ['uuidv4']
}
}
})(class User {});
});

context('when value is a valid guid', () => {
it('is valid', () => {
const uuidv4 = 'f35e1cf1-4ac9-4fbb-9c06-151dc8ff9107';

const user = new User({
id: uuidv4
});

assertValid(user);
});
});

context('when value is not a valid guid', () => {
it('is not valid and has errors set', () => {
const uuidv1 = 'c130564e-36d9-11e9-b210-d663bd873d93';

const user = new User({
id: uuidv1
});

assertInvalid(user, 'id');
});
});
});
});
});
});

0 comments on commit 43784a2

Please sign in to comment.