Documentation
¶
Index ¶
Examples ¶
Constants ¶
const NullValue = "null"
NullValue when setting an override to the 'null' value. This can be used as opposed to SkipValue where the key is omitted entirely
Variables ¶
var ErrInvalidInput = errors.New("invalid input given")
ErrInvalidInput is returned if given parameters are invalid
var ErrParsing = errors.New("failed to parse/process jsonschema")
ErrParsing is returned if during evaluation of the schema an error occurs
var SkipValue skipValue = true
SkipValue can be set on any key to signal that it should be emitted from the result set
Functions ¶
func SchemaToNode ¶ added in v0.0.1
SchemaToNode is a lower-level version of SchemaToYAML, but returns the yaml.Node instead of the marshalled YAML.
You may provide options to customise the output.
func SchemaToYAML ¶
func SchemaToYAML(schema *jsonschema.Schema, opts ...Option) ([]byte, error)
SchemaToYAML will take the given JSON schema and turn it into an example YAML file using fields like `description` and `examples` for documentation, `default` for default values and `properties` for listing blocks.
If any of the given properties match a regex given in pattern properties, the fields are added to the relevant fields.
You may provide options to customise the output.
Example ¶
input := `{
"type": "object",
"properties": {
"name": {
"type": "string",
"default": "Robin",
"description": "The name of the customer"
},
"beverages": {
"type": "array",
"description": "A list of beverages the customer has consumed",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "The name of the beverage",
"examples": ["Coffee", "Tea", "Cappucino"]
},
"price": {
"type": "number",
"description": "The price of the product",
"default": 4.5
}
}
}
}
}
}`
compiler := jsonschema.NewCompiler()
schema, _ := compiler.Compile([]byte(input))
result, _ := SchemaToYAML(schema)
fmt.Println(string(result))
Output: # A list of beverages the customer has consumed beverages: - # The name of the beverage # # Examples: # - Coffee # - Tea # - Cappucino name: null # TODO: Fill this in # The price of the product price: 4.5 # The name of the customer name: Robin
Example (WithOverrideValues) ¶
input := `{
"type": "object",
"properties": {
"name": {
"type": "string",
"default": "Robin",
"description": "The name of the customer"
},
"previous_orders": {
"type": "array",
"items": {
"type": "string"
},
"description": "names of beverages the customer has consumed"
},
"beverages": {
"type": "array",
"description": "A list of beverages the customer has consumed",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "The name of the beverage",
"examples": ["Coffee", "Tea", "Cappucino"]
},
"price": {
"type": "number",
"description": "The price of the product",
"default": 4.5
},
"description": {
"type": "string"
}
}
}
}
}
}`
overrides := map[string]any{
"name": "John",
"previous_orders": []string{"Water", "Tea"},
"beverages": []any{
map[string]any{"name": "Coffee"},
map[string]any{"name": "Beer"},
},
}
todoComment := "Do something with this"
compiler := jsonschema.NewCompiler()
schema, _ := compiler.Compile([]byte(input))
result, err := SchemaToYAML(schema, WithIndent(2), WithOverrideValues(overrides), WithTODOComment(todoComment))
if err != nil {
fmt.Println(err)
}
fmt.Println(string(result))
Output: # A list of beverages the customer has consumed beverages: - description: null # Do something with this # The name of the beverage # # Examples: # - Coffee # - Tea # - Cappucino name: Coffee # The price of the product price: 4.5 - description: null # Do something with this # The name of the beverage # # Examples: # - Coffee # - Tea # - Cappucino name: Beer # The price of the product price: 4.5 # The name of the customer name: John # names of beverages the customer has consumed previous_orders: - Water - Tea
Types ¶
type Config ¶ added in v0.0.2
type Config struct {
// OutputHeader is added to the top of the generated result as a comment, This property is only available at the root level and not copied in
// forProperty
OutputHeader string
// ValueOverride is a primitive value used outside of objects (mainly to support ItemsOverrides). For
// example when the schema is an array of which the items are primitives (e.g. "string"), the overrides
// are processed per item (hence the relation to ItemsOverrides) but are not of type map[string]any
// that is commonly used in ValueOverrides.
ValueOverride any
// HasOverride is configured in conjunction with ValueOverride to distinguish between the explicit
// and implicit nil
HasOverride bool
// ValueOverrides allows a user to override the default values of a schema with the given value(s).
// Because a schema may nested, this takes the form of a map[string]any of which the structure must mimic
// the schema to function.
ValueOverrides map[string]any
// ItemsOverrides allows a user to override the default values of a schema with the given value(s).
// Because a schema may be a slice (of potentially nested maps) this is stored separately from ValueOverrides
ItemsOverrides []any
// PatternProperties inherited from parent
PatternProperties []*jsonschema.Schema
// TODOComment is used in case no default value was defined for a property. It is set by
// default in NewConfig but can be emptied to remove the comment altogether.
TODOComment string
// OnlyRequired properties are returned
OnlyRequired bool
// LineLength prevents descriptions and unreasonably long lines. Can be disabled
// completely by setting it to 0.
LineLength uint
// Indent used when marshalling to YAML. This property is only available at the root level and not copied in
// forProperty
Indent int
// SkipValidate of the provided jsonschema and override values. Might result in undefined behavior, use
// at own risk. This property is only available at the root level and not copied in forProperty
SkipValidate bool
}
Config serves as the configuration object to allow customisation in the library
type InvalidSchemaError ¶ added in v1.1.0
type InvalidSchemaError struct {
Errors map[string]*jsonschema.EvaluationError
}
InvalidSchemaError is returned when the schema is not valid, see jsonschema.Validate
func (InvalidSchemaError) Error ¶ added in v1.1.0
func (e InvalidSchemaError) Error() string
Error is a multiline string of the string->jsonschema.EvaluationError
type Option ¶
type Option func(*Config)
Option is used to customise the output, we currently don't allow extensions yet
func SkipValidate ¶ added in v1.1.0
func SkipValidate() Option
SkipValidate will not evaluate jsonschema.Validate, might result in undefined behavior. Use at own risk
func WithCommentMaxLength ¶ added in v0.0.5
WithCommentMaxLength prevents descriptions generating unreasonably long lines. Can be disabled completely by setting it to 0.
func WithIndent ¶ added in v1.1.0
WithIndent amount of spaces to use when marshalling
func WithOverrideValues ¶ added in v0.0.2
WithOverrideValues allows you to override the default values from the JSON schema, you can nest map[string]any values to reach nested objects in the JSON schema.
func WithSchemaHeader ¶ added in v1.2.0
WithSchemaHeader will add the `# yaml-language-server: $schema=[...]` header to the output, allowing IDEs to provide autocompletion.
func WithTODOComment ¶ added in v0.0.2
WithTODOComment allows you to set the 'TODO: Fill this in' comment in the output YAML. Can be set to an empty string to turn it off altogether