/* Copyright 2026 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import { logger } from "../logger.ts"; /** * Type guard to check the given value is a Record type with string keys. * @param value - the value to check */ export const isRecord = (value: unknown): value is Record => !!value && typeof value === "object" && !Array.isArray(value); /** * Type guard to check the given record has a key and its value is a string * @param metadata - the record to check * @param key - the key to check */ export const hasRequiredStringProperty = ( metadata: Record, key: K, ): metadata is Record & Record => { if (!metadata[key] || !hasOptionalStringProperty(metadata, key)) { logger.error(`Missing or invalid property: ${key}`); return false; } return true; }; /** * Type guard to check that if the given record has a key then its value is a string * @param metadata - the record to check * @param key - the key to check */ export const hasOptionalStringProperty = ( metadata: Record, key: K, ): metadata is Record & Record => { if (!!metadata[key] && typeof metadata[key] !== "string") { logger.error(`Invalid property: ${key}`); return false; } return true; }; /** * Type guard to check the given record has a key and its value is a number * @param metadata - the record to check * @param key - the key to check */ export const hasRequiredNumberProperty = ( metadata: Record, key: K, ): metadata is Record & Record => { if (!metadata[key] || !hasOptionalNumberProperty(metadata, key)) { logger.error(`Missing or invalid property: ${key}`); return false; } return true; }; /** * Type guard to check that if the given record has a key then its value is a number * @param metadata - the record to check * @param key - the key to check */ export const hasOptionalNumberProperty = ( metadata: Record, key: K, ): metadata is Record & Record => { if (!!metadata[key] && typeof metadata[key] !== "number") { logger.error(`Invalid property: ${key}`); return false; } return true; }; /** * Type guard to check that if the given record has a key then its value is an array containing only strings * @param metadata - the record to check * @param key - the key to check */ export const optionalStringArrayProperty = ( metadata: Record, key: K, ): metadata is Record & Record => { if ( !!metadata[key] && (!Array.isArray(metadata[key]) || !(metadata[key]).every((v) => typeof v === "string")) ) { logger.error(`Invalid property: ${key}`); return false; } return true; }; /** * Type guard to check the given record has a key and its value is an array of strings containing at least the given value * @param metadata - the record to check * @param key - the key to check * @param value - the value to require to be present in the array */ export const requiredArrayValue = ( metadata: Record, key: K, value: V, ): metadata is Record & Record => { const array = metadata[key]; if (!array || !Array.isArray(array) || !array.includes(value)) { logger.error(`Invalid property: ${key}. ${value} is required.`); return false; } return true; };