From 50fba1a9093c9929dd7bdc88c6d204bf95c74212 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Tue, 16 Nov 2021 23:25:35 +0100 Subject: [PATCH] [api] Pass Date objects to IsoDateInput validators instead of string. --- api/components/IsoDateInput.vue | 18 ++++++++++++++++-- api/src/utils/validators.ts | 2 +- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/api/components/IsoDateInput.vue b/api/components/IsoDateInput.vue index 48ed355..ac873e0 100644 --- a/api/components/IsoDateInput.vue +++ b/api/components/IsoDateInput.vue @@ -54,7 +54,7 @@ export default defineComponent({ label: { type: String, default: 'Datum' }, readonly: Boolean, rules: { - type: Array as PropType, + type: Array as PropType[]>, default: () => [], }, }, @@ -62,7 +62,21 @@ export default defineComponent({ setup(props, { emit, attrs }) { const customRules = computed(() => [ props.type == 'date' ? stringIsDate : props.type == 'time' ? stringIsTime : stringIsDateTime, - ...props.rules, + (value?: string | null) => { + if (props.rules.length > 0 && value) { + let date: Date | undefined = undefined; + if (props.type == 'date') date = modifyDate(value); + else if (props.type == 'time') date = modifyTime(value); + else { + const split = value.split(' '); + date = modifyTime(split[1], modifyDate(split[0])); + } + props.rules.forEach((rule) => { + const r = rule(date); + if (typeof r === 'string') return r; + }); + } + }, ]); const clearable = computed(() => diff --git a/api/src/utils/validators.ts b/api/src/utils/validators.ts index e77a436..2db8e8c 100644 --- a/api/src/utils/validators.ts +++ b/api/src/utils/validators.ts @@ -1,4 +1,4 @@ -export type Validator = (value: unknown) => boolean | string; +export type Validator = (value?: T | null) => boolean | string; export function notEmpty(val: unknown) { return !!val || 'Feld darf nicht leer sein!';