Skip to content

Commit

Permalink
feat(core): init filter types
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Dec 6, 2022
1 parent 2e301d2 commit 6fb0b57
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions packages/core/filter/operators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { z } from 'zod'
import type { INumberFieldValue } from '../field/number-field.type'
import { numberFieldValue } from '../field/number-field.type'
import type { ITextFieldValue } from '../field/text-field.type'
import { textFieldValue } from '../field/text-field.type'

type Filter<TSchema extends Record<string, ComparableFieldValue> = Record<string, ComparableFieldValue>> =
| RootOperator
| TSchema
| Record<string, Operator>

export interface RootOperator {
$and?: Filter[]
$or?: Filter[]
}

type ComparableFieldValue = ITextFieldValue | INumberFieldValue

type FieldOperator = {
$eq?: ComparableFieldValue
$neq?: ComparableFieldValue
}

export type Operator = ComparableFieldValue | FieldOperator

const $comparableFieldValue: z.ZodOptional<z.ZodType<ComparableFieldValue>> = z
.union([textFieldValue, numberFieldValue])
.optional()

export const $fieldOperator: z.ZodType<FieldOperator> = z.object({
$eq: $comparableFieldValue,
$neq: $comparableFieldValue,
})

export const $filter: z.ZodType<Filter> = z.lazy(() =>
z.union([$rootOperator, z.record(z.union([$comparableFieldValue, $fieldOperator]))]),
)
export type IFilter = z.infer<typeof $filter>

const $filters = z.array($filter).optional()
const $rootOperator: z.ZodType<RootOperator> = z.object({
$and: $filters,
$or: $filters,
})

0 comments on commit 6fb0b57

Please sign in to comment.