Add schema utils

This commit is contained in:
Jamie Kyle 2024-10-02 12:03:10 -07:00 committed by GitHub
parent c8a729f8be
commit b26466e59d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
45 changed files with 674 additions and 151 deletions

View file

@ -21,6 +21,7 @@ import { prepare } from '../Server';
import { sql } from '../util';
import { strictAssert } from '../../util/assert';
import { CallStatusValue } from '../../types/CallDisposition';
import { parseStrict, parseUnknown } from '../../util/schemas';
export function callLinkExists(db: ReadableDB, roomId: string): boolean {
const [query, params] = sql`
@ -58,7 +59,7 @@ export function getCallLinkRecordByRoomId(
return undefined;
}
return callLinkRecordSchema.parse(row);
return parseUnknown(callLinkRecordSchema, row as unknown);
}
export function getAllCallLinks(db: ReadableDB): ReadonlyArray<CallLinkType> {
@ -68,7 +69,9 @@ export function getAllCallLinks(db: ReadableDB): ReadonlyArray<CallLinkType> {
return db
.prepare(query)
.all()
.map(item => callLinkFromRecord(callLinkRecordSchema.parse(item)));
.map((item: unknown) =>
callLinkFromRecord(parseUnknown(callLinkRecordSchema, item))
);
}
function _insertCallLink(db: WritableDB, callLink: CallLinkType): void {
@ -142,7 +145,10 @@ export function updateCallLinkState(
callLinkState: CallLinkStateType
): CallLinkType {
const { name, restrictions, expiration, revoked } = callLinkState;
const restrictionsValue = callLinkRestrictionsSchema.parse(restrictions);
const restrictionsValue = parseStrict(
callLinkRestrictionsSchema,
restrictions
);
const [query, params] = sql`
UPDATE callLinks
SET
@ -153,9 +159,9 @@ export function updateCallLinkState(
WHERE roomId = ${roomId}
RETURNING *;
`;
const row = db.prepare(query).get(params);
const row: unknown = db.prepare(query).get(params);
strictAssert(row, 'Expected row to be returned');
return callLinkFromRecord(callLinkRecordSchema.parse(row));
return callLinkFromRecord(parseUnknown(callLinkRecordSchema, row));
}
export function updateCallLinkAdminKeyByRoomId(
@ -302,7 +308,7 @@ export function getAllCallLinkRecordsWithAdminKey(
return db
.prepare(query)
.all()
.map(item => callLinkRecordSchema.parse(item));
.map((item: unknown) => parseUnknown(callLinkRecordSchema, item));
}
export function getAllMarkedDeletedCallLinkRoomIds(

View file

@ -8,7 +8,6 @@ import type {
} from '../../types/GroupSendEndorsements';
import {
groupSendEndorsementExpirationSchema,
groupSendCombinedEndorsementSchema,
groupSendMemberEndorsementSchema,
groupSendEndorsementsDataSchema,
} from '../../types/GroupSendEndorsements';
@ -17,6 +16,7 @@ import type { ReadableDB, WritableDB } from '../Interface';
import { sql } from '../util';
import type { AciString } from '../../types/ServiceId';
import { strictAssert } from '../../util/assert';
import { parseLoose, parseUnknown } from '../../util/schemas';
/**
* We don't need to store more than one endorsement per group or per member.
@ -110,7 +110,7 @@ export function getGroupSendCombinedEndorsementExpiration(
if (value == null) {
return null;
}
return groupSendEndorsementExpirationSchema.parse(value);
return parseUnknown(groupSendEndorsementExpirationSchema, value as unknown);
}
export function getGroupSendEndorsementsData(
@ -128,24 +128,21 @@ export function getGroupSendEndorsementsData(
WHERE groupId IS ${groupId}
`;
const combinedEndorsement = groupSendCombinedEndorsementSchema
.optional()
.parse(
prepare<Array<unknown>>(db, selectCombinedEndorsement).get(
selectCombinedEndorsementParams
)
);
const combinedEndorsement: unknown = prepare<Array<unknown>>(
db,
selectCombinedEndorsement
).get(selectCombinedEndorsementParams);
if (combinedEndorsement == null) {
return null;
}
const memberEndorsements = prepare<Array<unknown>>(
const memberEndorsements: Array<unknown> = prepare<Array<unknown>>(
db,
selectMemberEndorsements
).all(selectMemberEndorsementsParams);
return groupSendEndorsementsDataSchema.parse({
return parseLoose(groupSendEndorsementsDataSchema, {
combinedEndorsement,
memberEndorsements,
});
@ -168,5 +165,5 @@ export function getGroupSendMemberEndorsement(
if (row == null) {
return null;
}
return groupSendMemberEndorsementSchema.parse(row);
return parseUnknown(groupSendMemberEndorsementSchema, row as unknown);
}