Skip to content

Commit

Permalink
Accept options to validate specific guid versions
Browse files Browse the repository at this point in the history
  • Loading branch information
talyssonoc committed Feb 22, 2019
1 parent 70d37cd commit e473f6e
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 30 deletions.
2 changes: 1 addition & 1 deletion docs/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ 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 (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({
Expand Down
2 changes: 1 addition & 1 deletion new-docs/validation/string-validations.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
- `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 (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({
Expand Down
3 changes: 2 additions & 1 deletion 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,7 +13,7 @@ module.exports = {
['lowerCase', 'lowercase'],
['upperCase', 'uppercase'],
['email', 'email'],
['guid', 'guid'],
['guid', 'guid', isPlainObject],
['required', 'required']
],
createJoiSchema(typeDescriptor) {
Expand Down
18 changes: 10 additions & 8 deletions src/validation/utils.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
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(passValueToJoi && (!isFunction(passValueToJoi) || passValueToJoi(attributeDescriptor))) {
return joiSchema[joiMethod](attributeDescriptor);
}

return joiSchema[joiMethod]();
Expand All @@ -25,15 +27,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
79 changes: 60 additions & 19 deletions test/unit/validation/string.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,34 +445,75 @@ describe('validation', () => {
});

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

beforeEach(() => {
User = attributes({
id: {
type: String,
guid: true
}
})(class 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'
context('when value is a valid guid', () => {
it('is valid', () => {
const user = new User({
id: '759535af-3314-4ace-81b9-a519c29d0e17'
});

assertValid(user);
});
});

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 value is not a valid guid', () => {
it('is not valid and has errors set', () => {
const user = new User({
id: 'Not a valid guid'
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);
});
});

assertInvalid(user, 'id');
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');
});
});
});
});
Expand Down

0 comments on commit e473f6e

Please sign in to comment.