Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return (root, args, context, info) => {
const random = seedrandom(RANDOM_SEED);
jsf.format('uuid', generateUUID(random));
jsf.format('uniqueId', generateUUID(random));
jsf.option({ random });
const fake = jsf(operation.schema);
// console.log(`CALLING API: ${operation.path}\n\n${JSON.stringify(fake, null, 2)}`);
return fake;
}
};
statusCode: number;
headerSchema?: any;
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,
};
};
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,
};
};
headerSchema?: any;
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,
};
};
import faker from 'faker';
import jsf from 'json-schema-faker';
import { Definition } from 'typescript-json-schema';
// faker.locale = 'zh_CN';
jsf.option('useExamplesValue', true);
jsf.option('useDefaultValue', true);
jsf.extend('faker', () => faker);
export const getMock = (schema: any) => jsf.resolve(schema);
export const fakerGenerate = (schema: Definition) => jsf.generate(schema, []);
const generateMockFromTemplate = ({
rng,
statusCode,
headerSchema,
bodySchema,
}: {
rng: () => number;
statusCode: number;
headerSchema?: any;
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);
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,
};
};
import registerPromiseWorker from "promise-worker/register";
import transform from "transform-json-types";
import jsf from "json-schema-faker";
jsf.option({
alwaysFakeOptionals: true
});
registerPromiseWorker(
({ code, rustCase }) =>
new Promise(resolve => {
jsf.resolve(JSON.parse(code)).then(_json => {
resolve(
transform(_json, {
lang: "rust-serde",
rustCase
})
);
});
})
);
exports.schemaToJson = function(schema, options = {}) {
Object.assign(options, defaultOptions);
jsf.option(options);
let result;
try {
result = jsf(schema);
} catch (err) {
result = err.message;
}
jsf.option(defaultOptions);
return result;
};
evaluate(context) {
this.context = context
jsf.option({
failOnInvalidTypes: false,
defaultInvalidTypeProduct: null
})
let resolveRefs = this.resolveRefs
let _schema = this.schema
let mainKey = '@undefined'
let schemaDict = this._getSchemaDict(
context, _schema, resolveRefs, mainKey
)
let schema = {
$$schema: schemaDict[mainKey]
}
delete schemaDict[mainKey]