Git Product home page Git Product logo

gxschema's Introduction

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")
}

gxschema's People

Contributors

guinso avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.