All files / src/features/properties schemas.ts

100% Statements 42/42
100% Branches 23/23
100% Functions 8/8
100% Lines 41/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125                  51x   3x   3x 3x   3x 6x           3x           3x       9x       1152x 1152x 1082x   70x 2x 2x   68x 68x 21x 21x   47x             3x                       129x       384x 384x 266x 266x   118x       28x                     3x         360x 280x   80x 80x 43x 43x   37x 1x       1x   36x           3x   3x      
// Flat details, shared by the form and the server functions. Errors are
// reason codes (src/lib/reasons.ts), never text.
import { z } from 'zod'
import { isoDateInFinland, parseFiDate } from '#/lib/dates'
import { isValidBusinessId } from '#/lib/business-id'
import { parseDecimal } from '#/lib/decimal'
import type { FieldReason } from '#/lib/reasons'
import type { RoomCount } from './property'
 
const reason = (code: FieldReason) => ({ error: code })
 
const MAX_TEXT = 100
/** Floor area limits in tenths of m²: 1–10 000 m². */
const MIN_AREA = 10
const MAX_AREA = 100_000
 
const requiredText = (code: FieldReason) =>
  z
    .string(reason(code))
    .trim()
    .min(1, reason(code))
    .max(MAX_TEXT, reason('text_too_long'))
 
const MAX_APARTMENT_NUMBER = 20
 
/**
 * An optional text: spaces around it are dropped and an empty one is
 * missing (undefined).
 */
const optionalText = (
  max: number,
  check?: (text: string) => FieldReason | undefined,
) =>
  z
    .string(reason('text_too_long'))
    .optional()
    .transform((text, context) => {
      const trimmed = text?.trim()
      if (trimmed === undefined || trimmed === '') {
        return undefined
      }
      if (trimmed.length > max) {
        context.addIssue({ code: 'custom', message: 'text_too_long' })
        return z.NEVER
      }
      const failure = check?.(trimmed)
      if (failure) {
        context.addIssue({ code: 'custom', message: failure })
        return z.NEVER
      }
      return trimmed
    })
 
/**
 * The details as the form holds them: the floor area as typed ("48,5"), the
 * room count 0 until one is chosen. Parsed into PropertyDetails.
 */
export const propertyDetailsInput = z.object({
  streetAddress: requiredText('street_address_required'),
  postcode: z
    .string(reason('postcode_invalid'))
    .trim()
    .regex(/^\d{5}$/, reason('postcode_invalid')),
  city: requiredText('city_required'),
  roomCount: z
    .number(reason('room_count_required'))
    .int(reason('room_count_required'))
    .min(1, reason('room_count_required'))
    .max(4, reason('room_count_required'))
    .transform((count) => count as RoomCount),
  floorArea: z
    .string(reason('floor_area_invalid'))
    .transform((text, context) => {
      const tenths = parseDecimal(text, 1)
      if (tenths === undefined || tenths < MIN_AREA || tenths > MAX_AREA) {
        context.addIssue({ code: 'custom', message: 'floor_area_invalid' })
        return z.NEVER
      }
      return tenths
    }),
  housingCompanyName: optionalText(MAX_TEXT),
  businessId: optionalText(MAX_TEXT, (text) =>
    isValidBusinessId(text) ? undefined : 'business_id_invalid',
  ),
  apartmentNumber: optionalText(MAX_APARTMENT_NUMBER),
})
 
export type PropertyDetailsInput = z.input<typeof propertyDetailsInput>
 
/**
 * A new flat also has the date its owner has owned it since, as typed
 * ("12.6.2022"); missing means today (spec: "The first ownership period").
 */
export const newPropertyInput = propertyDetailsInput.extend({
  ownedSince: z
    .string(reason('date_invalid'))
    .optional()
    .transform((text, context) => {
      if (text === undefined || text.trim() === '') {
        return undefined
      }
      const date = parseFiDate(text)
      if (date === undefined) {
        context.addIssue({ code: 'custom', message: 'date_invalid' })
        return z.NEVER
      }
      if (date > isoDateInFinland(new Date())) {
        context.addIssue({
          code: 'custom',
          message: 'ownership_date_in_future',
        })
        return z.NEVER
      }
      return date
    }),
})
 
export type NewPropertyInput = z.input<typeof newPropertyInput>
 
export const propertyIdInput = z.object({ id: z.string().min(1).max(64) })
 
export const updatePropertyInput = propertyDetailsInput.extend(
  propertyIdInput.shape,
)