Skip to content

Commit

Permalink
Merge pull request lestrrat-go#66 from lestrrat-go/topic/gh-issue-65
Browse files Browse the repository at this point in the history
Add options for schema validation
  • Loading branch information
lestrrat authored Aug 28, 2020
2 parents 6483566 + 7e5b184 commit a52d2c7
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: go
go:
- 1.9.x
- 1.10.x
- 1.14.x
- 1.15.x
- tip
sudo: false
addons:
Expand Down
6 changes: 5 additions & 1 deletion clib/clib.go
Original file line number Diff line number Diff line change
Expand Up @@ -2134,7 +2134,7 @@ func XMLSchemaParse(buf []byte) (uintptr, error) {
return uintptr(unsafe.Pointer(s)), nil
}

func XMLSchemaValidateDocument(schema PtrSource, document PtrSource) []error {
func XMLSchemaValidateDocument(schema PtrSource, document PtrSource, options ...int) []error {
sptr, err := validSchemaPtr(schema)
if err != nil {
return []error{err}
Expand All @@ -2156,6 +2156,10 @@ func XMLSchemaValidateDocument(schema PtrSource, document PtrSource) []error {

C.MY_setErrWarnAccumulator(ctx, accum)

for _, option := range options {
C.xmlSchemaSetValidOptions(ctx, C.int(option))
}

if C.xmlSchemaValidateDoc(ctx, dptr) == 0 {
return nil
}
Expand Down
6 changes: 4 additions & 2 deletions xsd/xsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"github.com/pkg/errors"
)

const ValueVCCreate = 1

// Parse is used to parse an XML Schema Document to produce a
// Schema instance. Make sure to call Free() on the instance
// when you are done with it.
Expand Down Expand Up @@ -61,8 +63,8 @@ func (s *Schema) Free() {
// Validate takes in a XML document and validates it against
// the schema. If there are any problems, and error is
// returned.
func (s *Schema) Validate(d types.Document) error {
errs := clib.XMLSchemaValidateDocument(s, d)
func (s *Schema) Validate(d types.Document, options ...int) error {
errs := clib.XMLSchemaValidateDocument(s, d, options...)
if errs == nil {
return nil
}
Expand Down
48 changes: 48 additions & 0 deletions xsd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,51 @@ BrIVC58W3ydbkK+Ri4OKbaRZlYeRA==
}
}()
}

func TestXSDDefaultValue(t *testing.T) {
const schemasrc = `<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="config">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element ref="attribute"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="attribute">
<xs:complexType>
<xs:sequence>
<xs:element name="linguistic">
<xs:complexType>
<xs:attribute name="item" default="US"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>`
const docsrc = `<config>
<attribute>
<linguistic></linguistic>
</attribute>
</config>`

schema, err := xsd.Parse([]byte(schemasrc))
if !assert.NoError(t, err, `xsd.Parse should succeed`) {
return
}
defer schema.Free()

doc, err := libxml2.ParseString(docsrc)
if !assert.NoError(t, err, "parsing XML") {
return
}
defer doc.Free()
if !assert.NoError(t, schema.Validate(doc, xsd.ValueVCCreate), `schema.Validate should succeed`) {
return
}

t.Logf("%s", doc.String())

}

0 comments on commit a52d2c7

Please sign in to comment.