jsonschemaline

package module
v0.0.38 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2023 License: MIT Imports: 27 Imported by: 5

README

jsonschemaline

计划

将 validate_schema、output_schema 改成 key:value 格式(类似go tag),提升可阅读性和维护效率 出参案例:

{"$schema":"http://json-schema.org/draft-07/schema#","$id":"execAPIXyxzBlacklistInfoPaginate","properties":{"items":{"type":"array","items":{"type":"object","properties":{"id":{"type":"string","src":"PaginateOut.#.Fid"},"openId":{"type":"string","src":"PaginateOut.#.Fopen_id"},"type":{"type":"string","src":"PaginateOut.#.Fopen_id_type"},"status":{"type":"string","src":"PaginateOut.#.Fstatus"}},"required":["id","openId","type","status"]}},"pageInfo":{"type":"object","properties":{"pageIndex":{"type":"string","src":"input.pageIndex"},"pageSize":{"type":"string","src":"input.pageSize"},"total":{"type":"string","src":"PaginateTotalOut"}},"required":["pageIndex","pageSize","total"]}},"type":"object","required":["items","pageInfo"]}

转换为:

fullname:items[].id,src:PaginateOut.#.Fid,type:string,required:true
fullname:items[].openId,src:PaginateOut.#.Fopen_id,type:string,required:true
fullname:items[].type,src:PaginateOut.#.Fopen_id_type,type:string,required:true
fullname:items[].status,src:PaginateOut.#.Fstatus,type:string,required:true
fullname:pageInfo.pageIndex,src:input.pageIndex,type:string,required:true
fullname:pageInfo.pageSize,src:input.pageSize,type:string,required:true
fullname:pageInfo.total,src:PaginateTotalOut,type:string,required:true

入参案例:

{"$schema":"http://json-schema.org/draft-07/schema#","$id":"execAPIXyxzBlacklistInfoInsert","properties":{"config":{"type":"object","properties":{"openId":{"type":"string","format":"DBValidate"},"type":{"type":"string","format":"number","enum":["1","2"]},"status":{"type":"string","format":"number","enum":["0","1"]}},"required":["openId","type","status"]}},"type":"object"}

转换为:

fullname:config.openId,dst:FopenID,format:DBValidate,type:string,required:true
fullname:config.type,dst:FopenIDType,enum:["1","2"],type:string,required:true
fullname:config.status,dst:Fstatus,enum:["0","1"],type:string,required:true

分页入参案例:

{"$schema":"http://json-schema.org/draft-07/schema#","$id":"execAPIInquiryScreenIdentifyPaginate","properties":{"pageSize":{"type":"string","format":"number"},"pageIndex":{"type":"string","format":"number"}},"type":"object","required":["pageSize","pageIndex"]}

转换为:

fullname:pageSize,dst:limit,format:number,type:string,required:true
fullname:pageIndex,dst:Offset,format:number,type:string,required:true,tpl:{{setValue . "Offset" (mul  (getValue .  "input.pageIndex")   (getValue . "input.pageSize"))}}

dst、tpl 字段会提炼出,动态生成 template内容 通过这种转换后,更易于书写和阅读,和计划中的文档格式更相似,同时dst、tpl等字段定义优化了值转换的管理为自动校验提供可行的机制,后续api 的 exec 字段可能被弃用

有偿功能实现,价格500左右, 概念:把

fullname:xxx,type:xxx,required:true ...
fullname:xxx,type:xxx,required:true ...

的格式称为jsonschema 行列式 一行代表一个jsonschema 元素的描述,其格式为key:value,... 其中key包含fullname、src、dst tpl 4个特殊值,其余的为标准的jsonschema 的属性(如 type,required、enum 等关键词) 每对key:value 之间使用英文","分割.顺序无要求,除了fullname、type 属性必须之外,其它属性可有可无,特殊属性描述: fullname 必须存在,由[a-zA-z_]以及特殊字符串.和[]组成.其中"."代表json对象,"[]"代表数组 如"fullname:userList[].name" 代表 userList是数组格式,userList数组元素为对象,数组元素对象有个name属性 src、dst 两个属性无特殊意义,仅仅是给标准jsonschema增加的自定义属性 需要实现的功能: 标准jsonschema和jsonschema 行列式 互转

案例1: jsonschema:

{"$schema":"http://json-schema.org/draft-07/schema#","properties":{"config":{"type":"object","properties":{"openId":{"type":"string","format":"DBValidate"},"type":{"type":"string","format":"number","enum":["1","2"]},"status":{"type":"string","format":"number","enum":["0","1"]}},"required":["openId","type","status"]}},"type":"object"}

对应 jsonschema 行列式

fullname:config.openId,dst:FopenID,format:DBValidate,type:string,required:true
fullname:config.type,dst:FopenIDType,enum:["1","2"],type:string,required:true
fullname:config.status,dst:Fstatus,enum:["0","1"],type:string,required:true

案例2: jsonschema 行列式

fullname:config.openId,dst:FopenID,format:DBValidate,type:string,required:true
fullname:config.type,dst:FopenIDType,enum:["1","2"],type:string,required:true
fullname:config.status,dst:Fstatus,enum:["0","1"],type:string,required:true

jsonschema格式

{"$schema":"http://json-schema.org/draft-07/schema#","properties":{"config":{"type":"object","properties":{"openId":{"type":"string","format":"DBValidate"},"type":{"type":"string","format":"number","enum":["1","2"]},"status":{"type":"string","format":"number","enum":["0","1"]}},"required":["openId","type","status"]}},"type":"object"}

Documentation

Overview

Package jsonschema uses reflection to generate JSON Schemas from Go types [1].

If json tags are present on struct fields, they will be used to infer property names and if a property is required (omitempty is present).

[1] http://json-schema.org/latest/json-schema-validation.html

Index

Examples

Constants

View Source
const (
	LINE_SCHEMA_DIRECTION_OUT     = "out"     // 出参
	LINE_SCHEMA_DIRECTION_IN      = "in"      // 入参
	LINE_SCHEMA_DIRECTION_CONVERT = "convert" // 内部转换
)
View Source
const EOF = "\n"
View Source
const EOF_DOUBLE = "\n\n" //double
View Source
const (
	//jsonschema 本身支持的格式描述(后台表单经常用到)
	JsonSchemaLineSchema = `` /* 2278-byte string literal not displayed */

)

Variables

View Source
var (
	// TrueSchema defines a schema with a true value
	TrueSchema = &Schema{boolean: &[]bool{true}[0]}
	// FalseSchema defines a schema with a false value
	FalseSchema = &Schema{boolean: &[]bool{false}[0]}
)
View Source
var Version = "http://json-schema.org/draft/2020-12/schema"

Version is the JSON Schema version.

Functions

func BaseName added in v0.0.21

func BaseName(fullname string) (baseName string)

BaseName 获取最后.后的文本

func ExtractGoComments

func ExtractGoComments(base, path string, commentMap map[string]string) error

ExtractGoComments will read all the go files contained in the provided path, including sub-directories, in order to generate a dictionary of comments associated with Types and Fields. The results will be added to the `commentsMap` provided in the parameters and expected to be used for Schema "description" fields.

The `go/parser` library is used to extract all the comments and unfortunately doesn't have a built-in way to determine the fully qualified name of a package. The `base` paremeter, the URL used to import that package, is thus required to be able to match reflected types.

When parsing type comments, we use the `go/doc`'s Synopsis method to extract the first phrase only. Field comments, which tend to be much shorter, will include everything.

func FormatPathFnByFormatIn added in v0.0.34

func FormatPathFnByFormatIn(format string, src string, item *JsonschemalineItem) (path string)

使用format 属性格式化转换后的路径

func FormatPathFnByFormatOut added in v0.0.34

func FormatPathFnByFormatOut(format string, src string, item *JsonschemalineItem) (path string)

使用format 属性格式化转换后的路径

func GetJsonSchemaSchema added in v0.0.38

func GetJsonSchemaSchema() (schema string)

GetJsonSchemaScheme 返回jsonschema本身的schema

func GetMetaLine

func GetMetaLine(tagLineKVpairs []kvstruct.KVS) (tagLineKVpair *kvstruct.KVS, ok bool)

func IsMetaLine

func IsMetaLine(lineTags kvstruct.KVS) bool

func JsonMerge

func JsonMerge(first string, second string, more ...string) (merge string, err error)

func JsonSchema2LineSchema

func JsonSchema2LineSchema(jsonschemaStr string) (lineschemaStr string, err error)

func Namespace added in v0.0.21

func Namespace(fullname string) (namespace string)

Namespace 获取最后.前的文本

func OneRawLineSchema

func OneRawLineSchema(schema *Schema, required []string) (oneRaw string, err error)

func ParseJsonschemalineRaw

func ParseJsonschemalineRaw(jsonschemalineRaw string) (meta *Meta, item *JsonschemalineItem, err error)

ParseJsonschemalineRaw 解析 jsonschemaline 一行数据

func PretreatJsonschemalineRaw

func PretreatJsonschemalineRaw(tag string) (formatTag string)

PretreatJsonschemalineRaw 处理enum []格式

func ReplacePathSpecalChar added in v0.0.27

func ReplacePathSpecalChar(path string) (newPath string)

func SplitLineSchema

func SplitLineSchema(onelineSchema string) kvstruct.KVS

func SplitMultilineSchema

func SplitMultilineSchema(lineSchema string) []kvstruct.KVS

func SplitOnUnescapedCommas

func SplitOnUnescapedCommas(tagString string) []string

Split on commas that are not preceded by `\`. This way, we prevent splitting regexes

Types

type DefaultJson

type DefaultJson struct {
	ID      string
	Version string
	Json    string
}

func ParseDefaultJson

func ParseDefaultJson(jsonschemaline Jsonschemaline) (defaultJson *DefaultJson, err error)

type Definitions

type Definitions map[string]*Schema

Definitions hold schema definitions. http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.26 RFC draft-wright-json-schema-validation-00, section 5.26

type ID

type ID string

ID represents a Schema ID type which should always be a URI. See draft-bhutton-json-schema-00 section 8.2.1

const EmptyID ID = ""

EmptyID is used to explicitly define an ID with no value.

func (ID) Add

func (id ID) Add(path string) ID

Add appends the provided path to the id, and removes any anchor data that might be there.

func (ID) Anchor

func (id ID) Anchor(name string) ID

Anchor sets the anchor part of the schema URI.

func (ID) Base

func (id ID) Base() ID

Base removes any anchor information from the schema

func (ID) Def

func (id ID) Def(name string) ID

Def adds or replaces a definition identifier.

func (ID) String

func (id ID) String() string

String provides string version of ID

func (ID) Validate

func (id ID) Validate() error

Validate is used to check if the ID looks like a proper schema. This is done by parsing the ID as a URL and checking it has all the relevant parts.

type Instruct

type Instruct struct {
	ID            string // 唯一标识
	Cmd           string
	Src           string
	Dst           string
	Tpl           string
	ExtraEndTpl   []string // 一个元素代表一个模板命令,方便去重
	ExtraStartTpl []string // 一个元素代表一个模板命令,方便去重
}

func FormatOutputInstruct added in v0.0.5

func FormatOutputInstruct(instruct Instruct, root string) (newInstruct Instruct)

type InstructTpl

type InstructTpl struct {
	ID        ID
	Version   string
	Type      string
	Instructs Instructs
}

func FormatOutputTplInstruct added in v0.0.5

func FormatOutputTplInstruct(instructTpl InstructTpl) (newInstructTpl InstructTpl)

func ParseInstructTp

func ParseInstructTp(lineschema Jsonschemaline) (instructTpl *InstructTpl)

func (*InstructTpl) String

func (instructTpl *InstructTpl) String() string

type InstructTpls

type InstructTpls []*InstructTpl

func (InstructTpls) GetByID

func (instructTpls InstructTpls) GetByID(id string) *InstructTpl

type Instructs

type Instructs []*Instruct

func (Instructs) Len

func (a Instructs) Len() int

func (Instructs) Less

func (a Instructs) Less(i, j int) bool

func (Instructs) Swap

func (a Instructs) Swap(i, j int)

func (Instructs) Unique

func (instructs Instructs) Unique() Instructs

type Jsonschemaline

type Jsonschemaline struct {
	Meta  *Meta
	Items []*JsonschemalineItem
}

func Json2lineSchema

func Json2lineSchema(jsonStr string) (out *Jsonschemaline, err error)

Json2gsonPatch

func ParseJsonschemaline

func ParseJsonschemaline(jsonschemalineBlock string) (jsonschemaline *Jsonschemaline, err error)

ParseJsonschemaline 解析单个 jsonschemaline

func ParseMultiJsonSchemaline

func ParseMultiJsonSchemaline(jsonschemalineBlocks string) (jsonschemalines []*Jsonschemaline, err error)

ParseMultiJsonSchemaline 解析多个 jsonschemaline

func (*Jsonschemaline) DefaultJson added in v0.0.29

func (l *Jsonschemaline) DefaultJson() (defaultData string, err error)

DefaultJson 获取默认值

func (*Jsonschemaline) GjsonPath added in v0.0.8

func (l *Jsonschemaline) GjsonPath(ignoreID bool, formatPath func(format string, src string, item *JsonschemalineItem) (path string)) (gjsonPath string)

func (*Jsonschemaline) GjsonPathWithDefaultFormat added in v0.0.31

func (l *Jsonschemaline) GjsonPathWithDefaultFormat(ignoreID bool) (gjsonPath string)

GjsonPathWithDefaultFormat 生成格式化的jsonpath,用来重新格式化数据,比如入参字段类型全为字符串,在format中标记了实际类型,可以通过该方法获取转换数据的gjson path,从入参中提取数据后,对应字段类型就以format为准,此处仅仅提供有创意的案例,更多可以依据该思路扩展

func (*Jsonschemaline) JsonExample added in v0.0.19

func (l *Jsonschemaline) JsonExample() (jsonExample string, err error)

func (*Jsonschemaline) JsonSchema added in v0.0.2

func (l *Jsonschemaline) JsonSchema() (jsonschemaByte []byte, err error)

func (*Jsonschemaline) Jsonschemaline2json

func (l *Jsonschemaline) Jsonschemaline2json() (jsonStr string, err error)

Deprecated see JsonExample

func (*Jsonschemaline) String

func (l *Jsonschemaline) String() string

func (*Jsonschemaline) ToSturct added in v0.0.21

func (l *Jsonschemaline) ToSturct() (structs Structs)

type JsonschemalineItem

type JsonschemalineItem struct {
	Comments string `json:"comment,omitempty"` // section 8.3

	Type             string   `json:"type,omitempty"`                    // section 6.1.1
	Enum             []string `json:"enum,omitempty"`                    // section 6.1.2
	EnumNames        []string `json:"enumNames,omitempty"`               // section 6.1.2
	Const            string   `json:"const,omitempty"`                   // section 6.1.3
	MultipleOf       int      `json:"multipleOf,omitempty,string"`       // section 6.2.1
	Maximum          int      `json:"maximum,omitempty,string"`          // section 6.2.2
	ExclusiveMaximum bool     `json:"exclusiveMaximum,omitempty,string"` // section 6.2.3
	Minimum          int      `json:"minimum,omitempty,string"`          // section 6.2.4
	ExclusiveMinimum bool     `json:"exclusiveMinimum,omitempty,string"` // section 6.2.5
	MaxLength        int      `json:"maxLength,omitempty,string"`        // section 6.3.1
	MinLength        int      `json:"minLength,omitempty,string"`        // section 6.3.2
	Pattern          string   `json:"pattern,omitempty"`                 // section 6.3.3
	MaxItems         int      `json:"maxItems,omitempty,string"`         // section 6.4.1
	MinItems         int      `json:"minItems,omitempty,string"`         // section 6.4.2
	UniqueItems      bool     `json:"uniqueItems,omitempty,string"`      // section 6.4.3
	MaxContains      uint     `json:"maxContains,omitempty,string"`      // section 6.4.4
	MinContains      uint     `json:"minContains,omitempty,string"`      // section 6.4.5
	MaxProperties    int      `json:"maxProperties,omitempty,string"`    // section 6.5.1
	MinProperties    int      `json:"minProperties,omitempty,string"`    // section 6.5.2
	Required         bool     `json:"required,omitempty,string"`         // section 6.5.3
	// RFC draft-bhutton-json-schema-validation-00, section 7
	Format string `json:"format,omitempty"`
	// RFC draft-bhutton-json-schema-validation-00, section 8
	ContentEncoding  string       `json:"contentEncoding,omitempty"`   // section 8.3
	ContentMediaType string       `json:"contentMediaType,omitempty"`  // section 8.4
	Title            string       `json:"title,omitempty"`             // section 9.1
	Description      string       `json:"description,omitempty"`       // section 9.1
	Default          string       `json:"default,omitempty"`           // section 9.2
	Deprecated       bool         `json:"deprecated,omitempty,string"` // section 9.3
	ReadOnly         bool         `json:"readOnly,omitempty,string"`   // section 9.4
	WriteOnly        bool         `json:"writeOnly,omitempty,string"`  // section 9.4
	Example          string       `json:"example,omitempty"`           // section 9.5
	Examples         string       `json:"examples,omitempty"`          // section 9.5
	Src              string       `json:"src,omitempty"`
	Dst              string       `json:"dst,omitempty"`
	Fullname         string       `json:"fullname,omitempty"`
	AllowEmptyValue  bool         `json:"allowEmptyValue,omitempty,string"`
	TagLineKVpair    kvstruct.KVS `json:"-"`
}

func (JsonschemalineItem) Json added in v0.0.15

func (jItem JsonschemalineItem) Json() (jsonStr string)

func (JsonschemalineItem) ToJsonSchemaKVS added in v0.0.16

func (jItem JsonschemalineItem) ToJsonSchemaKVS() (kvs kvstruct.KVS, err error)

func (JsonschemalineItem) ToKVS added in v0.0.25

func (jItem JsonschemalineItem) ToKVS(namespance string) (kvs kvstruct.KVS)

type Meta

type Meta struct {
	ID        ID     `json:"id"`
	Version   string `json:"version"`
	Direction string `json:"direction"`
}

func ParseMeta

func ParseMeta(metaLineTags kvstruct.KVS) (meta Meta)

type Reflector

type Reflector struct {
	// BaseSchemaID defines the URI that will be used as a base to determine Schema
	// IDs for models. For example, a base Schema ID of `https://invopop.com/schemas`
	// when defined with a struct called `User{}`, will result in a schema with an
	// ID set to `https://invopop.com/schemas/user`.
	//
	// If no `BaseSchemaID` is provided, we'll take the type's complete package path
	// and use that as a base instead. Set `Anonymous` to try if you do not want to
	// include a schema ID.
	BaseSchemaID ID

	// Anonymous when true will hide the auto-generated Schema ID and provide what is
	// known as an "anonymous schema". As a rule, this is not recommended.
	Anonymous bool

	// AssignAnchor when true will use the original struct's name as an anchor inside
	// every definition, including the root schema. These can be useful for having a
	// reference to the original struct's name in CamelCase instead of the snake-case used
	// by default for URI compatibility.
	//
	// Anchors do not appear to be widely used out in the wild, so at this time the
	// anchors themselves will not be used inside generated schema.
	AssignAnchor bool

	// AllowAdditionalProperties will cause the Reflector to generate a schema
	// without additionalProperties set to 'false' for all struct types. This means
	// the presence of additional keys in JSON objects will not cause validation
	// to fail. Note said additional keys will simply be dropped when the
	// validated JSON is unmarshaled.
	AllowAdditionalProperties bool

	// RequiredFromJSONSchemaTags will cause the Reflector to generate a schema
	// that requires any key tagged with `jsonschema:required`, overriding the
	// default of requiring any key *not* tagged with `json:,omitempty`.
	RequiredFromJSONSchemaTags bool

	// YAMLEmbeddedStructs will cause the Reflector to generate a schema that does
	// not inline embedded structs. This should be enabled if the JSON schemas are
	// used with yaml.Marshal/Unmarshal.
	YAMLEmbeddedStructs bool

	// Prefer yaml: tags over json: tags to generate the schema even if json: tags
	// are present
	PreferYAMLSchema bool

	// Do not reference definitions. This will remove the top-level $defs map and
	// instead cause the entire structure of types to be output in one tree. The
	// list of type definitions (`$defs`) will not be included.
	DoNotReference bool

	// ExpandedStruct when true will include the reflected type's definition in the
	// root as opposed to a definition with a reference. Using a reference in the root
	// is useful as it allows us to maintain the struct's original name, but it is
	// not common practice.
	ExpandedStruct bool

	// IgnoredTypes defines a slice of types that should be ignored in the schema,
	// switching to just allowing additional properties instead.
	IgnoredTypes []interface{}

	// Lookup allows a function to be defined that will provide a custom mapping of
	// types to Schema IDs. This allows existing schema documents to be referenced
	// by their ID instead of being embedded into the current schema definitions.
	// Reflected types will never be pointers, only underlying elements.
	Lookup func(reflect.Type) ID

	// Mapper is a function that can be used to map custom Go types to jsonschema schemas.
	Mapper func(reflect.Type) *Schema

	// Namer allows customizing of type names. The default is to use the type's name
	// provided by the reflect package.
	Namer func(reflect.Type) string

	// KeyNamer allows customizing of key names.
	// The default is to use the key's name as is, or the json (or yaml) tag if present.
	// If a json or yaml tag is present, KeyNamer will receive the tag's name as an argument, not the original key name.
	KeyNamer func(string) string

	// AdditionalFields allows adding structfields for a given type
	AdditionalFields func(reflect.Type) []reflect.StructField

	// CommentMap is a dictionary of fully qualified go types and fields to comment
	// strings that will be used if a description has not already been provided in
	// the tags. Types and fields are added to the package path using "." as a
	// separator.
	//
	// Type descriptions should be defined like:
	//
	//   map[string]string{"github.com/invopop/jsonschema.Reflector": "A Reflector reflects values into a Schema."}
	//
	// And Fields defined as:
	//
	//   map[string]string{"github.com/invopop/jsonschema.Reflector.DoNotReference": "Do not reference definitions."}
	//
	// See also: AddGoComments
	CommentMap map[string]string
}

A Reflector reflects values into a Schema.

func (*Reflector) AddGoComments

func (r *Reflector) AddGoComments(base, path string) error

AddGoComments will update the reflectors comment map with all the comments found in the provided source directories. See the #ExtractGoComments method for more details.

func (*Reflector) Reflect

func (r *Reflector) Reflect(v interface{}) *Schema

Reflect reflects to Schema from a value.

func (*Reflector) ReflectFromType

func (r *Reflector) ReflectFromType(t reflect.Type) *Schema

ReflectFromType generates root schema

func (*Reflector) SetBaseSchemaID

func (r *Reflector) SetBaseSchemaID(id string)

SetBaseSchemaID is a helper use to be able to set the reflectors base schema ID from a string as opposed to then ID instance.

type Schema

type Schema struct {
	// RFC draft-bhutton-json-schema-00
	Version     string      `json:"$schema,omitempty"`     // section 8.1.1
	ID          ID          `json:"$id,omitempty"`         // section 8.2.1
	Anchor      string      `json:"$anchor,omitempty"`     // section 8.2.2
	Ref         string      `json:"$ref,omitempty"`        // section 8.2.3.1
	DynamicRef  string      `json:"$dynamicRef,omitempty"` // section 8.2.3.2
	Definitions Definitions `json:"$defs,omitempty"`       // section 8.2.4
	Comments    string      `json:"$comment,omitempty"`    // section 8.3
	// RFC draft-bhutton-json-schema-00 section 10.2.1 (Sub-schemas with logic)
	AllOf []*Schema `json:"allOf,omitempty"` // section 10.2.1.1
	AnyOf []*Schema `json:"anyOf,omitempty"` // section 10.2.1.2
	OneOf []*Schema `json:"oneOf,omitempty"` // section 10.2.1.3
	Not   *Schema   `json:"not,omitempty"`   // section 10.2.1.4
	// RFC draft-bhutton-json-schema-00 section 10.2.2 (Apply sub-schemas conditionally)
	If               *Schema            `json:"if,omitempty"`               // section 10.2.2.1
	Then             *Schema            `json:"then,omitempty"`             // section 10.2.2.2
	Else             *Schema            `json:"else,omitempty"`             // section 10.2.2.3
	DependentSchemas map[string]*Schema `json:"dependentSchemas,omitempty"` // section 10.2.2.4
	// RFC draft-bhutton-json-schema-00 section 10.3.1 (arrays)
	PrefixItems []*Schema `json:"prefixItems,omitempty"` // section 10.3.1.1
	Items       *Schema   `json:"items,omitempty"`       // section 10.3.1.2  (replaces additionalItems)
	Contains    *Schema   `json:"contains,omitempty"`    // section 10.3.1.3
	// RFC draft-bhutton-json-schema-00 section 10.3.2 (sub-schemas)
	Properties           *orderedmap.OrderedMap `json:"properties,omitempty"`           // section 10.3.2.1
	PatternProperties    map[string]*Schema     `json:"patternProperties,omitempty"`    // section 10.3.2.2
	AdditionalProperties *Schema                `json:"additionalProperties,omitempty"` // section 10.3.2.3
	PropertyNames        *Schema                `json:"propertyNames,omitempty"`        // section 10.3.2.4
	// RFC draft-bhutton-json-schema-validation-00, section 6
	Type              string              `json:"type,omitempty"`              // section 6.1.1
	Enum              []interface{}       `json:"enum,omitempty"`              // section 6.1.2
	Const             interface{}         `json:"const,omitempty"`             // section 6.1.3
	MultipleOf        int                 `json:"multipleOf,omitempty"`        // section 6.2.1
	Maximum           int                 `json:"maximum,omitempty"`           // section 6.2.2
	ExclusiveMaximum  bool                `json:"exclusiveMaximum,omitempty"`  // section 6.2.3
	Minimum           int                 `json:"minimum,omitempty"`           // section 6.2.4
	ExclusiveMinimum  bool                `json:"exclusiveMinimum,omitempty"`  // section 6.2.5
	MaxLength         int                 `json:"maxLength,omitempty"`         // section 6.3.1
	MinLength         int                 `json:"minLength,omitempty"`         // section 6.3.2
	Pattern           string              `json:"pattern,omitempty"`           // section 6.3.3
	MaxItems          int                 `json:"maxItems,omitempty"`          // section 6.4.1
	MinItems          int                 `json:"minItems,omitempty"`          // section 6.4.2
	UniqueItems       bool                `json:"uniqueItems,omitempty"`       // section 6.4.3
	MaxContains       uint                `json:"maxContains,omitempty"`       // section 6.4.4
	MinContains       uint                `json:"minContains,omitempty"`       // section 6.4.5
	MaxProperties     int                 `json:"maxProperties,omitempty"`     // section 6.5.1
	MinProperties     int                 `json:"minProperties,omitempty"`     // section 6.5.2
	Required          []string            `json:"required,omitempty"`          // section 6.5.3
	DependentRequired map[string][]string `json:"dependentRequired,omitempty"` // section 6.5.4
	// RFC draft-bhutton-json-schema-validation-00, section 7
	Format string `json:"format,omitempty"`
	// RFC draft-bhutton-json-schema-validation-00, section 8
	ContentEncoding  string  `json:"contentEncoding,omitempty"`  // section 8.3
	ContentMediaType string  `json:"contentMediaType,omitempty"` // section 8.4
	ContentSchema    *Schema `json:"contentSchema,omitempty"`    // section 8.5
	// RFC draft-bhutton-json-schema-validation-00, section 9
	Title       string        `json:"title,omitempty"`       // section 9.1
	Description string        `json:"description,omitempty"` // section 9.1
	Default     interface{}   `json:"default,omitempty"`     // section 9.2
	Deprecated  bool          `json:"deprecated,omitempty"`  // section 9.3
	ReadOnly    bool          `json:"readOnly,omitempty"`    // section 9.4
	WriteOnly   bool          `json:"writeOnly,omitempty"`   // section 9.4
	Examples    []interface{} `json:"examples,omitempty"`    // section 9.5

	Extras map[string]interface{} `json:"-"`

	Src          string  `json:"src,omitempty"`
	Dst          string  `json:"dst,omitempty"`
	Fullname     string  `json:"fullname,omitempty"`
	Tpl          string  `json:"tpl,omitempty"`
	PropertyName string  `json:"-"`
	Parent       *Schema `json:"-"`
	// contains filtered or unexported fields
}

Schema represents a JSON Schema object type. RFC draft-bhutton-json-schema-00 section 4.3

func Reflect

func Reflect(v interface{}) *Schema

Reflect reflects to Schema from a value using the default Reflector

Example
package main

import (
	"encoding/json"
	"fmt"
	"time"

	jsonschema "github.com/suifengpiao14/jsonschemaline"
)

type SampleUser struct {
	ID          int                    `json:"id"`
	Name        string                 `json:"name" jsonschema:"title=the name,description=The name of a friend,example=joe,example=lucy,default=alex"`
	Friends     []int                  `json:"friends,omitempty" jsonschema_description:"The list of IDs, omitted when empty"`
	Tags        map[string]interface{} `json:"tags,omitempty" jsonschema_extras:"a=b,foo=bar,foo=bar1"`
	BirthDate   time.Time              `json:"birth_date,omitempty" jsonschema:"oneof_required=date"`
	YearOfBirth string                 `json:"year_of_birth,omitempty" jsonschema:"oneof_required=year"`
	Metadata    interface{}            `json:"metadata,omitempty" jsonschema:"oneof_type=string;array"`
	FavColor    string                 `json:"fav_color,omitempty" jsonschema:"enum=red,enum=green,enum=blue"`
}

func main() {
	s := jsonschema.Reflect(&SampleUser{})
	data, _ := json.MarshalIndent(s, "", "  ")
	fmt.Println(string(data))
}
Output:

{
  "$schema": "http://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/invopop/jsonschema_test/sample-user",
  "$ref": "#/$defs/SampleUser",
  "$defs": {
    "SampleUser": {
      "oneOf": [
        {
          "required": [
            "birth_date"
          ],
          "title": "date"
        },
        {
          "required": [
            "year_of_birth"
          ],
          "title": "year"
        }
      ],
      "properties": {
        "id": {
          "type": "integer"
        },
        "name": {
          "type": "string",
          "title": "the name",
          "description": "The name of a friend",
          "default": "alex",
          "examples": [
            "joe",
            "lucy"
          ]
        },
        "friends": {
          "items": {
            "type": "integer"
          },
          "type": "array",
          "description": "The list of IDs, omitted when empty"
        },
        "tags": {
          "type": "object",
          "a": "b",
          "foo": [
            "bar",
            "bar1"
          ]
        },
        "birth_date": {
          "type": "string",
          "format": "date-time"
        },
        "year_of_birth": {
          "type": "string"
        },
        "metadata": {
          "oneOf": [
            {
              "type": "string"
            },
            {
              "type": "array"
            }
          ]
        },
        "fav_color": {
          "type": "string",
          "enum": [
            "red",
            "green",
            "blue"
          ]
        }
      },
      "additionalProperties": false,
      "type": "object",
      "required": [
        "id",
        "name"
      ]
    }
  }
}

func ReflectFromType

func ReflectFromType(t reflect.Type) *Schema

ReflectFromType generates root schema using the default Reflector

func (*Schema) GetByFullname

func (t *Schema) GetByFullname(fullname string) *Schema

func (*Schema) GetByName

func (t *Schema) GetByName(name string) *Schema

func (*Schema) Lineschema

func (t *Schema) Lineschema() (lineSchema string, err error)

func (*Schema) MarshalJSON

func (t *Schema) MarshalJSON() ([]byte, error)

func (*Schema) Raw2Schema

func (t *Schema) Raw2Schema(lineSchema Jsonschemaline)

func (*Schema) UnmarshalJSON

func (t *Schema) UnmarshalJSON(data []byte) error

UnmarshalJSON is used to parse a schema object or boolean.

type Struct added in v0.0.21

type Struct struct {
	IsRoot     bool
	Name       string
	Lineschema string
	Attrs      []*StructAttr
}

func (*Struct) AddAttrIgnore added in v0.0.21

func (s *Struct) AddAttrIgnore(attrs ...StructAttr)

AddAttrIgnore 已经存在则跳过

func (*Struct) AddAttrReplace added in v0.0.21

func (s *Struct) AddAttrReplace(attrs ...StructAttr)

AddAttrReplace 增加或者替换

func (*Struct) GetAttr added in v0.0.21

func (s *Struct) GetAttr(attrName string) (structAttr *StructAttr, exists bool)

type StructAttr added in v0.0.21

type StructAttr struct {
	Name    string
	Type    string
	Tag     string
	Comment string
}

type Structs added in v0.0.21

type Structs []*Struct

func (*Structs) AddIngore added in v0.0.21

func (s *Structs) AddIngore(structs ...*Struct)

func (*Structs) AddNameprefix added in v0.0.26

func (s *Structs) AddNameprefix(nameprefix string)

func (Structs) Copy added in v0.0.26

func (s Structs) Copy() (newStructs Structs)

Copy 深度复制

func (*Structs) Get added in v0.0.21

func (s *Structs) Get(name string) (struc *Struct, exists bool)

func (*Structs) GetRoot added in v0.0.21

func (s *Structs) GetRoot() (struc *Struct, exists bool)

func (*Structs) Json added in v0.0.21

func (s *Structs) Json() (str string)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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