README
¶
xsdvalidate
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. Look at this for an example using Go's embed
package to bake an XML schema into a simple http server.
To see how this could be plugged into middleware see the go-chi example I came up with.
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
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.
Example ¶
An example on how to use the package. Init() is only required once before parsing and validating, and Cleanup() respectively when finished.
Output: 3: Element 'shipto': This element is not expected. Expected is ( orderperson ). Error in line: 3 Element 'shipto': This element is not expected. Expected is ( orderperson ). 3: Element 'shipto': This element is not expected. Expected is ( orderperson ). Error in line: 3 Element 'shipto': This element is not expected. Expected is ( orderperson ).
Index ¶
Examples ¶
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 InitWithGc ¶
InitWithGc initializes lbxml2 with a goroutine that runs the go gc every d duration. Not required but might 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.
type Options ¶
type Options uint8
Options type for parser/validation options.
type StructError ¶
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).
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 ¶ added in v0.1.5
func NewXsdHandlerMem(inSchema []byte, options Options) (*XsdHandler, error)
NewXsdHandlerMem creates an xsd handler struct. Always use Free() method when done using this handler or memory will leak. 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).
Directories
¶
Path | Synopsis |
---|---|
examples
|
|
_server/chi
An example using go-chi (https://github.com/go-chi/chi) middleware and context
|
An example using go-chi (https://github.com/go-chi/chi) middleware and context |
_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 |
_server/simpler_mem
A simpler standalone example for xsd validation and http
|
A simpler standalone example for xsd validation and http |