xsdvalidate

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2019 License: MIT Imports: 8 Imported by: 0

README

xsdvalidate

GoDoc

The goal of this package is to preload xsd files into memory and to validate xml (fast) using libxml2, like post bodys of xml service endpoints or api routers. At the time of writing, similar packages I found on github either didn't provide error details or got stuck under load. In addition to providing error strings it also exposes some fields of libxml2 return structs.

Api Reference

https://godoc.org/github.com/terminalstatic/go-xsd-validate

Install

Install libxml2 dev via distribution package manager or from source, below an example how to install the latest libxml2 from source on linux(Debian/Ubuntu):

curl -L ftp://xmlsoft.org/libxml2/LATEST_LIBXML2 -o ./LIBXML2_LATEST.tar.gz
tar -xf ./LIBXML2_LATEST.tar.gz
cd ./libxml2*
./configure --prefix=/usr  --enable-static --with-threads --with-history
make
sudo make install

Go get the package:

go get github.com/terminalstatic/go-xsd-validate

Examples

Check this for a simple http server example and that for an even simpler one.

	xsdvalidate.Init()
	defer xsdvalidate.Cleanup()
	xsdhandler, err := xsdvalidate.NewXsdHandlerUrl("examples/test1_split.xsd", xsdvalidate.ParsErrDefault)
	if err != nil {
		panic(err)
	}
	defer xsdhandler.Free()

	xmlFile, err := os.Open("examples/test1_fail2.xml")
	if err != nil {
		panic(err)
	}
	defer xmlFile.Close()
	inXml, err := ioutil.ReadAll(xmlFile)
	if err != nil {
		panic(err)
	}

	// Option 1:
	xmlhandler, err := xsdvalidate.NewXmlHandlerMem(inXml, xsdvalidate.ParsErrDefault)
	if err != nil {
		panic(err)
	}
	defer xmlhandler.Free()

	err = xsdhandler.Validate(xmlhandler, xsdvalidate.ValidErrDefault)
	if err != nil {
		switch err.(type) {
		case xsdvalidate.ValidationError:
			fmt.Println(err)
			fmt.Printf("Error in line: %d\n", err.(xsdvalidate.ValidationError).Errors[0].Line)
			fmt.Println(err.(xsdvalidate.ValidationError).Errors[0].Message)
		default:
			fmt.Println(err)
		}
	}

	// Option 2:
	err = xsdhandler.ValidateMem(inXml, xsdvalidate.ValidErrDefault)
	ifT err != nil {
		switch err.(type) {
		case xsdvalidate.ValidationError:
			fmt.Println(err)
			fmt.Printf("Error in line: %d\n", err.(xsdvalidate.ValidationError).Errors[0].Line)
			fmt.Println(err.(xsdvalidate.ValidationError).Errors[0].Message)
		default:
			fmt.Println(err)
		}
	}

Licence

MIT

Documentation

Overview

The goal of this package is to preload xsd files into memory and to validate xml (fast) using libxml2, like post bodys of xml service endpoints or api routers. At the time of writing, similar packages I found on github either didn't provide error details or got stuck under load. In addition to providing error strings it also exposes some fields of libxml2 return structs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cleanup

func Cleanup()

Cleanup cleans up libxml2 memory and finishes gc goroutine when running.

func Init

func Init() error

Init initializes libxml2, see http://xmlsoft.org/threads.html.

func InitWithGc

func InitWithGc(d time.Duration)

InitWithGc initializes lbxml2 with a goroutine that trims memory and runs the go gc every d duration. Not required but can help to keep the memory footprint at bay when doing tons of validations.

Types

type Libxml2Error

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

Libxml2Error is returned when a Libxm2 initialization error occured.

func (Libxml2Error) Error

func (e Libxml2Error) Error() string

Implementation of the Error Interface.

func (Libxml2Error) String

func (e Libxml2Error) String() string

Implementation of the Stringer Interface.

type Options

type Options uint8

Options type for parser/validation options.

const (
	ParsErrDefault Options = 1 << iota // Default parser error output
	ParsErrVerbose                     // Verbose parser error output, considerably slower!
)

The parser options, ParsErrVerbose will slow down parsing considerably!

const (
	ValidErrDefault Options = 128 << iota // Default validation error output
)

Validation options for possible future enhancements.

type StructError

type StructError struct {
	Code     int    `json:"code"`
	Message  string `json:"message"`
	Level    int    `json:"level"`
	Line     int    `json:"line"`
	NodeName string `json:"node_name"`
}

StructError is a subset of libxml2 xmlError struct.

type ValidationError

type ValidationError struct {
	Errors []StructError
}

ValidationError is returned when xsd validation caused an error, to access the fields of the Errors slice use type assertion (see example).

func (ValidationError) Error

func (e ValidationError) Error() string

Implementation of the Error interface.

func (ValidationError) String

func (e ValidationError) String() string

Implementation of the Stringer interface. Aggregates line numbers and messages of the Errors slice.

type XmlHandler

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

XmlHandler handles xml parsing and wraps a pointer to libxml2's xmlDocPtr.

func NewXmlHandlerMem

func NewXmlHandlerMem(inXml []byte, options Options) (*XmlHandler, error)

NewXmlHandlerMem creates a xml handler struct. If an error is returned it can be of type Libxml2Error or XmlParserError. Always use the Free() method when done using this handler or memory will be leaking. The go garbage collector will not collect the allocated resources.

func (*XmlHandler) Free

func (xmlHandler *XmlHandler) Free()

Free frees the wrapped xml docPtr, call this when this handler is not needed anymore.

type XmlParserError

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

XmlParserError is returned when xml parsing caused error(s).

func (XmlParserError) Error

func (e XmlParserError) Error() string

Implementation of the Error Interface.

func (XmlParserError) String

func (e XmlParserError) String() string

Implementation of the Stringer Interface.

type XsdHandler

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

XsdHandler handles schema parsing and validation and wraps a pointer to libxml2's xmlSchemaPtr.

func NewXsdHandlerMem

func NewXsdHandlerMem(inXml []byte, options Options) (*XsdHandler, error)

NewXsdHandlerUrl creates a xsd handler struct from mem instead of location. Always use Free() method when done using this handler or memory will be leaking. If an error is returned it can be of type Libxml2Error or XsdParserError. The go garbage collector will not collect the allocated resources.

func NewXsdHandlerUrl

func NewXsdHandlerUrl(url string, options Options) (*XsdHandler, error)

NewXsdHandlerUrl creates a xsd handler struct. Always use Free() method when done using this handler or memory will be leaking. If an error is returned it can be of type Libxml2Error or XsdParserError. The go garbage collector will not collect the allocated resources.

func (*XsdHandler) Free

func (xsdHandler *XsdHandler) Free()

Free frees the wrapped schemaPtr, call this when this handler is not needed anymore.

func (*XsdHandler) Validate

func (xsdHandler *XsdHandler) Validate(xmlHandler *XmlHandler, options Options) error

Validate validates an xmlHandler against an xsdHandler and returns a ValidationError. If an error is returned it is of type Libxml2Error, XsdParserError, XmlParserError or ValidationError. Both xmlHandler and xsdHandler have to be created first.

func (*XsdHandler) ValidateMem

func (xsdHandler *XsdHandler) ValidateMem(inXml []byte, options Options) error

ValidateMem validates an xml byte slice against an xsdHandler. If an error is returned it can be of type Libxml2Error, XsdParserError, XmlParserError or ValidationError. The xsdHandler has to be created first.

type XsdParserError

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

XsdParserError is returned when xsd parsing caused a error(s).

func (XsdParserError) Error

func (e XsdParserError) Error() string

Implementation of the Error Interface.

func (XsdParserError) String

func (e XsdParserError) String() string

Implementation of the Stringer Interface.

Directories

Path Synopsis
examples
_server/simple
A simple standalone example for xsd validation and http
A simple standalone example for xsd validation and http
_server/simpler
A simpler standalone example for xsd validation and http
A simpler standalone example for xsd validation and http

Jump to

Keyboard shortcuts

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