validata-mongo
Version:
MongoDB update `$set` operations
141 lines • 7.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const validata_1 = require("validata");
const object_set_1 = require("./object-set");
const test_helpers_1 = require("./test-helpers");
describe('isObjectSet', () => {
it('will fail non-object', () => {
const fut = (0, object_set_1.isObjectSet)();
(0, test_helpers_1.expectIssue)(fut, null, 'not-defined');
(0, test_helpers_1.expectIssue)(fut, undefined, 'not-defined');
(0, test_helpers_1.expectIssue)(fut, 0, 'incorrect-type');
(0, test_helpers_1.expectIssue)(fut, new Date(), 'incorrect-type');
(0, test_helpers_1.expectIssue)(fut, [], 'incorrect-type');
(0, test_helpers_1.expectIssue)(fut, 'test', 'incorrect-type');
});
it('will accept object', () => {
const fut = (0, object_set_1.isObjectSet)();
(0, test_helpers_1.expectSuccess)(fut, {});
(0, test_helpers_1.expectSuccess)(fut, { a: 47 });
});
it('will process children', () => {
const fut = (0, object_set_1.isObjectSet)({
a: (0, validata_1.asNumber)({ coerceMin: 25 }),
b: (0, validata_1.asString)(),
});
(0, test_helpers_1.expectValue)(fut, { a: 47, b: 'asd' }, { a: 47, b: 'asd' });
(0, test_helpers_1.expectValue)(fut, { a: '47', b: 12 }, { a: 47, b: '12' });
(0, test_helpers_1.expectValue)(fut, { a: '7', b: 12 }, { a: 25, b: '12' });
});
describe('deep object', () => {
const fut = (0, object_set_1.isObjectSet)({
o: (0, object_set_1.isObjectSet)({
x: (0, validata_1.isNumber)(),
y: (0, validata_1.maybeAsString)(),
d: (0, object_set_1.isObjectSet)({
a: (0, validata_1.isNumber)({ coerceMax: 30 }),
b: (0, validata_1.asString)(),
}),
}),
s: (0, validata_1.asString)(),
});
it('will check type of 1st level props - incorrect-type', () => {
(0, test_helpers_1.expectIssue)(fut, { o: 47, s: 'asd' }, 'incorrect-type', ['o']);
});
it('will check type of 2nd level props - incorrect-type', () => {
(0, test_helpers_1.expectIssue)(fut, { o: { d: 23 }, s: 'asd' }, 'incorrect-type', ['o', 'd']);
});
it('will check type of 2nd level deep-props - incorrect-type', () => {
(0, test_helpers_1.expectIssue)(fut, { 'o.d': 23, s: 'asd' }, 'incorrect-type', ['o.d']);
});
it('will check type of 3rd level deep-props - success', () => {
(0, test_helpers_1.expectValue)(fut, { 'o.d.a': 45 }, { 'o.d.a': 30 });
});
it('will check all levels full objects - success', () => {
(0, test_helpers_1.expectValue)(fut, { o: { x: 12, y: 12, d: { a: 23, b: 'foo' } }, s: 'asd' }, { o: { x: 12, y: '12', d: { a: 23, b: 'foo' } }, s: 'asd' });
});
it('will check all levels full objects (maybe) - success', () => {
(0, test_helpers_1.expectValue)(fut, { o: { x: 12, d: { a: 23, b: 'foo' } }, s: 'asd' }, { o: { x: 12, d: { a: 23, b: 'foo' } }, s: 'asd' });
});
it('will check will check 2nd level object property - incorrect-type', () => {
(0, test_helpers_1.expectIssue)(fut, { o: { x: 'hello', y: 'hello' }, s: 'asd' }, 'incorrect-type', ['o', 'x']);
});
it('will allow 1st level properties to be optional', () => {
(0, test_helpers_1.expectValue)(fut, { s: 'asd' }, { s: 'asd' });
});
it('will coerce 2nd level deep-properties', () => {
(0, test_helpers_1.expectValue)(fut, { 'o.x': 12, 'o.y': 12 }, { 'o.x': 12, 'o.y': '12' });
(0, test_helpers_1.expectValue)(fut, { 'o.y': 12 }, { 'o.y': '12' });
});
it('will allow 2nd level deep-properties to be optional', () => {
(0, test_helpers_1.expectValue)(fut, { 'o.x': 12 }, { 'o.x': 12 });
});
it('will check 2nd level deep-properties - unexpected-property', () => {
(0, test_helpers_1.expectIssue)(fut, { 'o.a': 12 }, 'unexpected-property', ['o.a']);
});
it('will check 3rd level deep-properties - unexpected-property', () => {
(0, test_helpers_1.expectIssue)(fut, { 'o.d.foo': 12 }, 'unexpected-property', ['o.d.foo']);
});
it('will not allow nested deep-properties - unexpected-property', () => {
(0, test_helpers_1.expectIssue)(fut, { o: { 'd.a': 23 } }, 'unexpected-property', ['o', 'd.a']);
});
it('will require non-optional properties on nested objects - not-defined', () => {
(0, test_helpers_1.expectIssue)(fut, { o: {}, s: 'asd' }, 'not-defined', ['o', 'x']);
(0, test_helpers_1.expectIssue)(fut, { o: {}, s: 'asd' }, 'not-defined', ['o', 'd']);
(0, test_helpers_1.expectIssue)(fut, { o: { x: 12, y: 12 } }, 'not-defined', ['o', 'd']);
});
it('will not require properties on nested objects - success', () => {
(0, test_helpers_1.expectSuccess)(fut, { o: { x: 12, d: { a: 23, b: 'foo' } } });
});
it('will not allow additional properties on nested objects - unexpected-property', () => {
(0, test_helpers_1.expectIssue)(fut, { o: { x: 12, foo: 12, d: { a: 23, b: 'foo' } } }, 'unexpected-property', ['o', 'foo']);
});
});
it('will process children', () => {
const fut = (0, object_set_1.isObjectSet)({
a: (0, validata_1.isNumber)({ min: 25 }),
b: (0, validata_1.isString)(),
});
(0, test_helpers_1.expectValue)(fut, { a: 47, b: 'asd' }, { a: 47, b: 'asd' });
(0, test_helpers_1.expectIssue)(fut, { a: '47', b: 'asd' }, 'incorrect-type', ['a']);
(0, test_helpers_1.expectIssue)(fut, { a: 47, b: 'asd', c: 234 }, 'unexpected-property', ['c']);
(0, test_helpers_1.expectValue)(fut, {}, {});
});
it('will handle optional property', () => {
const fut = (0, object_set_1.isObjectSet)({
a: (0, validata_1.isNumber)(),
b: (0, validata_1.maybeString)(),
});
(0, test_helpers_1.expectValue)(fut, {}, {});
(0, test_helpers_1.expectValue)(fut, { b: 'asd' }, { b: 'asd' });
{
const result = (0, test_helpers_1.expectSuccess)(fut, { a: 42 });
expect(result.value).not.toHaveProperty('b');
}
{
const result = (0, test_helpers_1.expectSuccess)(fut, { a: 42, b: undefined });
expect(result.value).toHaveProperty('b');
expect(result.value.b).toEqual(undefined);
}
{
const result = (0, test_helpers_1.expectSuccess)(fut, { a: 42, b: null });
expect(result.value).toHaveProperty('b');
expect(result.value.b).toEqual(undefined);
}
});
it('will error on unexpected properties', () => {
const fut = (0, object_set_1.isObjectSet)({
a: (0, validata_1.isNumber)({ min: 25 }),
b: (0, validata_1.isString)(),
});
(0, test_helpers_1.expectIssue)(fut, { a: 47, b: 'asd', c: 234 }, 'unexpected-property', ['c']);
});
it('will strip unexpected properties', () => {
const fut = (0, object_set_1.isObjectSet)({
a: (0, validata_1.isNumber)({ min: 25 }),
b: (0, validata_1.isString)(),
}, { stripExtraProperties: true });
(0, test_helpers_1.expectValue)(fut, { a: 47, b: 'asd', c: 345, d: 'hello' }, { a: 47, b: 'asd' });
});
});
//# sourceMappingURL=object-set.test.js.map