How to use the json-schema-faker.generate function in json-schema-faker

To help you get started, weā€™ve selected a few json-schema-faker examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github unmock / unmock-js / packages / unmock-core / src / generator.ts View on Github external
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,
  };
};
github apiaryio / api-elements.js / packages / fury-adapter-swagger / lib / generator.js View on Github external
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;
}
github apiaryio / api-elements.js / packages / fury-adapter-swagger / lib / generator.js View on Github external
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;
}
github unmock / unmock-js / packages / unmock-core / src / generator.ts View on Github external
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,
  };
};
github zandaqo / structurae / bench / json-object-view.js View on Github external
},
    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)}%)`);
github unmock / unmock-js / packages / unmock-core / src / generator-experimental.ts View on Github external
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,
  };
};
github unmock / unmock-js / packages / unmock-core / src / generator-experimental.ts View on Github external
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,
  };
};