Skip to content

Commit

Permalink
Fix linter errors, bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
James Carmichael committed Nov 26, 2020
1 parent 6554871 commit 6adaf88
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 84 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@envage/water-abstraction-helpers",
"version": "4.3.3",
"version": "4.3.4",
"description": "Contains functions for processing water abstraction data which are shared across multiple projects",
"main": "./src/index.js",
"scripts": {
Expand Down
16 changes: 8 additions & 8 deletions src/validators/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
const { isString } = require('lodash');

const extension = joi => ({
base: joi.string().allow(null),
name: 'nullableString',
coerce: function (value, state, options) {
if (isString(value) && value.trim() === '') {
return null;
}
return value;
}
base: joi.string().allow(null),
name: 'nullableString',
coerce: function (value, state, options) {
if (isString(value) && value.trim() === '') {
return null;
}
return value;
}
});

module.exports = extension;
2 changes: 1 addition & 1 deletion src/validators/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

/**
* @module validators/index.js
* @module validators/index.js
* Provides some common validators used throughout the services
*/

Expand Down
16 changes: 5 additions & 11 deletions test/validators/extensions.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,38 @@
'use strict';

const { expect } = require('@hapi/code');
const { over } = require('lodash');
const { omit } = require('lodash');
const { experiment, test, beforeEach, afterEach } = exports.lab = require('@hapi/lab').script();
const { experiment, test, beforeEach } = exports.lab = require('@hapi/lab').script();

const Joi = require('@hapi/joi').extend(
require('../../src/validators/extensions')
);


experiment('validators/extensions.js', () => {
experiment('.convertEmptyStringToNull', () => {

let schema;

beforeEach(async () => {
schema = Joi.nullableString();
});

test('converts an empty string to null', async () => {
const { error, value } = Joi.validate('', schema);
const { value } = Joi.validate('', schema);
expect(value).to.be.null();
});

test('converts a padded empty string to null', async () => {
const { error, value } = Joi.validate(' ', schema);
const { value } = Joi.validate(' ', schema);
expect(value).to.be.null();
});

test('leaves other strings unchanged', async () => {
const { error, value } = Joi.validate('Hello', schema);
const { value } = Joi.validate('Hello', schema);
expect(value).to.equal('Hello');
});

test('accepts null', async () => {
const { error, value } = Joi.validate(null, schema);
const { value } = Joi.validate(null, schema);
expect(value).to.be.null();
});


});
});
84 changes: 21 additions & 63 deletions test/validators/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

const Joi = require('@hapi/joi');
const { expect } = require('@hapi/code');
const { over } = require('lodash');
const { omit } = require('lodash');
const { experiment, test, beforeEach, afterEach } = exports.lab = require('@hapi/lab').script();
const { experiment, test } = exports.lab = require('@hapi/lab').script();

const { VALID_ADDRESS } = require('../../src/validators');

Expand All @@ -23,21 +22,18 @@ const createAddress = (overrides = {}) => ({
});

experiment('validators', () => {

experiment('VALID_ADDRESS', () => {

test('can be a string', async () => {
const { error } = Joi.validate(createAddress(), VALID_ADDRESS);
expect(error).to.be.null();
});

experiment('.addressLine1', () => {

test('can be named .address1', async () => {
const address = {
...omit(createAddress(), 'addressLine1'),
address1: 'Flat 123'
}
};
const { error } = Joi.validate(address, VALID_ADDRESS);
expect(error).to.be.null();
});
Expand All @@ -64,14 +60,12 @@ experiment('validators', () => {
});
});


experiment('.addressLine2', () => {

test('can be named .address2', async () => {
const address = {
...omit(createAddress(), 'addressLine2'),
address2: 'Flat 123'
}
};
const { error } = Joi.validate(address, VALID_ADDRESS);
expect(error).to.be.null();
});
Expand Down Expand Up @@ -99,27 +93,23 @@ experiment('validators', () => {

experiment('when addresssLine3 is null', async () => {
test('cannot be null', async () => {

const address = {
addressLine2: null,
addressLine3: null
}
};

const { error, value } = Joi.validate(address, VALID_ADDRESS);
const { error } = Joi.validate(address, VALID_ADDRESS);
expect(error).to.not.be.null();
});
});
});



experiment('.addressLine3', () => {

test('can be named .address3', async () => {
const address = {
...omit(createAddress(), 'addressLine3'),
address3: 'Flat 123'
}
};
const { error } = Joi.validate(address, VALID_ADDRESS);
expect(error).to.be.null();
});
Expand Down Expand Up @@ -147,26 +137,23 @@ experiment('validators', () => {

experiment('when addresssLine2 is null', async () => {
test('cannot be null', async () => {

const address = {
addressLine2: null,
addressLine3: null
}
};

const { error, value } = Joi.validate(address, VALID_ADDRESS);
const { error } = Joi.validate(address, VALID_ADDRESS);
expect(error).to.not.be.null();
});
});
});


experiment('.addressLine4', () => {

test('can be named .address4', async () => {
const address = {
...omit(createAddress(), 'addressLine4'),
address4: 'Flat 123'
}
};
const { error } = Joi.validate(address, VALID_ADDRESS);
expect(error).to.be.null();
});
Expand All @@ -193,9 +180,7 @@ experiment('validators', () => {
});
});


experiment('.town', () => {

test('cannot be omitted', async () => {
const address = omit(createAddress(), 'town');
const { error } = Joi.validate(address, VALID_ADDRESS);
Expand All @@ -219,20 +204,18 @@ experiment('validators', () => {

experiment('when addresssLine4 is null', async () => {
test('cannot be null', async () => {

const address = {
addressLine4: null,
town: null
}
};

const { error, value } = Joi.validate(address, VALID_ADDRESS);
const { error } = Joi.validate(address, VALID_ADDRESS);
expect(error).to.not.be.null();
});
});
});

experiment('.county', () => {

test('cannot be omitted', async () => {
const address = omit(createAddress(), 'county');
const { error } = Joi.validate(address, VALID_ADDRESS);
Expand All @@ -253,11 +236,9 @@ experiment('validators', () => {
expect(error).to.be.null();
expect(value.county).to.be.null();
});

});

experiment('.country', () => {

test('cannot be omitted', async () => {
const address = omit(createAddress(), 'country');
const { error } = Joi.validate(address, VALID_ADDRESS);
Expand All @@ -272,21 +253,16 @@ experiment('validators', () => {
});

test('cannot be an empty string', async () => {
const { error, value } = Joi.validate(createAddress({
const { error } = Joi.validate(createAddress({
country: ''
}), VALID_ADDRESS);
expect(error).to.not.be.null();
});

});


experiment('.postcode', () => {

['U.K.', 'United Kingdom', 'England', 'Wales', 'Scotland', 'Northern Ireland'].forEach(country => {

experiment(`For ${country}`, () => {

test('cannot be omitted', async () => {
const address = omit(createAddress({ country }), 'postcode');
const { error } = Joi.validate(address, VALID_ADDRESS);
Expand All @@ -295,26 +271,24 @@ experiment('validators', () => {

test('must be a valid postcode', async () => {
const address = createAddress({ country });
const { error, value } = Joi.validate(address, VALID_ADDRESS);
const { error } = Joi.validate(address, VALID_ADDRESS);
expect(error).to.be.null();
});

test('cannot be an invalid postcode', async () => {
const address = createAddress({ country, postcode: 'XXX XXX' });
const { error, value } = Joi.validate(address, VALID_ADDRESS);
const { error } = Joi.validate(address, VALID_ADDRESS);
expect(error).to.not.be.null();
});

test('cannot be null', async () => {
const address = createAddress({ country, postcode: null });
const { error, value } = Joi.validate(address, VALID_ADDRESS);
const { error } = Joi.validate(address, VALID_ADDRESS);
expect(error).to.not.be.null();
});

});

experiment('for non-UK countries', () => {

const country = 'Non-UK country';

test('cannot be omitted', async () => {
Expand All @@ -325,23 +299,20 @@ experiment('validators', () => {

test('can be null', async () => {
const address = createAddress({ country, postcode: null });
const { error, value } = Joi.validate(address, VALID_ADDRESS);
const { error } = Joi.validate(address, VALID_ADDRESS);
expect(error).to.be.null();
});

test('can be any string', async () => {
const address = createAddress({ country, postcode: 'XXX XXX' });
const { error, value } = Joi.validate(address, VALID_ADDRESS);
const { error } = Joi.validate(address, VALID_ADDRESS);
expect(error).to.be.null();
});

});
});
});


experiment('.isTest', () => {

test('can be omitted - defaults to false', async () => {
const address = omit(createAddress(), 'isTest');
const { error, value } = Joi.validate(address, VALID_ADDRESS);
Expand All @@ -357,17 +328,14 @@ experiment('validators', () => {
});

test('can be a boolean value', async () => {
const { error, value } = Joi.validate(createAddress({
const { value } = Joi.validate(createAddress({
isTest: true
}), VALID_ADDRESS);
expect(value.isTest).to.be.true();
});

});


experiment('.uprn', () => {

test('can be omitted - defaults to null', async () => {
const address = omit(createAddress(), 'uprn');
const { error, value } = Joi.validate(address, VALID_ADDRESS);
Expand All @@ -383,23 +351,21 @@ experiment('validators', () => {
});

test('can be an integer', async () => {
const { error, value } = Joi.validate(createAddress({
const { value } = Joi.validate(createAddress({
uprn: 123
}), VALID_ADDRESS);
expect(value.uprn).to.equal(123);
});

test('cannot be negative', async () => {
const { error, value } = Joi.validate(createAddress({
const { error } = Joi.validate(createAddress({
uprn: -1
}), VALID_ADDRESS);
expect(error).to.not.be.null();
});

});

experiment('.dataSource', () => {

test('can be omitted - defaults to wrls', async () => {
const address = omit(createAddress(), 'dataSource');
const { value } = Joi.validate(address, VALID_ADDRESS);
Expand All @@ -420,22 +386,14 @@ experiment('validators', () => {
}), VALID_ADDRESS);
expect(error).to.be.null();
});
})
});

test('cannot be an unknown source', async () => {
const { error } = Joi.validate(createAddress({
dataSource: 'invalid-source'
}), VALID_ADDRESS);
expect(error).to.not.be.null();
});


});


});
});




0 comments on commit 6adaf88

Please sign in to comment.