Commit 97ae49ee authored by Stefan Probst's avatar Stefan Probst
Browse files

feat: validate date fields

parent cbecf4b3
......@@ -11,7 +11,8 @@ import { RelatedItemsFormSection } from '@/components/item/RelatedItemsFormSecti
import { SourceFormSection } from '@/components/item/SourceFormSection/SourceFormSection'
import { Button } from '@/elements/Button/Button'
import { useToast } from '@/elements/Toast/useToast'
import { validate } from '@/lib/sshoc/validate'
import { validateCommonFormFields } from '@/lib/sshoc/validateCommonFormFields'
import { validateDateFormFields } from '@/lib/sshoc/validateDateFormFields'
import { useAuth } from '@/modules/auth/AuthContext'
import { Form } from '@/modules/form/Form'
import { getSingularItemCategoryLabel } from '@/utils/getSingularItemCategoryLabel'
......@@ -78,7 +79,8 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const errors: Partial<Record<keyof typeof values, any>> = {}
validate(values, errors)
validateCommonFormFields(values, errors)
validateDateFormFields(values, errors)
return errors
}
......
......@@ -11,7 +11,8 @@ import { RelatedItemsFormSection } from '@/components/item/RelatedItemsFormSecti
import { SourceFormSection } from '@/components/item/SourceFormSection/SourceFormSection'
import { Button } from '@/elements/Button/Button'
import { useToast } from '@/elements/Toast/useToast'
import { validate } from '@/lib/sshoc/validate'
import { validateCommonFormFields } from '@/lib/sshoc/validateCommonFormFields'
import { validateDateFormFields } from '@/lib/sshoc/validateDateFormFields'
import { useAuth } from '@/modules/auth/AuthContext'
import { Form } from '@/modules/form/Form'
import { getSingularItemCategoryLabel } from '@/utils/getSingularItemCategoryLabel'
......@@ -84,7 +85,8 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
function onValidate(values: Partial<ItemFormValues>) {
const errors: Partial<Record<keyof typeof values, string>> = {}
validate(values, errors)
validateCommonFormFields(values, errors)
validateDateFormFields(values, errors)
return errors
}
......
......@@ -11,7 +11,8 @@ import { RelatedItemsFormSection } from '@/components/item/RelatedItemsFormSecti
import { SourceFormSection } from '@/components/item/SourceFormSection/SourceFormSection'
import { Button } from '@/elements/Button/Button'
import { useToast } from '@/elements/Toast/useToast'
import { validate } from '@/lib/sshoc/validate'
import { validateCommonFormFields } from '@/lib/sshoc/validateCommonFormFields'
import { validateDateFormFields } from '@/lib/sshoc/validateDateFormFields'
import { useAuth } from '@/modules/auth/AuthContext'
import { Form } from '@/modules/form/Form'
import { getSingularItemCategoryLabel } from '@/utils/getSingularItemCategoryLabel'
......@@ -77,7 +78,8 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
function onValidate(values: Partial<ItemFormValues>) {
const errors: Partial<Record<keyof typeof values, string>> = {}
validate(values, errors)
validateCommonFormFields(values, errors)
validateDateFormFields(values, errors)
return errors
}
......
......@@ -11,7 +11,8 @@ import { RelatedItemsFormSection } from '@/components/item/RelatedItemsFormSecti
import { SourceFormSection } from '@/components/item/SourceFormSection/SourceFormSection'
import { Button } from '@/elements/Button/Button'
import { useToast } from '@/elements/Toast/useToast'
import { validate } from '@/lib/sshoc/validate'
import { validateCommonFormFields } from '@/lib/sshoc/validateCommonFormFields'
import { validateDateFormFields } from '@/lib/sshoc/validateDateFormFields'
import { useAuth } from '@/modules/auth/AuthContext'
import { Form } from '@/modules/form/Form'
import { getSingularItemCategoryLabel } from '@/utils/getSingularItemCategoryLabel'
......@@ -84,7 +85,8 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
function onValidate(values: Partial<ItemFormValues>) {
const errors: Partial<Record<keyof typeof values, string>> = {}
validate(values, errors)
validateCommonFormFields(values, errors)
validateDateFormFields(values, errors)
return errors
}
......
......@@ -10,7 +10,7 @@ import { RelatedItemsFormSection } from '@/components/item/RelatedItemsFormSecti
import { SourceFormSection } from '@/components/item/SourceFormSection/SourceFormSection'
import { Button } from '@/elements/Button/Button'
import { useToast } from '@/elements/Toast/useToast'
import { validate } from '@/lib/sshoc/validate'
import { validateCommonFormFields } from '@/lib/sshoc/validateCommonFormFields'
import { useAuth } from '@/modules/auth/AuthContext'
import { Form } from '@/modules/form/Form'
import { getSingularItemCategoryLabel } from '@/utils/getSingularItemCategoryLabel'
......@@ -76,7 +76,7 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
function onValidate(values: Partial<ItemFormValues>) {
const errors: Partial<Record<keyof typeof values, string>> = {}
validate(values, errors)
validateCommonFormFields(values, errors)
return errors
}
......
......@@ -10,7 +10,7 @@ import { RelatedItemsFormSection } from '@/components/item/RelatedItemsFormSecti
import { SourceFormSection } from '@/components/item/SourceFormSection/SourceFormSection'
import { Button } from '@/elements/Button/Button'
import { useToast } from '@/elements/Toast/useToast'
import { validate } from '@/lib/sshoc/validate'
import { validateCommonFormFields } from '@/lib/sshoc/validateCommonFormFields'
import { useAuth } from '@/modules/auth/AuthContext'
import { Form } from '@/modules/form/Form'
import { getSingularItemCategoryLabel } from '@/utils/getSingularItemCategoryLabel'
......@@ -83,7 +83,7 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
function onValidate(values: Partial<ItemFormValues>) {
const errors: Partial<Record<keyof typeof values, string>> = {}
validate(values, errors)
validateCommonFormFields(values, errors)
return errors
}
......
......@@ -10,7 +10,7 @@ import { RelatedItemsFormSection } from '@/components/item/RelatedItemsFormSecti
import { SourceFormSection } from '@/components/item/SourceFormSection/SourceFormSection'
import { Button } from '@/elements/Button/Button'
import { useToast } from '@/elements/Toast/useToast'
import { validate } from '@/lib/sshoc/validate'
import { validateCommonFormFields } from '@/lib/sshoc/validateCommonFormFields'
import { useAuth } from '@/modules/auth/AuthContext'
import { Form } from '@/modules/form/Form'
import { getSingularItemCategoryLabel } from '@/utils/getSingularItemCategoryLabel'
......@@ -76,7 +76,7 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
function onValidate(values: Partial<ItemFormValues>) {
const errors: Partial<Record<keyof typeof values, string>> = {}
validate(values, errors)
validateCommonFormFields(values, errors)
return errors
}
......
......@@ -10,7 +10,7 @@ import { RelatedItemsFormSection } from '@/components/item/RelatedItemsFormSecti
import { SourceFormSection } from '@/components/item/SourceFormSection/SourceFormSection'
import { Button } from '@/elements/Button/Button'
import { useToast } from '@/elements/Toast/useToast'
import { validate } from '@/lib/sshoc/validate'
import { validateCommonFormFields } from '@/lib/sshoc/validateCommonFormFields'
import { useAuth } from '@/modules/auth/AuthContext'
import { Form } from '@/modules/form/Form'
import { getSingularItemCategoryLabel } from '@/utils/getSingularItemCategoryLabel'
......@@ -83,7 +83,7 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
function onValidate(values: Partial<ItemFormValues>) {
const errors: Partial<Record<keyof typeof values, string>> = {}
validate(values, errors)
validateCommonFormFields(values, errors)
return errors
}
......
......@@ -10,7 +10,7 @@ import { RelatedItemsFormSection } from '@/components/item/RelatedItemsFormSecti
import { SourceFormSection } from '@/components/item/SourceFormSection/SourceFormSection'
import { Button } from '@/elements/Button/Button'
import { useToast } from '@/elements/Toast/useToast'
import { validate } from '@/lib/sshoc/validate'
import { validateCommonFormFields } from '@/lib/sshoc/validateCommonFormFields'
import { useAuth } from '@/modules/auth/AuthContext'
import { Form } from '@/modules/form/Form'
import { getSingularItemCategoryLabel } from '@/utils/getSingularItemCategoryLabel'
......@@ -76,7 +76,7 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
function onValidate(values: Partial<ItemFormValues>) {
const errors: Partial<Record<keyof typeof values, string>> = {}
validate(values, errors)
validateCommonFormFields(values, errors)
return errors
}
......
......@@ -10,7 +10,7 @@ import { RelatedItemsFormSection } from '@/components/item/RelatedItemsFormSecti
import { SourceFormSection } from '@/components/item/SourceFormSection/SourceFormSection'
import { Button } from '@/elements/Button/Button'
import { useToast } from '@/elements/Toast/useToast'
import { validate } from '@/lib/sshoc/validate'
import { validateCommonFormFields } from '@/lib/sshoc/validateCommonFormFields'
import { useAuth } from '@/modules/auth/AuthContext'
import { Form } from '@/modules/form/Form'
import { getSingularItemCategoryLabel } from '@/utils/getSingularItemCategoryLabel'
......@@ -83,7 +83,7 @@ export function ItemForm(props: ItemFormProps<ItemFormValues>): JSX.Element {
function onValidate(values: Partial<ItemFormValues>) {
const errors: Partial<Record<keyof typeof values, string>> = {}
validate(values, errors)
validateCommonFormFields(values, errors)
return errors
}
......
......@@ -7,17 +7,14 @@ import type {
} from '@/api/sshoc'
import { isUrl } from '@/modules/form/validate'
export function validate<
export function validateCommonFormFields<
T extends
| DatasetCore
| PublicationCore
| ToolCore
| TrainingMaterialCore
| WorkflowCore
>(
values: Partial<T>,
errors: Partial<Record<keyof typeof values, any>>,
): Partial<Record<keyof typeof values, any>> {
>(values: Partial<T>, errors: Partial<Record<keyof typeof values, any>>): void {
/** Required field `label`. */
if (values.label === undefined) {
errors.label = 'Label is required.'
......@@ -164,6 +161,4 @@ export function validate<
if (values.sourceItemId != null && values.source?.id == null) {
errors.sourceItemId = 'Missing value in Source.'
}
return errors
}
import type { DatasetCore, PublicationCore } from '@/api/sshoc'
import { isDate } from '@/modules/form/validate'
export function validateDateFormFields<T extends DatasetCore | PublicationCore>(
values: Partial<T>,
errors: Partial<Record<keyof typeof values, any>>,
): void {
if (
values.dateLastUpdated !== undefined &&
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
!isDate(values.dateLastUpdated!)
) {
errors.dateLastUpdated = 'Must be a valid date.'
}
if (
values.dateLastUpdated !== undefined &&
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
!isDate(values.dateLastUpdated!)
) {
errors.dateLastUpdated = 'Must be a valid date.'
}
}
......@@ -14,3 +14,7 @@ export function isUrl(str: string): boolean {
return false
}
}
export function isDate(str: string): boolean {
return !Number.isNaN(new Date(str).getDate())
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment