validjsonator

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2024 License: MIT Imports: 2 Imported by: 1

README

ValidJSONator

ValidJSONator is a basic Go library that takes a Go Validator V10 validation string and converts it into a Schema that can be used in JSONSchema or OpenAPI.

This library is used to share this logic between our other libraries Astra and Confuse, and hopefully will be useful to others.

Installation

go get github.com/ls6-events/validjsonator

Usage

package main

import (
	"reflect"

	"github.com/davecgh/go-spew/spew"
	"github.com/ls6-events/validjsonator"
)

type Example struct {
	ExampleString string  `validate:"required,base64"`
	ExampleInt    int     `validate:"required,min=1,max=10"`
	ExampleFloat  float64 `validate:"required,min=1,max=10"`
}

func main() {
	Example := Example{}
	exampleType := reflect.TypeOf(Example)

	structFieldsSchema := make(map[string]validjsonator.Schema)
	for i := 0; i < exampleType.NumField(); i++ {
		fieldTag := exampleType.Field(i).Tag
		fieldName := exampleType.Field(i).Name

		validationTag := fieldTag.Get("validate")

		validationSchema, _ := validjsonator.ValidationTagsToSchema(validationTag)

		structFieldsSchema[fieldName] = validationSchema
	}

	spew.Dump(structFieldsSchema)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Schema

type Schema struct {
	Ref                  string            `json:"$ref,omitempty" yaml:"$ref,omitempty"`
	Title                string            `json:"title,omitempty" yaml:"title,omitempty"`
	MultipleOf           float64           `json:"multipleOf,omitempty" yaml:"multipleOf,omitempty"`
	Maximum              float64           `json:"maximum,omitempty" yaml:"maximum,omitempty"`
	ExclusiveMaximum     bool              `json:"exclusiveMaximum,omitempty" yaml:"exclusiveMaximum,omitempty"`
	Minimum              float64           `json:"minimum,omitempty" yaml:"minimum,omitempty"`
	ExclusiveMinimum     bool              `json:"exclusiveMinimum,omitempty" yaml:"exclusiveMinimum,omitempty"`
	MaxLength            int               `json:"maxLength,omitempty" yaml:"maxLength,omitempty"`
	MinLength            int               `json:"minLength,omitempty" yaml:"minLength,omitempty"`
	Pattern              string            `json:"pattern,omitempty" yaml:"pattern,omitempty"`
	MaxItems             int               `json:"maxItems,omitempty" yaml:"maxItems,omitempty"`
	MinItems             int               `json:"minItems,omitempty" yaml:"minItems,omitempty"`
	UniqueItems          bool              `json:"uniqueItems,omitempty" yaml:"uniqueItems,omitempty"`
	MaxProperties        int               `json:"maxProperties,omitempty" yaml:"maxProperties,omitempty"`
	MinProperties        int               `json:"minProperties,omitempty" yaml:"minProperties,omitempty"`
	Required             []string          `json:"required,omitempty" yaml:"required,omitempty"`
	Enum                 []interface{}     `json:"enum,omitempty" yaml:"enum,omitempty"`
	Type                 string            `json:"type,omitempty" yaml:"type,omitempty"`
	Format               string            `json:"format,omitempty" yaml:"format,omitempty"`
	AllOf                []Schema          `json:"allOf,omitempty" yaml:"allOf,omitempty"`
	OneOf                []Schema          `json:"oneOf,omitempty" yaml:"oneOf,omitempty"`
	AnyOf                []Schema          `json:"anyOf,omitempty" yaml:"anyOf,omitempty"`
	Not                  *Schema           `json:"not,omitempty" yaml:"not,omitempty"`
	Items                *Schema           `json:"items,omitempty" yaml:"items,omitempty"`
	Properties           map[string]Schema `json:"properties,omitempty" yaml:"properties,omitempty"`
	PatternProperties    map[string]Schema `json:"patternProperties,omitempty" yaml:"patternProperties,omitempty"`
	AdditionalProperties *Schema           `json:"additionalProperties,omitempty" yaml:"additionalProperties,omitempty"`
	Description          string            `json:"description,omitempty" yaml:"description,omitempty"`
}

func ValidationTagsToSchema

func ValidationTagsToSchema(validationTag string) (tagSchema Schema, required bool)

Jump to

Keyboard shortcuts

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