UNPKG

@tinybirdco/mockingbird

Version:
32 lines (31 loc) 1.23 kB
import { extendedFaker } from "./extendedFaker"; export { PRESET_SCHEMA_NAMES } from "./schemas"; // Helper function to safely get nested object properties const getNestedValue = (obj, path) => { return path.split(".").reduce((current, key) => current?.[key], obj); }; export function validateSchema(schema) { const errors = []; for (const schemaItem of Object.values(schema)) { const { type, count } = schemaItem; if (typeof count !== "undefined" && count < 1) errors.push(`${type}: Count must be greater than 0`); if ("params" in schemaItem) { const generator = getNestedValue(extendedFaker, type); // console.log(`Validating ${type}:`, { // params: schemaItem.params, // generator: generator?.toString(), // }); try { generator(schemaItem.params); } catch (e) { console.error(`Error validating ${type}:`, e); errors.push(`${type}: ${e && typeof e === "object" && "toString" in e ? e.toString() : "Unknown error"}`); } } } return { valid: !errors.length, errors }; }