mirror of
https://github.com/directus/directus.git
synced 2026-04-03 03:00:39 -04:00
Add support for Conditional Fields (#6864)
* Add conditions field to directus_fields * Add conditions configuration * Apply conditional overrides * Handle conditions in nested groups * Fix reverse mutating conditions * Start on filter setup interface * Move field types/constants to shared * [WIP] Updated client side filter validation * Support logical operators in client validation step * Use new validation util in conditions check * Add nesting in filter seutp * Add filter rule setup configurator * Fixes that should've been done in the merge * Strip out filter-settings interface TBD in a new PR * Move browser to index
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import BaseJoi, { AnySchema } from 'joi';
|
||||
import { escapeRegExp } from 'lodash';
|
||||
import { escapeRegExp, merge } from 'lodash';
|
||||
|
||||
/**
|
||||
* @TODO
|
||||
@@ -58,20 +58,19 @@ const Joi = BaseJoi.extend({
|
||||
});
|
||||
|
||||
type JoiOptions = {
|
||||
allowUnknown: boolean;
|
||||
allowUnknown?: boolean;
|
||||
requireAll?: boolean;
|
||||
};
|
||||
|
||||
const defaults: JoiOptions = {
|
||||
allowUnknown: true,
|
||||
requireAll: false,
|
||||
};
|
||||
|
||||
export default function generateJoi(filter: Record<string, any> | null, options?: JoiOptions): AnySchema {
|
||||
filter = filter || {};
|
||||
|
||||
options = {
|
||||
...defaults,
|
||||
...(options || {}),
|
||||
};
|
||||
options = merge({}, defaults, options);
|
||||
|
||||
const schema: Record<string, AnySchema> = {};
|
||||
|
||||
@@ -98,26 +97,26 @@ export default function generateJoi(filter: Record<string, any> | null, options?
|
||||
}
|
||||
|
||||
if (operator === '_starts_with') {
|
||||
return Joi.string().pattern(new RegExp(`^${escapeRegExp(Object.values(value)[0] as string)}.*`), {
|
||||
schema[key] = Joi.string().pattern(new RegExp(`^${escapeRegExp(Object.values(value)[0] as string)}.*`), {
|
||||
name: 'starts_with',
|
||||
});
|
||||
}
|
||||
|
||||
if (operator === '_nstarts_with') {
|
||||
return Joi.string().pattern(new RegExp(`^${escapeRegExp(Object.values(value)[0] as string)}.*`), {
|
||||
schema[key] = Joi.string().pattern(new RegExp(`^${escapeRegExp(Object.values(value)[0] as string)}.*`), {
|
||||
name: 'starts_with',
|
||||
invert: true,
|
||||
});
|
||||
}
|
||||
|
||||
if (operator === '_ends_with') {
|
||||
return Joi.string().pattern(new RegExp(`.*${escapeRegExp(Object.values(value)[0] as string)}$`), {
|
||||
schema[key] = Joi.string().pattern(new RegExp(`.*${escapeRegExp(Object.values(value)[0] as string)}$`), {
|
||||
name: 'ends_with',
|
||||
});
|
||||
}
|
||||
|
||||
if (operator === '_nends_with') {
|
||||
return Joi.string().pattern(new RegExp(`.*${escapeRegExp(Object.values(value)[0] as string)}$`), {
|
||||
schema[key] = Joi.string().pattern(new RegExp(`.*${escapeRegExp(Object.values(value)[0] as string)}$`), {
|
||||
name: 'ends_with',
|
||||
invert: true,
|
||||
});
|
||||
@@ -172,6 +171,10 @@ export default function generateJoi(filter: Record<string, any> | null, options?
|
||||
const values = Object.values(value)[0] as number[];
|
||||
schema[key] = Joi.number().less(values[0]).greater(values[1]);
|
||||
}
|
||||
|
||||
if (options.requireAll) {
|
||||
schema[key] = schema[key].required();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user