Skip to content

JSON data validation tool based on stripped down XML schema

License

Notifications You must be signed in to change notification settings

guinso/gxschema

Repository files navigation

gxschema

Data validation tool, a simplified version of XML schema.

TODO

  • Support Date and Time data format
  • Export definition into XSD format

Sample Format

Schema (XML)

<?xml version="1.0"?>
<dxdoc name="order" revision="3" id="8">
    <dxstr name="order number" lenLimit="7"></dxstr>
    <dxint name="qty"></dxint>
    <dxdecimal name="rate" precision="2"></dxdecimal>
    <dxbool name="is member"></dxbool>
    <dxsection name="customer info">
        <dxstr name="name"></dxstr>
        <dxstr name="id" lenLimit="6"></dxstr>
    </dxsection>
    <dxsection name="items" isArray="true">
        <dxstr name="description"></dxstr>
        <dxint name="qty"></dxint>
        <dxdecimal name="unit price" precision="2"></dxdecimal>
    </dxsection>
</dxdoc>

Data (JSON)

{
    "order number": "ODR0001",
    "qty": 10,
    "rate": 12.56,
    "is member": true,
    "customer info":{
        "name":"John",
        "id":"cust01"
    },
    "items":[
        {"description": "Cap Kapak winter oil", "qty": 1, "unit price":3.50},
        {"description": "Lucky coffee powder", "qty": 3, "unit price":0.60},
    ]
}

Example 1

validate data by loading schema definition from XML and input data from JSON string

//define XML schema
defRaw := `
<dxdoc name="invoice" revision="1" id="7">
    <dxstr name="invNo"></dxstr>
    <dxint name="totalQty" isOptional="true"></dxint>
    <dxdecimal name="price" precision="2"></dxdecimal>
</dxdoc>`

//parse XML schema into GO instance
dxdoc, dxErr := document.DecodeDxXML(defRaw)
if dxErr != nil {
    panic(dxErr) //show parsed failed message
}

//define input data (JSON string)
inputJSON := `
{
	"invNo": "abcd",
	"totalQty": 3,
	"price": 12.58
}`

//convert JSON string into GO generic MAP instance
rawInput := make(map[string]interface{})
jsonErr := json.unmarshal([]byte(inputJSON), &rawInput) 

//validate input data
validateErr := dxdoc.ValidateData(rawInput) 

if validateErr != nil {
    log.Println(validateErr.Error()) //show invalid data message
} else {
    log.PrintLn("input data is valid")
}

Example 2

Validate data by direct define definition and input data

//define definition
dxdoc := document.DxDoc{
    Name:     "invoice",
    Revision: 1,
    ID: 7,
    Items: []document.DxItem{
        document.DxStr{Name: "invNo", EnableLenLimit: true, LenLimit: 4},
        document.DxInt{Name: "totalQty", IsOptional: true},
        document.DxDecimal{Name: "price", Precision: 2},
    },
}

//define input data
rawInput := map[string]interface{}{
    "invNo": "abcd",
    "totalQty": 3,
    "price": 12.58,
}

//validate input data
validateErr := dxdoc.ValidateData(rawInput) 

if validateErr != nil {
    log.Println(validateErr.Error()) //show invalid data message
} else {
    log.PrintLn("input data is valid")
}

About

JSON data validation tool based on stripped down XML schema

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages