UNPKG

typescript-scaffolder

Version:

![npm version](https://img.shields.io/npm/v/typescript-scaffolder) ![coverage](https://img.shields.io/badge/coverage-97.38%25-green)

42 lines (41 loc) 1.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateMockData = generateMockData; const logger_1 = require("../logger"); const mock_value_core_1 = require("./mock-value-core"); const mock_value_resolver_1 = require("./mock-value-resolver"); /** * Generates a mock data sample from a type-hinted json schema * @param count * @param schemaJson * @param arrayLength */ function generateMockData(count, schemaJson, arrayLength = 1) { const funcName = 'generateMockData'; logger_1.Logger.debug(funcName, 'Starting mock data generation...'); const results = []; const schema = JSON.parse(schemaJson); for (let i = 0; i < count; i++) { const autoFaked = {}; logger_1.Logger.debug(funcName, `Identified the following keys: ${Object.keys(schema).join(', ')}`); for (const key of Object.keys(schema)) { const value = schema[key]; // Handle array type hints like "string[]" if (typeof value === 'string' && value.endsWith('[]')) { const baseType = value.replace('[]', ''); logger_1.Logger.debug(funcName, `Array type identified at key: ${String(key)} with base type ${baseType}`); autoFaked[key] = Array.from({ length: arrayLength }).map(() => (0, mock_value_core_1.generatePrimitiveMock)(baseType)); continue; } if (typeof value === 'string') { logger_1.Logger.debug(funcName, `Primitive type string detected at key: ${String(key)} with type hint: ${value}`); autoFaked[key] = (0, mock_value_resolver_1.getRuntimeFakerValueForKey)(String(key)) ?? (0, mock_value_core_1.generatePrimitiveMock)(value); } else { autoFaked[key] = (0, mock_value_core_1.handleDefaultCase)(value, String(key), arrayLength); } } results.push(autoFaked); } return results; }