mirror of
https://github.com/directus/directus.git
synced 2026-01-29 00:27:59 -05:00
* Step 1 * Step 2 * False sense of confidence * Couple more before dinner * Update schema package * Update format-title * Upgrade specs file * Close * Replace ts-node-dev with tsx, and various others * Replace lodash with lodash-es * Add lodash-es types * Update knex import * More fun is had * FSE * Consolidate repos * Various tweaks and fixes * Fix specs * Remove dependency on knex-schema-inspector * Fix wrong imports of inspector * Move shared exceptions to new package * Move constants to separate module * Move types to new types package * Use directus/types * I believe this is no longer needed * [WIP] Start moving utils to esm * ESMify Shared * Move shared utils to @directus/utils * Use @directus/utils instead of @directus/shared/utils * It runs! * Use correct schemaoverview type * Fix imports * Fix the thing * Start on new update-checker lib * Use new update-check package * Swap out directus/shared in app * Pushing through the last bits now * Dangerously make extensions SDK ESM * Use @directus/types in tests * Copy util function to test * Fix linter config * Add missing import * Hot takes * Fix build * Curse these default exports * No tests in constants * Add tests * Remove tests from types * Add tests for exceptions * Fix test * Fix app tests * Fix import in test * Fix various tests * Fix specs export * Some more tests * Remove broken integration tests These were broken beyond repair.. They were also written before we really knew what we we're doing with tests, so I think it's better to say goodbye and start over with these * Regenerate lockfile * Fix imports from merge * I create my own problems * Make sharp play nice * Add vitest config * Install missing blackbox dep * Consts shouldn't be in types tsk tsk tsk tsk * Fix type/const usage in extensions-sdk * cursed.default * Reduce circular deps * Fix circular dep in items service * vvv * Trigger testing for all vendors * Add workaround for rollup * Prepend the file protocol for the ESM loader to be compatible with Windows "WARN: Only URLs with a scheme in: file and data are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'c:'" * Fix postgres * Schema package updates Co-authored-by: Azri Kahar <42867097+azrikahar@users.noreply.github.com> * Resolve cjs/mjs extensions * Clean-up eslint config * fixed extension concatination * using string interpolation for consistency * Revert MySQL optimisation * Revert testing for all vendors * Replace tsx with esbuild-kit/esm-loader Is a bit faster and we can rely on the built-in `watch` and `inspect` functionalities of Node.js Note: The possibility to watch other files (.env in our case) might be added in the future, see https://github.com/nodejs/node/issues/45467 * Use exact version for esbuild-kit/esm-loader * Fix import --------- Co-authored-by: ian <licitdev@gmail.com> Co-authored-by: Brainslug <tim@brainslug.nl> Co-authored-by: Azri Kahar <42867097+azrikahar@users.noreply.github.com> Co-authored-by: Pascal Jufer <pascal-jufer@bluewin.ch>
686 lines
22 KiB
TypeScript
686 lines
22 KiB
TypeScript
import { escapeRegExp } from 'lodash-es';
|
|
import { describe, expect, it } from 'vitest';
|
|
import type { FieldFilter } from '@directus/types';
|
|
import { generateJoi, Joi, JoiOptions, StringSchema } from './generate-joi.js';
|
|
|
|
describe(`generateJoi`, () => {
|
|
const date = new Date(1632431505992);
|
|
const compareDate = new Date(1632431605992);
|
|
|
|
it(`returns an error when no key is passed`, () => {
|
|
const mockFieldFilter = {} as FieldFilter;
|
|
const mockError = `[generateJoi] Filter doesn't contain field key. Passed filter: {}`;
|
|
|
|
expect(() => {
|
|
generateJoi(mockFieldFilter);
|
|
}).toThrowError(mockError);
|
|
});
|
|
|
|
it(`returns an error when no filter rule is passed`, () => {
|
|
const mockFieldFilter = { field: { eq: undefined } } as unknown as FieldFilter;
|
|
const mockError = `[generateJoi] Filter doesn't contain filter rule. Passed filter: {}`;
|
|
|
|
expect(() => {
|
|
generateJoi(mockFieldFilter);
|
|
}).toThrowError(mockError);
|
|
});
|
|
|
|
it(`returns an recursively goes through nested filters`, () => {
|
|
const mockFieldFilter = { field: { eq: { _eq: 'field' } } } as unknown as FieldFilter;
|
|
|
|
const mockSchema = Joi.object({
|
|
field: Joi.object({ eq: Joi.any().equal('field') }).unknown(),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema with an option of "requireAll" true`, () => {
|
|
const mockFieldFilter = { field: { _eq: 'field' } } as FieldFilter;
|
|
const mockOptions = { requireAll: true } as JoiOptions;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().equal('field').required(),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter, mockOptions).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _eq match`, () => {
|
|
const mockFieldFilter = { field: { _eq: 'field' } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().equal('field'),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for a _neq match`, () => {
|
|
const mockFieldFilter = { field: { _neq: 'field' } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().not('field'),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an integer _eq match`, () => {
|
|
const mockFieldFilter = { field: { _eq: '123' } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().equal('123', 123),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an integer _neq match`, () => {
|
|
const mockFieldFilter = { field: { _neq: '123' } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().not('123', 123),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for a float _eq match`, () => {
|
|
const mockFieldFilter = { field: { _eq: '123.456' } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().equal('123.456', 123.456),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for a float _neq match`, () => {
|
|
const mockFieldFilter = { field: { _neq: '123.456' } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().not('123.456', 123.456),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for a null _eq match`, () => {
|
|
const mockFieldFilter = { field: { _eq: null } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().equal(null),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for a null _neq match`, () => {
|
|
const mockFieldFilter = { field: { _neq: null } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().not(null),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an empty string _eq match`, () => {
|
|
const mockFieldFilter = { field: { _eq: '' } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().equal(''),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an empty string _neq match`, () => {
|
|
const mockFieldFilter = { field: { _neq: '' } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().not(''),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for a true _eq match`, () => {
|
|
const mockFieldFilter = { field: { _eq: true } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().equal(true),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for a true _neq match`, () => {
|
|
const mockFieldFilter = { field: { _neq: true } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().not(true),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for a false _eq match`, () => {
|
|
const mockFieldFilter = { field: { _eq: false } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().equal(false),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for a false _neq match`, () => {
|
|
const mockFieldFilter = { field: { _neq: false } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().not(false),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _ncontains contain match`, () => {
|
|
const mockFieldFilter = { field: { _ncontains: 'field' } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: (Joi.string() as StringSchema).ncontains('field'),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _ncontains with null value`, () => {
|
|
const mockFieldFilter = { field: { _ncontains: null } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().equal(true),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _contains contain match`, () => {
|
|
const mockFieldFilter = { field: { _contains: 'field' } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: (Joi.string() as StringSchema).contains('field'),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _contains with null value`, () => {
|
|
const mockFieldFilter = { field: { _contains: null } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().equal(true),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _icontains contain match`, () => {
|
|
const mockFieldFilter = { field: { _icontains: 'field' } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: (Joi.string() as StringSchema).contains('field'),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _icontains with null value`, () => {
|
|
const mockFieldFilter = { field: { _icontains: null } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().equal(true),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns a value if the substring is included in the value`, () => {
|
|
expect(() => {
|
|
Joi.assert('testfield', (Joi.string() as StringSchema).contains('field'));
|
|
}).toEqual(expect.any(Function));
|
|
});
|
|
|
|
it(`returns a value if the substring is not contained in the value`, () => {
|
|
expect(() => {
|
|
Joi.assert('testfield', (Joi.string() as StringSchema).contains('field'));
|
|
}).toEqual(expect.any(Function));
|
|
});
|
|
|
|
it(`returns an error if the substring is included in the value`, () => {
|
|
expect(() => {
|
|
Joi.assert('field', (Joi.string() as StringSchema).ncontains('field'));
|
|
// eslint-disable-next-line no-useless-escape
|
|
}).toThrowError(`\"value\" can't contain [field]`);
|
|
});
|
|
|
|
it(`returns an error if the substring is not contained in the value`, () => {
|
|
expect(() => {
|
|
Joi.assert('test', (Joi.string() as StringSchema).contains('field'));
|
|
// eslint-disable-next-line no-useless-escape
|
|
}).toThrowError(`\"value\" must contain [field`);
|
|
});
|
|
|
|
it(`returns the correct schema for a _starts_with match`, () => {
|
|
const mockFieldFilter = { field: { _starts_with: 'field' } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.string().pattern(new RegExp(`^${escapeRegExp('field')}.*`), {
|
|
name: 'starts_with',
|
|
}),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for a _starts_with with null value`, () => {
|
|
const mockFieldFilter = { field: { _starts_with: null } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().equal(true),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for a _nstarts_with with match`, () => {
|
|
const mockFieldFilter = { field: { _nstarts_with: 'field' } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.string().pattern(new RegExp(`^${escapeRegExp('field')}.*`), {
|
|
name: 'starts_with',
|
|
invert: true,
|
|
}),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for a _nstarts_with with null value`, () => {
|
|
const mockFieldFilter = { field: { _nstarts_with: null } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().equal(true),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an ends_with match`, () => {
|
|
const mockFieldFilter = { field: { _ends_with: 'field' } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.string().pattern(new RegExp(`.*field$`), {
|
|
name: 'ends_with',
|
|
}),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an ends_with with null value`, () => {
|
|
const mockFieldFilter = { field: { _ends_with: null } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().equal(true),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for a doesnt _nends_with match`, () => {
|
|
const mockFieldFilter = { field: { _nends_with: 'field' } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.string().pattern(new RegExp(`.*field$`), {
|
|
name: 'ends_with',
|
|
invert: true,
|
|
}),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for a doesnt _nends_with with null value`, () => {
|
|
const mockFieldFilter = { field: { _nends_with: null } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().equal(true),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _in match`, () => {
|
|
const mockFieldFilter = { field: { _in: 'field' } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().equal(...'field'),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _in number array match`, () => {
|
|
const mockFieldFilter = { field: { _in: [1] } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().equal(...[1]),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _in a string array match`, () => {
|
|
const mockFieldFilter = { field: { _in: ['field', 'secondField'] } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().equal(...['field', 'secondField']),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for a _nin match`, () => {
|
|
const mockFieldFilter = { field: { _nin: 'field' } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().not(...'field'),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _nin number array match`, () => {
|
|
const mockFieldFilter = { field: { _nin: [1] } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().not(...[1]),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _nin a string array match`, () => {
|
|
const mockFieldFilter = { field: { _nin: ['field', 'secondField'] } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().not(...['field', 'secondField']),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _gt number match`, () => {
|
|
const mockFieldFilter = { field: { _gt: 1 } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.number().greater(1),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _gt date match`, () => {
|
|
const mockFieldFilter = { field: { _gt: date } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.date().greater(date),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _gt string match`, () => {
|
|
const mockFieldFilter = { field: { _gt: date.toISOString() } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.date().greater(date),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _gte number match`, () => {
|
|
const mockFieldFilter = { field: { _gte: 1 } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.number().min(1),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _gte date match`, () => {
|
|
const mockFieldFilter = { field: { _gte: date } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.date().min(date),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _gte string match`, () => {
|
|
const mockFieldFilter = { field: { _gte: date.toISOString() } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.date().min(date),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _lt number match`, () => {
|
|
const mockFieldFilter = { field: { _lt: 1 } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.number().less(1),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _lt date match`, () => {
|
|
const mockFieldFilter = { field: { _lt: date } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.date().less(date),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _lt string match`, () => {
|
|
const mockFieldFilter = { field: { _lt: date.toISOString() } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.date().less(date),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _lte number match`, () => {
|
|
const mockFieldFilter = { field: { _lte: 1 } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.number().max(1),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _lte date match`, () => {
|
|
const mockFieldFilter = { field: { _lte: date } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.date().max(date),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _lte string match`, () => {
|
|
const mockFieldFilter = { field: { _lte: date.toISOString() } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.date().max(date),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _null match`, () => {
|
|
const mockFieldFilter = { field: { _null: null } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().valid(null),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _nnull match`, () => {
|
|
const mockFieldFilter = { field: { _nnull: null } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().invalid(null),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _empty match`, () => {
|
|
const mockFieldFilter = { field: { _empty: '' } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().valid(''),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _nempty match`, () => {
|
|
const mockFieldFilter = { field: { _nempty: '' } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().invalid(''),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _between number match`, () => {
|
|
const mockFieldFilter = { field: { _between: [1, 3] } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.number().min(1).max(3),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _between float match`, () => {
|
|
const mockFieldFilter = { field: { _between: [1.111, 3.333] } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.number().min(1.111).max(3.333),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _between date match`, () => {
|
|
const mockFieldFilter = { field: { _between: [date, compareDate] } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.date().min(date).max(compareDate),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _nbetween number match`, () => {
|
|
const mockFieldFilter = { field: { _nbetween: [1, 3] } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.number().less(1).greater(3),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _nbetween float match`, () => {
|
|
const mockFieldFilter = { field: { _nbetween: [1.111, 3.333] } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.number().less(1.111).greater(3.333),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _nbetween date match`, () => {
|
|
const mockFieldFilter = { field: { _nbetween: [date, compareDate] } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.date().less(date).greater(compareDate),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _submitted match`, () => {
|
|
const mockFieldFilter = { field: { _submitted: '' } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().required(),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _regex match when wrapped`, () => {
|
|
const mockFieldFilter = { field: { _regex: '/.*field$/' } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.string().regex(new RegExp(`.*field$`)),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _regex match when unwrapped`, () => {
|
|
const mockFieldFilter = { field: { _regex: '.*field$' } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.string().regex(new RegExp(`.*field$`)),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
|
|
it(`returns the correct schema for an _regex match with null value`, () => {
|
|
const mockFieldFilter = { field: { _regex: null } } as FieldFilter;
|
|
const mockSchema = Joi.object({
|
|
field: Joi.any().equal(true),
|
|
})
|
|
.unknown()
|
|
.describe();
|
|
expect(generateJoi(mockFieldFilter).describe()).toStrictEqual(mockSchema);
|
|
});
|
|
});
|