yaml_checker

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2023 License: GPL-3.0 Imports: 6 Imported by: 0

README

yaml_checker

一、校验原理说明

涉及两种类型的YAML文件:schema文件和被校验文件,校验器的原理就是通过schema定义YAML文件的结构,利用schema定义检查被校验文件的有效性。

schema文件必须是schema为根节点的YAML文件,schema节点类型有object和field两种,如果需要将定义的object或者field类型节点升级为数组,只需要as_array字段为true即可。

object类型的节点属性:

type: object # 必须为object
name: # 必传,yaml对象的名称
describe: # 必传,yaml对象的描述
required: # 非必传,该字段在被检测文件中是否是必传字段,默认为false
as_array: # 非必传,是否升级为数组类型,默认为false
fields: # 子字段定义,可以是object或者field类型的节点定义

field类型的节点属性:

type: field # 必须为field
name: # 必传,yaml对象的名称
describe: # 必传,yaml对象的描述
required: # 非必传,该字段在被检测文件中是否是必传字段,默认为false
as_array: # 非必传,是否升级为数组类型,默认为false
field_type: # 字段类型,可以是string,int,bool

给出一个较完整的示例:

schema:
  - type: field
    name: test_field_array
    describe: 测试field数组(必传)
    required: true
    as_array: true
    field_type: string
  - type: field
    name: test_field
    describe: 测试field(必传)
    required: true
    field_type: string
  - type: object
    name: test_obj_array
    describe: 测试对象(必传)
    required: true
    as_array: true
    fields:
      - type: field
        name: field
        describe: 测试对象字段类型字段(非必传)
        required: false
        field_type: string
  - type: object
    name: test_obj
    describe: 测试对象(必传)
    required: true
    fields:
      - type: field
        name: field_array
        describe: 测试对象字段类型数组字段(必传)
        required: true
        as_array: true
        field_type: string
      - type: field
        name: field
        describe: 测试对象字段类型字段(非必传)
        required: false
        field_type: string
      - type: object
        name: obj_array
        describe: 测试对象包含对象数组(必传)
        required: true
        as_array: true
        fields:
          - type: field
            name: obj_array_field1
            describe: 对象数组字段1(必传)
            required: true
            field_type: string
          - type: field
            name: obj_array_field2
            describe: 对象数组字段2(非必传)
            required: false
            field_type: string

一个有效的YAML文件,如下所示:

test_field_array:
  - aaa
  - bbb
  - ccc
test_field: ddd
test_obj_array:
  - field: aaa
test_obj:
  field_array:
    - aaa
    - bbb
  field: ccc
  obj_array:
    - obj_array_field1: aaa
    - obj_array_field1: bbb
      obj_array_field2: ccc

二、包使用说明

首先导入包

import "git.sxidc.com/go-tools/yaml_checker"
schemaDoc, err := NewSchemaDoc("schema.yaml")
if err != nil {
    t.Fatal(err)
}

err = schemaDoc.ValidateFile("test.yaml")
if err != nil {
    t.Fatal(err)
}

/*
err = schemaDoc.ValidateMap(yamlMap)
if err != nil {
    t.Fatal(err)
}
*/

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseNamingSchemaNode

type BaseNamingSchemaNode struct {
	Type     string
	Name     string
	Describe string
	Required bool
}

type FieldSchemaNode

type FieldSchemaNode struct {
	BaseNamingSchemaNode
	AsArray   bool
	FieldType string
}

func (*FieldSchemaNode) Validate

func (schema *FieldSchemaNode) Validate(yamlMap map[string]any) error

type ObjectSchemaNode

type ObjectSchemaNode struct {
	BaseNamingSchemaNode
	AsArray bool
	Fields  []SchemaNode
}

func (*ObjectSchemaNode) Validate

func (schema *ObjectSchemaNode) Validate(yamlMap map[string]any) error

type SchemaDoc

type SchemaDoc struct {
	// contains filtered or unexported fields
}

func NewSchemaDoc

func NewSchemaDoc(schemaFilePath string) (*SchemaDoc, error)

func (*SchemaDoc) ValidateFile

func (schemaDoc *SchemaDoc) ValidateFile(filePath string) error

func (*SchemaDoc) ValidateMap

func (schemaDoc *SchemaDoc) ValidateMap(yamlMap map[string]any) error

type SchemaDocYaml

type SchemaDocYaml struct {
	RootSchemas []SchemaYaml `yaml:"schema"`
}

type SchemaNode

type SchemaNode interface {
	Validate(yamlMap map[string]any) error
}

type SchemaYaml

type SchemaYaml struct {
	Type     string `yaml:"type"`
	Name     string `yaml:"name"`
	Describe string `yaml:"describe"`
	Required bool   `yaml:"required"`
	AsArray  bool   `yaml:"as_array"`

	// object
	Fields []SchemaYaml `yaml:"fields"`

	// field
	FieldType string `yaml:"field_type"`
}

Jump to

Keyboard shortcuts

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