Files
supabase/apps/studio/components/interfaces/Integrations/CronJobs/CreateCronJobSheet/CreateCronJobSheet.constants.test.ts
Danny White 3a72b128de chore(studio): standardise key-value field array partial-row validation (#44411)
## What kind of change does this PR introduce?

Design system and validation consistency update.

## What is the current behaviour?

`KeyValueFieldArray` already renders per-cell form messages, but each
consumer still decides its own validation rules. At the moment, some
consumers allow partially filled rows to submit silently, while Log
Drains now treats them as inline validation errors.

## What is the new behaviour?

This PR standardises the recommended partial-row behaviour for the
current `KeyValueFieldArray` consumers by introducing a shared
validation helper and using it from each form schema.

- adds `getKeyValueFieldArrayValidationIssues` alongside
`KeyValueFieldArray`
- keeps `KeyValueFieldArray` presentation-only and leaves validation in
consumer schemas
- shows inline errors when one side of a key/value row is filled and the
other is empty
- keeps fully empty rows as draft rows
- keeps duplicate-key validation in Log Drains, where it already applies
- updates the design-system docs and examples to describe the validation
pattern explicitly


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Added reusable key/value validation utilities and public export; forms
now trim header/key/value inputs, show inline errors for partially
filled rows, and remove fully empty draft rows on submit.

* **Documentation**
* Clarified the field-array is rendering-only and added guidance for
placing validation in form schemas and handling draft rows.

* **Tests**
* Added unit and integration tests covering validation rules, duplicate
keys, trimming, draft-row stripping, and payload behavior.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-04-02 12:49:25 +11:00

100 lines
2.7 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { FormSchema } from './CreateCronJobSheet.constants'
describe('CreateCronJobSheet FormSchema', () => {
it('rejects incomplete http_request hostnames', () => {
const result = FormSchema.safeParse({
name: 'Send webhook',
supportsSeconds: false,
schedule: '* * * * *',
values: {
type: 'http_request' as const,
method: 'POST' as const,
endpoint: 'https://webhook',
timeoutMs: 1000,
httpHeaders: [],
snippet: '',
},
})
expect(result.success).toBe(false)
if (!result.success) {
expect(
result.error.issues.some((issue) => issue.message === 'Please provide a valid URL')
).toBe(true)
}
})
it('rejects http_request URLs without an explicit protocol', () => {
const result = FormSchema.safeParse({
name: 'Send webhook',
supportsSeconds: false,
schedule: '* * * * *',
values: {
type: 'http_request' as const,
method: 'POST' as const,
endpoint: 'hooks.example.com/webhook',
timeoutMs: 1000,
httpHeaders: [],
snippet: '',
},
})
expect(result.success).toBe(false)
if (!result.success) {
expect(
result.error.issues.some(
(issue) => issue.message === 'Please prefix your URL with http:// or https://'
)
).toBe(true)
}
})
it('rejects key-only http_request headers', () => {
const result = FormSchema.safeParse({
name: 'Send webhook',
supportsSeconds: false,
schedule: '* * * * *',
values: {
type: 'http_request' as const,
method: 'POST' as const,
endpoint: 'https://hooks.example.com/webhook',
timeoutMs: 1000,
httpHeaders: [{ name: 'X-Test', value: '' }],
snippet: '',
},
})
expect(result.success).toBe(false)
if (!result.success) {
expect(
result.error.issues.some((issue) => issue.message === 'Header value is required')
).toBe(true)
}
})
it('rejects value-only edge function headers', () => {
const result = FormSchema.safeParse({
name: 'Invoke edge function',
supportsSeconds: false,
schedule: '* * * * *',
values: {
type: 'edge_function' as const,
method: 'POST' as const,
edgeFunctionName: 'my-function',
timeoutMs: 1000,
httpHeaders: [{ name: '', value: 'test-value' }],
snippet: '',
},
})
expect(result.success).toBe(false)
if (!result.success) {
expect(result.error.issues.some((issue) => issue.message === 'Header name is required')).toBe(
true
)
}
})
})