Skip to content
This repository has been archived by the owner on Jun 12, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1 from motorway/anton/ch28432/tidy-up-vrm
Browse files Browse the repository at this point in the history
Tidy up VRM [ch28432]
  • Loading branch information
antonmotorwayuk authored Mar 24, 2021
2 parents 022d262 + 4e37c07 commit 1879e9c
Show file tree
Hide file tree
Showing 7 changed files with 4,986 additions and 39 deletions.
12 changes: 6 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const Validator = require('is-my-json-valid');
const Alternatives = require('./alternatives');
const Formats = require('./formats');
const schemas = require('./schemas.json');

exports.coerce = coerce;
exports.info = info;

const validateInput = Validator(schemas.input);
const validateAllowedFormats = Validator(schemas.allowedFormats);
const validateNormalizedVRM = Validator(schemas.normalizedVRM);
const validateFormat = Validator(schemas.format);
const {
validateInput,
validateAllowedFormats,
validateNormalizedVRM,
validateFormat,
} = require('./validators');

function coerce(input, allowedFormats) {
if (allowedFormats === void 0)
Expand Down
36 changes: 36 additions & 0 deletions lib/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const { coerce, info } = require('./index');

describe('coerce', () => {
it('should return normalized VRM', () => {
const [coerced] = coerce('HJ 1');
expect(coerced.vrm).toStrictEqual('HJ1');
});

it('should return empty array if invalid sting', () => {
const coerced = coerce('lalalalala');
expect(coerced).toStrictEqual([]);
});

it('should throw on invalid input', () => {
expect(() => coerce(1)).toThrow('input invalid');
expect(() => coerce(null)).toThrow('input invalid');
expect(() => coerce([])).toThrow('input invalid');
expect(() => coerce({})).toThrow('input invalid');
expect(() => coerce(undefined)).toThrow('input invalid');
});
});

describe('info', () => {
it('should return pretty VRM', () => {
const vrmInfo = info('HJ1');
expect(vrmInfo.prettyVrm).toStrictEqual('HJ 1');
});

it('should throw on invalid input', () => {
expect(() => info(1)).toThrow('normalized vrm invalid');
expect(() => info(null)).toThrow('normalized vrm invalid');
expect(() => info([])).toThrow('normalized vrm invalid');
expect(() => info({})).toThrow('normalized vrm invalid');
expect(() => info(undefined)).toThrow('normalized vrm invalid');
});
});
30 changes: 0 additions & 30 deletions lib/schemas.json

This file was deleted.

46 changes: 46 additions & 0 deletions lib/validators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const isString = (val) => {
return typeof val === 'string';
};

const minLength = (val, length) => {
return val.length >= length;
};

const maxLength = (val, length) => {
return val.length <= length;
};

const validateInput = (input) => {
return isString(input) && minLength(input, 1);
};

const formatPattern = /^[a-z0-9_]+/;

const validateAllowedFormats = (formats) => {
if (!Array.isArray(formats)) return false;
if (!minLength(formats, 1)) return false;
return formats.reduce((acc, val) => {
if (!acc) return false;
return isString(val) && minLength(val, 1) && formatPattern.test(val);
}, true);
};

const validateNormalizedVRM = (vrm) => {
return (
isString(vrm) &&
minLength(vrm, 1) &&
maxLength(vrm, 7) &&
/^[A-Z0-9]+$/.test(vrm)
);
};

const validateFormat = (format) => {
return isString(format) && minLength(format, 1) && formatPattern.test(format);
};

module.exports = {
validateInput,
validateAllowedFormats,
validateNormalizedVRM,
validateFormat,
};
83 changes: 83 additions & 0 deletions lib/validators.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const {
validateInput,
validateAllowedFormats,
validateNormalizedVRM,
validateFormat,
} = require('./validators');

describe('validateInput', () => {
it('should return false if not string', () => {
expect(validateInput(2)).toBe(false);
});

it('should return false if length is 0', () => {
expect(validateInput('')).toBe(false);
});

it('should return true if string with value', () => {
expect(validateInput('test')).toBe(true);
});
});

describe('validateAllowedFormats', () => {
it('should return false if not an array', () => {
expect(validateAllowedFormats('test')).toBe(false);
});

it('should return false if empty array', () => {
expect(validateAllowedFormats([])).toBe(false);
});

it('should return false if at least one of the children is not a string', () => {
expect(validateAllowedFormats([1, 'test', null])).toBe(false);
});

it('should return false if at least one of the children has wrong format', () => {
expect(validateAllowedFormats(['Test', 'test'])).toBe(false);
});

it('should return true if all formats are valid', () => {
expect(validateAllowedFormats(['testTest', 'test1', 'test2'])).toBe(true);
});
});

describe('validateNormalizedVRM', () => {
it('should return false if not a string', () => {
expect(validateNormalizedVRM(1)).toBe(false);
});

it('should return false if empty', () => {
expect(validateNormalizedVRM('')).toBe(false);
});

it('should return false if longer than 7 characters', () => {
expect(validateNormalizedVRM('this is a long string')).toBe(false);
});

it('should return false if wrong format', () => {
expect(validateNormalizedVRM('test @')).toBe(false);
});

it('should return true if correct format', () => {
expect(validateNormalizedVRM('HJ1')).toBe(true);
});
});

describe('validateFormat', () => {
it('should return false if not a string', () => {
expect(validateFormat(1)).toBe(false);
});

it('should return false if empty', () => {
expect(validateFormat('')).toBe(false);
});

it('should return false if wrong format', () => {
expect(validateFormat('Test')).toBe(false);
});

it('should return true if correct format', () => {
expect(validateFormat('military')).toBe(true);
expect(validateFormat('gb_2001')).toBe(true);
});
});
Loading

0 comments on commit 1879e9c

Please sign in to comment.