lex-model-validator
Version:
Validate lex-language-models with ease.
181 lines (180 loc) • 7.1 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var src_1 = require("../src");
var TestValidator = /** @class */ (function (_super) {
__extends(TestValidator, _super);
function TestValidator() {
return _super !== null && _super.apply(this, arguments) || this;
}
TestValidator.prototype.getConstraints = function () {
return {};
};
return TestValidator;
}(src_1.Validator));
describe('test constraint validation functions', function () {
var constraintValidationFunctionMap = src_1.CONSTRAINT_VALIDATION_FUNCTION_MAP;
var validator = new src_1.LexModelValidator([TestValidator]);
var testValidator = validator.validator('TestValidator');
var expectNoErrors = function () {
expect(validator.collectedErrors).toHaveLength(0);
};
var expectErrors = function (amount) {
if (amount) {
expect(validator.collectedErrors).toHaveLength(amount);
}
else {
expect(validator.collectedErrors).not.toHaveLength(0);
}
};
var fooBarPair = {
key: 'foo',
value: 'bar'
};
beforeEach(function () {
validator.collectedErrors = [];
});
describe('test required', function () {
var required = constraintValidationFunctionMap.required;
test('test required enabled succeeds', function () {
required.call(testValidator, true, fooBarPair);
expectNoErrors();
});
test('test required enabled fails', function () {
required.call(testValidator, true, { key: 'foo', value: undefined });
expectErrors(1);
});
test('test required disabled undefined succeeds', function () {
required.call(testValidator, false, { key: 'foo', value: undefined });
expectNoErrors();
});
});
describe('test validate', function () {
var validate = constraintValidationFunctionMap.validate;
// tslint:disable-next-line
var validationFunction = function (validationPair) {
if (validationPair.value !== 'bar') {
this.pushError("test error");
}
};
test('test validate succeeds', function () {
validate.call(testValidator, validationFunction, fooBarPair);
expectNoErrors();
});
test('test validate fails', function () {
validate.call(testValidator, validationFunction, {
key: 'foo',
value: undefined
});
expectErrors(1);
});
});
describe('test minLength', function () {
var minLength = constraintValidationFunctionMap.minLength;
test('test minLength succeeds', function () {
minLength.call(testValidator, 1, fooBarPair);
expectNoErrors();
});
test('test minLength fails', function () {
minLength.call(testValidator, 4, fooBarPair);
expectErrors(1);
});
});
describe('test maxLength', function () {
var maxLength = constraintValidationFunctionMap.maxLength;
test('test maxLength succeeds', function () {
maxLength.call(testValidator, 10, fooBarPair);
expectNoErrors();
});
test('test maxLength fails', function () {
maxLength.call(testValidator, 2, fooBarPair);
expectErrors(1);
});
});
describe('test type', function () {
var type = constraintValidationFunctionMap.type;
test('test maxLength succeeds', function () {
type.call(testValidator, 'string', fooBarPair);
expectNoErrors();
});
test('test maxLength fails', function () {
type.call(testValidator, 'number', fooBarPair);
expectErrors(1);
});
});
describe('test isArray', function () {
var isArray = constraintValidationFunctionMap.isArray;
test('test isArray succeeds', function () {
isArray.call(testValidator, true, { key: 'foo', value: ['bar'] });
expectNoErrors();
});
test('test isArray fails', function () {
isArray.call(testValidator, true, fooBarPair);
expectErrors(1);
});
});
describe('test min', function () {
var min = constraintValidationFunctionMap.min;
test('test min succeeds', function () {
min.call(testValidator, 3, { key: 'foo', value: 10 });
expectNoErrors();
});
test('test min fails', function () {
min.call(testValidator, 3, { key: 'foo', value: 2 });
expectErrors(1);
});
});
describe('test max', function () {
var max = constraintValidationFunctionMap.max;
test('test max succeeds', function () {
max.call(testValidator, 10, { key: 'foo', value: 10 });
expectNoErrors();
});
test('test max fails', function () {
max.call(testValidator, 10, { key: 'foo', value: 11 });
expectErrors(1);
});
});
describe('test equals', function () {
var equals = constraintValidationFunctionMap.equals;
test('test equals succeeds', function () {
equals.call(testValidator, 'bar', fooBarPair);
expectNoErrors();
});
test('test equals fails', function () {
equals.call(testValidator, 'foo', fooBarPair);
expectErrors(1);
});
});
describe('test matches', function () {
var matches = constraintValidationFunctionMap.matches;
test('test matches regexp succeeds', function () {
matches.call(testValidator, RegExp('bar', 'g'), fooBarPair);
expectNoErrors();
});
test('test matches regexp fails', function () {
matches.call(testValidator, RegExp('foo', 'g'), fooBarPair);
expectErrors(1);
});
test('test matches array succeeds', function () {
matches.call(testValidator, ['abc', 'bar'], fooBarPair);
expectNoErrors();
});
test('test matches array fails', function () {
matches.call(testValidator, ['abc', 'foo'], fooBarPair);
expectErrors(1);
});
});
});