stubs

package module
v0.0.0-...-3faadc8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2025 License: Apache-2.0 Imports: 22 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

OpenAPI initiative random data generator Build Status codecov Slack Status

license GoDoc

A library to generate random data for a swagger specification. This is a building block for generating stubs for your API as well as tests.

Project archived on 2025/11/09

Generating mock data driven by an API spec remains a topic of interest.

Documentation

Index

Constants

View Source
const (
	// XdataGen is the extension tag to be used in specs and hint the stub generator
	XdataGen = "x-datagen"
	// StubsDefaultWordCount is the default maximum word count in sentences. It may be overriden with the "words" argument.
	StubsDefaultWordCount = 10
	// StubsDefaultStringLength is the default length for generated strings
	StubsDefaultStringLength = 30
	// StubsDefaultSupplemental defines if "supplemental" words from extra dictionary in faker is enabled
	StubsDefaultSupplemental = true
	// StubsDefaultMaxAmount defines the default max amount for prices and other currency amounts
	StubsDefaultMaxAmount = 1000000
	// StubsDefaultMinAmount defines the default min amount for prices and other currency amounts
	StubsDefaultMinAmount = 100
	// StubsDefaultMaxSmallAmount defines the default max amount for small prices and other small currency amounts
	StubsDefaultMaxSmallAmount = 1000
	// StubsDefaultMinSmallAmount defines the default min amount for small prices and other small currency amounts
	StubsDefaultMinSmallAmount = 10
)

Variables

View Source
var (
	// ErrNoValid indicates to the caller that value generator could not abide by validation constraints and generate a valid value
	ErrNoValid error
	// ErrNoInvalid indicates to the caller that value generator could not abide by specified invalid mode flags
	ErrNoInvalid error
)
View Source
var (
	// Debug is true when the SWAGGER_DEBUG env var is not empty.
	// It enables a more verbose logging of validators.
	Debug = os.Getenv("STUBS_DEBUG") != ""
)

Functions

func RegisterAltGenNames

func RegisterAltGenNames(key string, alts ...string)

RegisterAltGenNames registers alternatives for a generator name

Types

type Converter

type Converter interface {
	Bool(interface{}) (bool, error)
	Int(interface{}) (int, error)
}

Converter provides functions to convert values from other types

type Generator

type Generator struct {
	Language string
}

Generator generates a stub for a descriptor. A descriptor can either be a parameter, response header or json schema

func (*Generator) GenHeader

func (s *Generator) GenHeader(key string, header *spec.Header) (interface{}, error)

GenHeader generates a random value for a header

func (*Generator) GenParameter

func (s *Generator) GenParameter(key string, param *spec.Parameter) (interface{}, error)

GenParameter generates a random value for a parameter

func (*Generator) GenResponse

func (s *Generator) GenResponse(key string, response *spec.Response) (interface{}, error)

GenResponse generates a random value for a response

func (*Generator) GenSchema

func (s *Generator) GenSchema(key string, schema *spec.Schema) (interface{}, error)

GenSchema generates a random value for a schema

func (*Generator) Generate

func (s *Generator) Generate(key string, descriptor interface{}) (interface{}, error)

Generate a stub from swagger spec constructs into the opts.Target

type GeneratorOpts

type GeneratorOpts interface {

	// FieldName for the value generator, this is mostly used as an alternative to the name
	// for inferring which value generator to use
	// TODO(fredbi): atm unused
	FieldName() string

	// Type for the value generator to return, aids in infering the name of the value generator
	Type() string

	// Format for the value generator to return, aids in infering the name of the value generator
	Format() string

	// Maximum a numeric value can have, returns value, exclusive, defined
	Maximum() (float64, bool, bool)

	// Minimum a numeric value can have, returns value, exclusive, defined
	Minimum() (float64, bool, bool)

	// MaxLength a string can have, returns value, defined
	MaxLength() (int64, bool)

	// MinLength a string can have, returns value, defined
	MinLength() (int64, bool)

	// Pattern a string should match, returns value, defined
	Pattern() (string, bool)

	// MaxItems a collection of values can contain, returns length, defined
	MaxItems() (int64, bool)

	// MinItems a collection of values must contain, returns length, defined
	MinItems() (int64, bool)

	// UniqueItems when true the collection can't contain duplicates
	UniqueItems() bool

	// MultipleOf a numeric value should be divisible by this value, returns value, defined
	MultipleOf() (float64, bool)

	// Enum a list of acceptable values for a value, returns value, defined
	Enum() ([]interface{}, bool)

	// Items options for the members of a collection
	Items() (GeneratorOpts, error)

	// Required when true the property can't be nil
	Required() bool
	// contains filtered or unexported methods
}

GeneratorOpts interface to capture various types that can get data generated for them.

type StubMode

type StubMode uint64

StubMode for generating data

const (
	// Invalid produces a stub which is invalid for a random validation
	Invalid StubMode = 1 << iota
	// InvalidRequired produces a stub which is invalid for required
	InvalidRequired
	// InvalidMaximum produces a stub which is invalid for maximum
	InvalidMaximum
	// InvalidMinimum produces a stub which is invalid for minimum
	InvalidMinimum
	// InvalidMaxLength produces a stub which is invalid for max length
	InvalidMaxLength
	// InvalidMinLength produces a stub which is invalid for min length
	InvalidMinLength
	// InvalidPattern produces a stub which is invalid for pattern
	InvalidPattern
	// InvalidMaxItems produces a stub which is invalid for max items
	InvalidMaxItems
	// InvalidMinItems produces a stub which is invalid for min items
	InvalidMinItems
	// InvalidUniqueItems produces a stub which is invalid for unique items
	InvalidUniqueItems
	// InvalidMultipleOf produces a stub which is invalid for multiple of
	InvalidMultipleOf
	// InvalidEnum produces a stub which is invalid for enum
	InvalidEnum

	// Valid is the default value and generates valid data
	Valid StubMode = 0
)

func (StubMode) Has

func (s StubMode) Has(m StubMode) bool

Has returns true when this mode has the provided flag configured

type ValueGenerator

type ValueGenerator func(GeneratorOpts) (interface{}, error)

ValueGenerator represents a function to generate a piece of random data

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL