All Projects → terminalstatic → go-xsd-validate

terminalstatic / go-xsd-validate

Licence: MIT license
Xsd validation for go based on libxml2

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to go-xsd-validate

xslweb
Web application framework for XSLT and XQuery developers
Stars: ✭ 39 (-7.14%)
Mutual labels:  xsd
openimmo
OpenImmo library
Stars: ✭ 36 (-14.29%)
Mutual labels:  xsd
SAF-T-AO
Official XSD from the Government of Angola for use in SAF-T AO
Stars: ✭ 42 (+0%)
Mutual labels:  xsd
xml-lint
A php tool to lint and validate xml files from the commandline.
Stars: ✭ 14 (-66.67%)
Mutual labels:  xsd
xsd-reader
Pure PHP XSD Reader (XML Schema)
Stars: ✭ 45 (+7.14%)
Mutual labels:  xsd
xgen
XSD (XML Schema Definition) parser and Go/C/Java/Rust/TypeScript code generator
Stars: ✭ 153 (+264.29%)
Mutual labels:  xsd
xrechnung-visualization
XSL transformators for web and pdf rendering of German CIUS XRechnung or EN16931-1:2017 [MIRROR OF GitLab]
Stars: ✭ 26 (-38.1%)
Mutual labels:  xsd
xsd-parser-rs
A xsd/wsdl => rust code generator written in rust
Stars: ✭ 45 (+7.14%)
Mutual labels:  xsd
DynAdjust
Least squares adjustment software
Stars: ✭ 43 (+2.38%)
Mutual labels:  xsd
xsdata
Naive XML & JSON Bindings for python
Stars: ✭ 144 (+242.86%)
Mutual labels:  xsd
jsons2xsd
Highly configurable converter from JSON-schema to XML-schema (XSD).
Stars: ✭ 65 (+54.76%)
Mutual labels:  xsd
LinqToXsdCore
LinqToXsd ported to .NET Core (targets .NET Standard 2 for generated code and .NET Core 3.1, .NET 5+ 6 for the code generator CLI tool).
Stars: ✭ 23 (-45.24%)
Mutual labels:  xsd
xsd to django model
Generate Django models from an XSD schema description (and a bunch of hints)
Stars: ✭ 20 (-52.38%)
Mutual labels:  xsd
jgeXml
The Just-Good-Enough XML Toolkit
Stars: ✭ 20 (-52.38%)
Mutual labels:  xsd
phoenix.webui.framework
基于WebDriver的WebUI自动化测试框架
Stars: ✭ 118 (+180.95%)
Mutual labels:  xsd
Ono
A sensible way to deal with XML & HTML for iOS & macOS
Stars: ✭ 2,599 (+6088.1%)
Mutual labels:  libxml2
Nokogiri
Nokogiri (鋸) makes it easy and painless to work with XML and HTML from Ruby.
Stars: ✭ 5,748 (+13585.71%)
Mutual labels:  libxml2
libxml2-win-build
libxml2 Windows build with Visual Studio.
Stars: ✭ 45 (+7.14%)
Mutual labels:  libxml2

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. 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

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].