http-assert-value
Advanced tools
Comparing version 2.0.0 to 2.1.0
44
index.js
@@ -61,6 +61,9 @@ const httpAssert = require('http-assert'); | ||
* | ||
* If value equals undefined, format value to string | ||
* @see https://stackoverflow.com/a/1085199 | ||
* | ||
* @param {String} value | ||
* @param {String} [field] - name of the parameter containing value | ||
*/ | ||
text: (value, field = 'Text') => { | ||
text: (value = '', field = 'Text') => { | ||
const isValid = /^[^*;!#$%:^&)(?></\\]+$/i.test(value); | ||
@@ -83,2 +86,41 @@ | ||
assert(isValid, 'Check by schema failed', 400, 'CSF', { errors: ajv.errors }); | ||
}, | ||
/** | ||
* Assert value, that it is in array | ||
* | ||
* @param {*} value | ||
* @param {Array} expected | ||
* @param {Function} comparator, which compare values | ||
*/ | ||
oneOf(value, expected = [], comparator = (lhs, rhs) => lhs === rhs) { | ||
const isValid = expected.some(expValue => comparator(expValue, value)); | ||
assert(isValid, 'Value is not allowed', 400, 'VNA', { value, expected }); | ||
}, | ||
/** | ||
* Assert that value is right id (positive number) | ||
* | ||
* @param {Number} value | ||
* @param {String} [field] - name of the parameter containing value | ||
*/ | ||
id(value, field = 'Id') { | ||
const isValid = /^\d+$/.test(value) && Number(value) > 0; | ||
assert(isValid, `${field} is invalid`, 400, 'III', { value }); | ||
}, | ||
/** | ||
* Assert that value length is less than passed maxLength | ||
* | ||
* @param {String} value | ||
* @param {Number} [maxLength] - max length of a string | ||
* @param {String} [field] - name of the parameter containing value | ||
*/ | ||
maxLength(value, maxLength = 0, field = 'Text') { | ||
const isValid = value && value.length < maxLength; | ||
const assertMessage = `${field} should be less than ${maxLength} characters`; | ||
assert(isValid, assertMessage, 400, 'TML', { value, maxLength }); | ||
} | ||
@@ -85,0 +127,0 @@ }; |
{ | ||
"name": "http-assert-value", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "assert values with status codes", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -81,34 +81,34 @@ # http-assert of typed values | ||
* value `Any|Array<Any>` - asserting value | ||
* schema `Object` - asserting value | ||
* schema `Object` - json-schema | ||
* \[options\] `Object` - ajv constructor options | ||
### assert.tryIdentity(value, field = 'Identity') | ||
Assert slug or unique identity value, when value is defined | ||
### assert.oneOf(value, expected = [], comparator) | ||
Assert value, that it is in array | ||
* value `String|Array<String>` - asserting value | ||
* \[field\] `String` - name of the parameter containing value | ||
* value `Any` - asserting value | ||
* \[expected\] `Array` - array which contains expected values | ||
* \[comparator\] `Function` - function which compare values from array with asserting | ||
value. By default used comparator to compare primitive (`(lhs, rhs) => lhs === rhs`) | ||
### assert.tryFloat(value, field = 'Float') | ||
Assert float value, when value is defined | ||
### assert.id(value, field = 'Id') | ||
Alias for `assert.positiveInt` | ||
* value `String|Array<String>` - asserting value | ||
* \[field\] `String` - name of the parameter containing value | ||
### assert.maxLength(value, maxLength = 0, field = 'Text') | ||
Assert that value length is less than passed maxLength | ||
### assert.tryPositiveInt(value, field = 'Positive integer') | ||
Assert positive integer value, when value is defined | ||
* value `String|Array<String>` - asserting value | ||
* value `Any` - asserting value | ||
* \[maxLength\] `Number` - max allowed length of a string | ||
* \[field\] `String` - name of the parameter containing value | ||
### assert.tryText(value, field = 'Text') | ||
Assert text, like search request, when value is defined | ||
* value `String|Array<String>` - asserting value | ||
* \[field\] `String` - name of the parameter containing value | ||
Every method has *try-version* method, which has same interface and assert value, | ||
when value is passed: | ||
### assert.tryBySchema(value, schema, options = {}) | ||
Assert object by schema, when value is defined | ||
* value `Any|Array<Any>` - asserting value | ||
* schema `Object` - asserting value | ||
* \[options\] `Object` - ajv constructor options | ||
- assert.tryIdentity | ||
- assert.tryFloat | ||
- assert.tryPositiveInt | ||
- assert.tryText | ||
- assert.tryBySchema | ||
- assert.oneOf | ||
- assert.id | ||
- assert.maxLength |
@@ -226,2 +226,11 @@ const catchError = require('catch-error-async'); | ||
}); | ||
it('should throw error when value is not passed', async () => { | ||
const error = await catchError(sut.text); | ||
assert.strictEqual(error.message, 'Text is invalid'); | ||
assert.strictEqual(error.statusCode, 400); | ||
assert.strictEqual(error.options.internalCode, '400_TVI'); | ||
assert.strictEqual(error.options.value, ''); | ||
}); | ||
}); | ||
@@ -314,2 +323,134 @@ | ||
}); | ||
describe('oneOf', () => { | ||
it('should do nothing when value is expected', () => { | ||
sut.oneOf(1, [1, 2]); | ||
sut.oneOf('abc', ['abc', true, 3]); | ||
sut.oneOf(true, ['abc', true, 3]); | ||
// should apply passed comparator | ||
sut.oneOf({ a: 1 }, [2, { a: 1 }], (a, b) => a.toString() === b.toString()); | ||
}); | ||
it('should throw error when value is not expected', async () => { | ||
const error = await catchError(sut.oneOf, 'abc', ['cde', 'ab']); | ||
assert.strictEqual(error.message, 'Value is not allowed'); | ||
assert.strictEqual(error.statusCode, 400); | ||
assert.strictEqual(error.options.internalCode, '400_VNA'); | ||
assert.strictEqual(error.options.value, 'abc'); | ||
}); | ||
it('should throw error when value not passed', async () => { | ||
const error = await catchError(sut.oneOf); | ||
assert.strictEqual(error.message, 'Value is not allowed'); | ||
}); | ||
}); | ||
describe('tryOneOf', () => { | ||
it('should do nothing when value is expected', () => { | ||
sut.tryOneOf(1, [1, 2]); | ||
sut.tryOneOf('abc', ['abc', true, 3]); | ||
sut.tryOneOf(true, ['abc', true, 3]); | ||
// should apply passed comparator | ||
sut.tryOneOf({ a: 1 }, [2, { a: 1 }], (a, b) => a.toString() === b.toString()); | ||
}); | ||
it('should throw error when value is not expected', async () => { | ||
const error = await catchError(sut.tryOneOf, 'abc', ['cde', 'ab']); | ||
assert.strictEqual(error.message, 'Value is not allowed'); | ||
assert.strictEqual(error.statusCode, 400); | ||
assert.strictEqual(error.options.internalCode, '400_VNA'); | ||
assert.strictEqual(error.options.value, 'abc'); | ||
}); | ||
it('should do nothing when value not passed', () => { | ||
sut.tryOneOf(); | ||
}); | ||
}); | ||
describe('id', () => { | ||
it('should do nothing when value is correct id', () => { | ||
sut.id(1); | ||
sut.id(2); | ||
sut.id(4815162342); | ||
}); | ||
it('should throw error when value is not id', async () => { | ||
const error = await catchError(sut.id, 'abc'); | ||
assert.strictEqual(error.message, 'Id is invalid'); | ||
assert.strictEqual(error.statusCode, 400); | ||
assert.strictEqual(error.options.internalCode, '400_III'); | ||
assert.strictEqual(error.options.value, 'abc'); | ||
}); | ||
it('should throw error when value not passed', async () => { | ||
const error = await catchError(sut.id); | ||
assert.strictEqual(error.message, 'Id is invalid'); | ||
}); | ||
}); | ||
describe('tryId', () => { | ||
it('should do nothing when value is correct id', () => { | ||
sut.tryId(1); | ||
sut.tryId(2); | ||
sut.tryId(4815162342); | ||
}); | ||
it('should throw error when value is not correct id', async () => { | ||
const error = await catchError(sut.tryId, 'abc'); | ||
assert.strictEqual(error.message, 'Id is invalid'); | ||
assert.strictEqual(error.statusCode, 400); | ||
assert.strictEqual(error.options.internalCode, '400_III'); | ||
assert.strictEqual(error.options.value, 'abc'); | ||
}); | ||
it('should do nothing when value not passed', () => { | ||
sut.tryId(); | ||
}); | ||
}); | ||
describe('maxLength', () => { | ||
it('should do nothing when value\'s length is less than max length', () => { | ||
sut.maxLength('hello world', 13); | ||
sut.maxLength('hey', 4); | ||
}); | ||
it('should throw error when value\'s length is more than max length', async () => { | ||
const error = await catchError(sut.maxLength, 'hello world', 5); | ||
assert.strictEqual(error.message, 'Text should be less than 5 characters'); | ||
assert.strictEqual(error.statusCode, 400); | ||
assert.strictEqual(error.options.internalCode, '400_TML'); | ||
assert.strictEqual(error.options.value, 'hello world'); | ||
assert.strictEqual(error.options.maxLength, 5); | ||
}); | ||
}); | ||
describe('tryMaxLength', () => { | ||
it('should do nothing when value\'s length is less than max length', () => { | ||
sut.tryMaxLength('hello world', 13); | ||
sut.tryMaxLength('hey', 4); | ||
}); | ||
it('should throw error when value\'s length is more than max length', async () => { | ||
const error = await catchError(sut.tryMaxLength, 'hello world', 5); | ||
assert.strictEqual(error.message, 'Text should be less than 5 characters'); | ||
assert.strictEqual(error.statusCode, 400); | ||
assert.strictEqual(error.options.internalCode, '400_TML'); | ||
assert.strictEqual(error.options.value, 'hello world'); | ||
assert.strictEqual(error.options.maxLength, 5); | ||
}); | ||
it('should do nothing when value not passed', () => { | ||
sut.tryMaxLength(); | ||
}); | ||
}); | ||
}); |
26925
531