fix: $contains may have only string value additionally in an entity filter.
Signed-off-by: Sandor Karpf <sandor.karpf@lastpass.com>
This commit is contained in:
@@ -478,7 +478,7 @@ export const EntityIconLinkBlueprint: ExtensionBlueprint<{
|
||||
export type EntityPredicate =
|
||||
| EntityPredicateExpression
|
||||
| EntityPredicatePrimitive
|
||||
| EntityPredicatePrimitive[]
|
||||
| never[]
|
||||
| {
|
||||
$all: EntityPredicate[];
|
||||
}
|
||||
@@ -515,7 +515,7 @@ export type EntityPredicateValue =
|
||||
$in: EntityPredicatePrimitive[];
|
||||
}
|
||||
| {
|
||||
$contains: EntityPredicate;
|
||||
$contains: EntityPredicateExpression | string;
|
||||
};
|
||||
|
||||
// @alpha
|
||||
|
||||
@@ -91,6 +91,7 @@ describe('createEntityPredicateSchema', () => {
|
||||
const predicates: Array<
|
||||
Exclude<EntityPredicate | unknown, EntityPredicate>
|
||||
> = [
|
||||
['service', 'website'],
|
||||
{ kind: { 1: 'foo' } },
|
||||
{ kind: { foo: 'bar' } },
|
||||
{ kind: { $unknown: 'foo' } },
|
||||
|
||||
@@ -14,36 +14,51 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { EntityPredicate, EntityPredicateValue } from './types';
|
||||
import {
|
||||
EntityPredicate,
|
||||
EntityPredicateExpression,
|
||||
EntityPredicatePrimitive,
|
||||
EntityPredicateValue,
|
||||
} from './types';
|
||||
import type { z as zImpl, ZodType } from 'zod';
|
||||
|
||||
/** @internal */
|
||||
export function createEntityPredicateSchema(z: typeof zImpl) {
|
||||
const primitiveSchema = z.union([z.string(), z.number(), z.boolean()]);
|
||||
const primitiveSchema = z.union([
|
||||
z.string(),
|
||||
z.number(),
|
||||
z.boolean(),
|
||||
]) as ZodType<EntityPredicatePrimitive>;
|
||||
|
||||
const comparableValueSchema = z.union([
|
||||
primitiveSchema,
|
||||
z.array(primitiveSchema),
|
||||
]);
|
||||
const onlyEmptyArraySchema = z.array(z.never()) as ZodType<never[]>;
|
||||
|
||||
// eslint-disable-next-line prefer-const
|
||||
let valuePredicateSchema: ZodType<EntityPredicateValue>;
|
||||
|
||||
const expressionSchema = z.lazy(() =>
|
||||
z.union([
|
||||
z.record(z.string().regex(/^(?!\$).*$/), valuePredicateSchema),
|
||||
z.record(z.string().regex(/(?!\$)+/), z.never()),
|
||||
]),
|
||||
) as ZodType<EntityPredicateExpression>;
|
||||
|
||||
const predicateSchema = z.lazy(() =>
|
||||
z.union([
|
||||
comparableValueSchema,
|
||||
expressionSchema,
|
||||
primitiveSchema,
|
||||
onlyEmptyArraySchema,
|
||||
z.object({ $all: z.array(predicateSchema) }),
|
||||
z.object({ $any: z.array(predicateSchema) }),
|
||||
z.object({ $not: predicateSchema }),
|
||||
z.record(z.string().regex(/^(?!\$).*$/), valuePredicateSchema),
|
||||
]),
|
||||
) as ZodType<EntityPredicate>;
|
||||
|
||||
valuePredicateSchema = z.union([
|
||||
comparableValueSchema,
|
||||
primitiveSchema,
|
||||
z.array(primitiveSchema),
|
||||
z.object({ $exists: z.boolean() }),
|
||||
z.object({ $in: z.array(primitiveSchema) }),
|
||||
z.object({ $contains: predicateSchema }),
|
||||
z.object({ $contains: z.union([expressionSchema, z.string()]) }),
|
||||
]) as ZodType<EntityPredicateValue>;
|
||||
|
||||
return predicateSchema;
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
export type EntityPredicate =
|
||||
| EntityPredicateExpression
|
||||
| EntityPredicatePrimitive
|
||||
| EntityPredicatePrimitive[]
|
||||
| never[]
|
||||
| { $all: EntityPredicate[] }
|
||||
| { $any: EntityPredicate[] }
|
||||
| { $not: EntityPredicate };
|
||||
@@ -36,7 +36,7 @@ export type EntityPredicateValue =
|
||||
| EntityPredicatePrimitive[]
|
||||
| { $exists: boolean }
|
||||
| { $in: EntityPredicatePrimitive[] }
|
||||
| { $contains: EntityPredicate };
|
||||
| { $contains: EntityPredicateExpression | string };
|
||||
|
||||
/** @alpha */
|
||||
export type EntityPredicatePrimitive = string | number | boolean;
|
||||
|
||||
Reference in New Issue
Block a user