Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
bodySchema?: any;
}): ISerializedResponse => {
jsf.extend("faker", () => require("faker"));
jsf.option("optionalsProbability", runnerConfiguration.optionalsProbability);
// When optionalsProbability is set to 100%, generate exactly 100% of all optionals.
// otherwise, generate up to optionalsProbability% of optionals
jsf.option(
"fixedProbabilities",
runnerConfiguration.optionalsProbability === 1,
);
// disables this temporarily as it messes with user-defined min items
// jsf.option("minItems", runnerConfiguration.minItems);
// jsf.option("minLength", runnerConfiguration.minItems);
jsf.option("useDefaultValue", false);
jsf.option("random", rng);
const bodyAsJson = bodySchema ? jsf.generate(bodySchema) : undefined;
const body = bodyAsJson ? JSON.stringify(bodyAsJson) : undefined;
jsf.option("useDefaultValue", true);
const resHeaders = headerSchema ? jsf.generate(headerSchema) : undefined;
jsf.option("useDefaultValue", false);
return {
body,
bodyAsJson,
headers: resHeaders,
statusCode,
};
};
function generateBody(schema) {
if (schema.allOf && schema.allOf.length === 1 && schema.allOf[0].examples && schema.allOf[0].examples.length > 0) {
return schema.allOf[0].examples[0];
}
if (schema.examples && schema.examples.length > 0) {
return schema.examples[0];
}
const body = faker.generate(schema);
if (isEmptyArray(body) && schemaIsArrayAndHasItems(schema)) {
// Faker failed to generate array schema, pass it `items` and wrap in array ourselves
return [faker.generate(schema.items)];
}
return body;
}
function generateBody(schema) {
if (schema.allOf && schema.allOf.length === 1 && schema.allOf[0].examples && schema.allOf[0].examples.length > 0) {
return schema.allOf[0].examples[0];
}
if (schema.examples && schema.examples.length > 0) {
return schema.examples[0];
}
const body = faker.generate(schema);
if (isEmptyArray(body) && schemaIsArrayAndHasItems(schema)) {
// Faker failed to generate array schema, pass it `items` and wrap in array ourselves
return [faker.generate(schema.items)];
}
return body;
}
jsf.option("optionalsProbability", runnerConfiguration.optionalsProbability);
// When optionalsProbability is set to 100%, generate exactly 100% of all optionals.
// otherwise, generate up to optionalsProbability% of optionals
jsf.option(
"fixedProbabilities",
runnerConfiguration.optionalsProbability === 1,
);
// disables this temporarily as it messes with user-defined min items
// jsf.option("minItems", runnerConfiguration.minItems);
// jsf.option("minLength", runnerConfiguration.minItems);
jsf.option("useDefaultValue", false);
jsf.option("random", rng);
const bodyAsJson = bodySchema ? jsf.generate(bodySchema) : undefined;
const body = bodyAsJson ? JSON.stringify(bodyAsJson) : undefined;
jsf.option("useDefaultValue", true);
const resHeaders = headerSchema ? jsf.generate(headerSchema) : undefined;
jsf.option("useDefaultValue", false);
return {
body,
bodyAsJson,
headers: resHeaders,
statusCode,
};
};
},
parents: {
type: 'array',
items: [
{ type: 'string', minLength: 5, maxLength: 10 },
{ type: 'string', minLength: 5, maxLength: 10 },
],
},
},
required: ['type', 'id', 'name', 'weight', 'height', 'scores', 'pets', 'house', 'parents'],
};
const objects = [];
for (let i = 0; i < 100; i++) {
objects.push(jsf.generate(JSONSchema));
}
const people = People.from(objects);
const views = [...people];
const strings = objects.map((i) => JSON.stringify(i));
const binarySize = people.byteLength;
const binaryCompressedSize = zlib.deflateSync(people, { level: 1 }).byteLength;
const stringSize = StringView.getByteSize(JSON.stringify(objects));
const stringCompressedSize = zlib.deflateSync(JSON.stringify(objects), { level: 1 }).byteLength;
console.log(`Encoded Sizes:
ObjectView: ${binarySize}
JSON String: ${stringSize} (${Math.round((stringSize / binarySize) * 100)}%)
ObjectView Compressed: ${binaryCompressedSize}
JSON Compressed: ${stringCompressedSize} (${Math.round((stringCompressedSize / binaryCompressedSize) * 100)}%)`);
const generateMockFromTemplate2 = (
statusCode: number,
headerSchema?: any,
bodySchema?: any,
): ISerializedResponse => {
jsf.option("alwaysFakeOptionals", false);
jsf.option("useDefaultValue", false);
const body = bodySchema ? JSON.stringify(jsf.generate(bodySchema)) : undefined;
jsf.option("useDefaultValue", true);
const resHeaders = headerSchema ? jsf.generate(headerSchema) : undefined;
jsf.option("useDefaultValue", false);
return {
body,
headers: resHeaders,
statusCode,
};
};
const generateMockFromTemplate2 = (
statusCode: number,
headerSchema?: any,
bodySchema?: any,
): ISerializedResponse => {
jsf.option("alwaysFakeOptionals", false);
jsf.option("useDefaultValue", false);
const body = bodySchema ? JSON.stringify(jsf.generate(bodySchema)) : undefined;
jsf.option("useDefaultValue", true);
const resHeaders = headerSchema ? jsf.generate(headerSchema) : undefined;
jsf.option("useDefaultValue", false);
return {
body,
headers: resHeaders,
statusCode,
};
};