mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 18:54:17 +00:00
Describe the journey bodies instead of tightening them
Sixteen of the thirty-four entries in the body-contract allow list were the
JourneyController, and a strict schema already existed for six of them. That
is the trap: eight of these handlers validate their own body and answer with
their own text - 'Title is required', 'trip_id required', 'provider and
asset_id required', 'entry_date is required', 'user_id required' - and those
bodies are pinned by e2e and integration cases. Adopting the strict schemas
would have the pipe answer first, with a different body. That is a
client-visible change wearing the costume of a refactor.
So the schemas follow the memories doctrine instead: loose objects, and every
field the handler type-checks itself is unknown here. They describe the shape
that already works, which is what the boot gate needs, and every hand-rolled
400 keeps answering. The spec inverts accordingly - it now asserts that each
schema ACCEPTS what the handler still rejects, which is the property that
makes the claim checkable.
Two of them are optional at the top level, and that is not decoration: a
multipart upload with files and no text fields arrives with no body at all,
and JOURNEY-INT-045 pins 'No files uploaded' for it. Without the optional the
pipe answered 'body: Invalid input: expected object' instead. The suite
caught it.
Backup joins them - its schema was already a passthrough, because the service
normalises the body itself.
Allow list: 34 entries down to 17. Tags and the two OAuth entries stay for
now; tags reads @Body('name') per property, which is a signature change
rather than a contract one.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import {
|
||||
journeyCreateRequestSchema,
|
||||
journeyAddTripRequestSchema,
|
||||
@@ -5,67 +6,91 @@ import {
|
||||
journeyContributorRequestSchema,
|
||||
journeyProviderPhotosRequestSchema,
|
||||
journeyShareLinkRequestSchema,
|
||||
journeyUpdateRequestSchema,
|
||||
journeyPreferencesRequestSchema,
|
||||
journeyLinkPhotoRequestSchema,
|
||||
} from './journey.schema';
|
||||
|
||||
import { describe, it, expect } from 'vitest';
|
||||
|
||||
/**
|
||||
* These schemas describe the journey bodies rather than tighten them, and the
|
||||
* cases below are what makes that checkable: every shape the handler still
|
||||
* rejects itself has to PARSE here, or the global pipe would answer first with
|
||||
* a different error body than the one the e2e cases pin.
|
||||
*
|
||||
* This inverts the assertions the file carried while the schemas were strict.
|
||||
* That was the point of the change.
|
||||
*/
|
||||
describe('journeyCreateRequestSchema', () => {
|
||||
it('requires a title; subtitle + trip_ids optional', () => {
|
||||
it('accepts the well-formed body', () => {
|
||||
expect(journeyCreateRequestSchema.safeParse({ title: 'Trip of a lifetime' }).success).toBe(true);
|
||||
expect(journeyCreateRequestSchema.safeParse({ title: 'X', trip_ids: [1, '2'] }).success).toBe(true);
|
||||
expect(journeyCreateRequestSchema.safeParse({ subtitle: 'no title' }).success).toBe(false);
|
||||
});
|
||||
|
||||
it('leaves the missing/blank/non-string title to the handler', () => {
|
||||
// The handler answers 'Title is required' for all three.
|
||||
expect(journeyCreateRequestSchema.safeParse({ subtitle: 'no title' }).success).toBe(true);
|
||||
expect(journeyCreateRequestSchema.safeParse({ title: ' ' }).success).toBe(true);
|
||||
expect(journeyCreateRequestSchema.safeParse({ title: 42 }).success).toBe(true);
|
||||
});
|
||||
|
||||
it('leaves a non-array trip_ids to the handler, which reads it as none', () => {
|
||||
expect(journeyCreateRequestSchema.safeParse({ title: 'X', trip_ids: 'nope' }).success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('journeyAddTripRequestSchema', () => {
|
||||
it('requires a trip_id (string or number)', () => {
|
||||
it('accepts an id and leaves the missing one to the handler', () => {
|
||||
expect(journeyAddTripRequestSchema.safeParse({ trip_id: 5 }).success).toBe(true);
|
||||
expect(journeyAddTripRequestSchema.safeParse({ trip_id: '5' }).success).toBe(true);
|
||||
expect(journeyAddTripRequestSchema.safeParse({}).success).toBe(false);
|
||||
// The handler answers 'trip_id required'.
|
||||
expect(journeyAddTripRequestSchema.safeParse({}).success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('journeyReorderEntriesRequestSchema', () => {
|
||||
it('requires a non-empty orderedIds array', () => {
|
||||
expect(journeyReorderEntriesRequestSchema.safeParse({ orderedIds: [3, 1, 2] }).success).toBe(true);
|
||||
expect(journeyReorderEntriesRequestSchema.safeParse({ orderedIds: [] }).success).toBe(false);
|
||||
it('accepts a list, and an empty one, which the handler short-circuits', () => {
|
||||
expect(journeyReorderEntriesRequestSchema.safeParse({ orderedIds: [3, '1'] }).success).toBe(true);
|
||||
expect(journeyReorderEntriesRequestSchema.safeParse({ orderedIds: [] }).success).toBe(true);
|
||||
expect(journeyReorderEntriesRequestSchema.safeParse({}).success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('journeyContributorRequestSchema', () => {
|
||||
it('requires user_id; role limited to editor/viewer', () => {
|
||||
expect(journeyContributorRequestSchema.safeParse({ user_id: 2 }).success).toBe(true);
|
||||
expect(journeyContributorRequestSchema.safeParse({ user_id: 2, role: 'editor' }).success).toBe(true);
|
||||
expect(journeyContributorRequestSchema.safeParse({ user_id: 2, role: 'admin' }).success).toBe(false);
|
||||
it('accepts the roles and leaves an unknown one to the handler', () => {
|
||||
expect(journeyContributorRequestSchema.safeParse({ user_id: 3, role: 'editor' }).success).toBe(true);
|
||||
expect(journeyContributorRequestSchema.safeParse({ user_id: 3 }).success).toBe(true);
|
||||
// Never validated: the handler casts it.
|
||||
expect(journeyContributorRequestSchema.safeParse({ user_id: 3, role: 'admin' }).success).toBe(true);
|
||||
// The handler answers 'user_id required'.
|
||||
expect(journeyContributorRequestSchema.safeParse({}).success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('journeyProviderPhotosRequestSchema', () => {
|
||||
it('requires a provider; accepts single asset_id or a batch', () => {
|
||||
expect(
|
||||
journeyProviderPhotosRequestSchema.safeParse({
|
||||
provider: 'immich',
|
||||
asset_id: 'a1',
|
||||
}).success,
|
||||
).toBe(true);
|
||||
expect(
|
||||
journeyProviderPhotosRequestSchema.safeParse({
|
||||
provider: 'immich',
|
||||
asset_ids: ['a1', 'a2'],
|
||||
}).success,
|
||||
).toBe(true);
|
||||
expect(journeyProviderPhotosRequestSchema.safeParse({ asset_id: 'a1' }).success).toBe(false);
|
||||
it('accepts single and batch, and leaves the incomplete pair to the handler', () => {
|
||||
expect(journeyProviderPhotosRequestSchema.safeParse({ provider: 'immich', asset_id: 'a1' }).success).toBe(true);
|
||||
expect(journeyProviderPhotosRequestSchema.safeParse({ provider: 'immich', asset_ids: ['a1', 2], media_types: ['video'] }).success).toBe(true);
|
||||
// The handler answers 'provider and asset_id required'.
|
||||
expect(journeyProviderPhotosRequestSchema.safeParse({ asset_id: 'a1' }).success).toBe(true);
|
||||
expect(journeyProviderPhotosRequestSchema.safeParse({}).success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('journeyShareLinkRequestSchema', () => {
|
||||
it('accepts optional share toggles', () => {
|
||||
expect(
|
||||
journeyShareLinkRequestSchema.safeParse({
|
||||
share_timeline: true,
|
||||
share_gallery: false,
|
||||
}).success,
|
||||
).toBe(true);
|
||||
it('accepts the flags in whatever shape the client sends them', () => {
|
||||
expect(journeyShareLinkRequestSchema.safeParse({ share_timeline: true, share_gallery: false }).success).toBe(true);
|
||||
expect(journeyShareLinkRequestSchema.safeParse({}).success).toBe(true);
|
||||
// The controller coerces with !!; a string flag has always worked.
|
||||
expect(journeyShareLinkRequestSchema.safeParse({ share_map: 'true' }).success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('the free-form bodies keep their unknown keys', () => {
|
||||
it('does not strip what the services are handed whole', () => {
|
||||
// The whole reason these are loose objects: a strict object drops unknown
|
||||
// keys, and update/preferences/link are forwarded to the service as sent.
|
||||
expect(journeyUpdateRequestSchema.parse({ title: 'X', cover_photo_id: 9 })).toEqual({ title: 'X', cover_photo_id: 9 });
|
||||
expect(journeyPreferencesRequestSchema.parse({ layout: 'grid', density: 2 })).toEqual({ layout: 'grid', density: 2 });
|
||||
expect(journeyLinkPhotoRequestSchema.parse({ journey_photo_id: 4, extra: true })).toEqual({ journey_photo_id: 4, extra: true });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -7,50 +7,126 @@ import { z } from 'zod';
|
||||
*
|
||||
* Authenticated routes live under /api/journeys (gated by the Journey addon);
|
||||
* the public read/photo-proxy routes live under /api/public/journey and are
|
||||
* share-token validated. Access control lives inside journeyService (it returns
|
||||
* null/false → the controller maps to 403/404), so these schemas pin the
|
||||
* well-defined request bodies; entry create/update stay open-ended (forwarded
|
||||
* to the service) and the bespoke 400/403/404 messages pin the rest.
|
||||
* share-token validated. Access control lives inside the journey services (they
|
||||
* return null/false → the controller maps to 403/404).
|
||||
*
|
||||
* Deliberately permissive, the same doctrine as memories (#1842): eight of the
|
||||
* sixteen handlers validate their own body and answer with their own text —
|
||||
* 'Title is required', 'trip_id required', 'provider and asset_id required',
|
||||
* and so on — and those messages are pinned by e2e and integration cases. A
|
||||
* strict schema would have the global pipe answer first, with a different body,
|
||||
* which is a client-visible change wearing the costume of a refactor. So every
|
||||
* field the handler type-checks itself is `unknown` here: the schema describes
|
||||
* the shape that already works, and the boot-time contract gate gets something
|
||||
* to point at.
|
||||
*
|
||||
* They are LOOSE objects for the second memories reason: a strict object strips
|
||||
* unknown keys, and the update/entry/preferences bodies are forwarded to the
|
||||
* services whole. Stripping there would silently drop a field added later.
|
||||
*/
|
||||
|
||||
export const journeyCreateRequestSchema = z.object({
|
||||
title: z.string().min(1),
|
||||
subtitle: z.string().optional(),
|
||||
trip_ids: z.array(z.union([z.string(), z.number()])).optional(),
|
||||
export const journeyCreateRequestSchema = z.looseObject({
|
||||
// The handler does `!body.title || typeof body.title !== 'string' || !trim()`
|
||||
// and answers 'Title is required'. Typing it here would pre-empt that.
|
||||
title: z.unknown().optional(),
|
||||
subtitle: z.unknown().optional(),
|
||||
// `Array.isArray(body.trip_ids) ? body.trip_ids.map(Number) : []` — anything
|
||||
// that is not an array is silently treated as none.
|
||||
trip_ids: z.unknown().optional(),
|
||||
});
|
||||
export type JourneyCreateRequest = z.infer<typeof journeyCreateRequestSchema>;
|
||||
|
||||
export const journeyAddTripRequestSchema = z.object({
|
||||
trip_id: z.union([z.string(), z.number()]),
|
||||
/** Free-form: forwarded to the service as-is. */
|
||||
export const journeyUpdateRequestSchema = z.looseObject({});
|
||||
export type JourneyUpdateRequest = z.infer<typeof journeyUpdateRequestSchema>;
|
||||
|
||||
export const journeyAddTripRequestSchema = z.looseObject({
|
||||
// Handler answers 'trip_id required'.
|
||||
trip_id: z.unknown().optional(),
|
||||
});
|
||||
export type JourneyAddTripRequest = z.infer<typeof journeyAddTripRequestSchema>;
|
||||
|
||||
export const journeyReorderEntriesRequestSchema = z.object({
|
||||
orderedIds: z.array(z.union([z.string(), z.number()])).min(1),
|
||||
export const journeyReorderEntriesRequestSchema = z.looseObject({
|
||||
// No .min(1): an empty list is accepted today and short-circuits in the
|
||||
// handler, and the handler's own 'orderedIds must be...' check is the one
|
||||
// that answers for a malformed list.
|
||||
orderedIds: z.unknown().optional(),
|
||||
});
|
||||
export type JourneyReorderEntriesRequest = z.infer<typeof journeyReorderEntriesRequestSchema>;
|
||||
|
||||
export const journeyContributorRequestSchema = z.object({
|
||||
user_id: z.union([z.string(), z.number()]),
|
||||
role: z.enum(['editor', 'viewer']).optional(),
|
||||
export const journeyContributorRequestSchema = z.looseObject({
|
||||
// Handler answers 'user_id required'; role is cast, never validated.
|
||||
user_id: z.unknown().optional(),
|
||||
role: z.unknown().optional(),
|
||||
});
|
||||
export type JourneyContributorRequest = z.infer<typeof journeyContributorRequestSchema>;
|
||||
|
||||
export const journeyProviderPhotosRequestSchema = z.object({
|
||||
provider: z.string().min(1),
|
||||
asset_id: z.string().optional(),
|
||||
asset_ids: z.array(z.union([z.string(), z.number()])).optional(),
|
||||
caption: z.string().optional(),
|
||||
passphrase: z.string().optional(),
|
||||
export const journeyContributorUpdateRequestSchema = z.looseObject({
|
||||
role: z.unknown().optional(),
|
||||
});
|
||||
export type JourneyContributorUpdateRequest = z.infer<typeof journeyContributorUpdateRequestSchema>;
|
||||
|
||||
export const journeyProviderPhotosRequestSchema = z.looseObject({
|
||||
// Handler answers 'provider and asset_id required' when either is missing,
|
||||
// and branches on Array.isArray(asset_ids) before that.
|
||||
provider: z.unknown().optional(),
|
||||
asset_id: z.unknown().optional(),
|
||||
asset_ids: z.unknown().optional(),
|
||||
caption: z.unknown().optional(),
|
||||
passphrase: z.unknown().optional(),
|
||||
// Per-asset 'image' | 'video' discriminator, parallel to asset_ids (#823).
|
||||
media_type: z.string().optional(),
|
||||
media_types: z.array(z.string()).optional(),
|
||||
media_type: z.unknown().optional(),
|
||||
media_types: z.unknown().optional(),
|
||||
});
|
||||
export type JourneyProviderPhotosRequest = z.infer<typeof journeyProviderPhotosRequestSchema>;
|
||||
|
||||
export const journeyShareLinkRequestSchema = z.object({
|
||||
share_timeline: z.boolean().optional(),
|
||||
share_gallery: z.boolean().optional(),
|
||||
share_map: z.boolean().optional(),
|
||||
export const journeyLinkPhotoRequestSchema = z.looseObject({
|
||||
// Handler answers 'journey_photo_id required'.
|
||||
journey_photo_id: z.unknown().optional(),
|
||||
photo_id: z.unknown().optional(),
|
||||
});
|
||||
export type JourneyLinkPhotoRequest = z.infer<typeof journeyLinkPhotoRequestSchema>;
|
||||
|
||||
/** Free-form: forwarded to the service as-is. */
|
||||
export const journeyPhotoUpdateRequestSchema = z.looseObject({});
|
||||
export type JourneyPhotoUpdateRequest = z.infer<typeof journeyPhotoUpdateRequestSchema>;
|
||||
|
||||
/** Free-form plus the entry_date the handler demands itself. */
|
||||
export const journeyEntryCreateRequestSchema = z.looseObject({
|
||||
entry_date: z.unknown().optional(),
|
||||
});
|
||||
export type JourneyEntryCreateRequest = z.infer<typeof journeyEntryCreateRequestSchema>;
|
||||
|
||||
/** Free-form: forwarded to the service as-is. */
|
||||
export const journeyEntryUpdateRequestSchema = z.looseObject({});
|
||||
export type JourneyEntryUpdateRequest = z.infer<typeof journeyEntryUpdateRequestSchema>;
|
||||
|
||||
/**
|
||||
* Multipart field alongside the uploaded files.
|
||||
*
|
||||
* `.optional()` is not decoration: a multipart request with files and no text
|
||||
* fields arrives with NO body at all, and the handler answers 'No files
|
||||
* uploaded' for the empty case. Without this the pipe would answer
|
||||
* 'body: Invalid input: expected object' first and the message would change.
|
||||
*/
|
||||
export const journeyEntryPhotoUploadRequestSchema = z.looseObject({
|
||||
caption: z.unknown().optional(),
|
||||
}).optional();
|
||||
export type JourneyEntryPhotoUploadRequest = z.infer<typeof journeyEntryPhotoUploadRequestSchema>;
|
||||
|
||||
/** Multipart field alongside the uploaded video — absent body for the same reason. */
|
||||
export const journeyGalleryVideoRequestSchema = z.looseObject({
|
||||
duration_ms: z.unknown().optional(),
|
||||
}).optional();
|
||||
export type JourneyGalleryVideoRequest = z.infer<typeof journeyGalleryVideoRequestSchema>;
|
||||
|
||||
/** Free-form: per-user display preferences, forwarded whole. */
|
||||
export const journeyPreferencesRequestSchema = z.looseObject({});
|
||||
export type JourneyPreferencesRequest = z.infer<typeof journeyPreferencesRequestSchema>;
|
||||
|
||||
export const journeyShareLinkRequestSchema = z.looseObject({
|
||||
share_timeline: z.unknown().optional(),
|
||||
share_gallery: z.unknown().optional(),
|
||||
share_map: z.unknown().optional(),
|
||||
});
|
||||
export type JourneyShareLinkRequest = z.infer<typeof journeyShareLinkRequestSchema>;
|
||||
|
||||
Reference in New Issue
Block a user