Skip to content

Commit

Permalink
Merge pull request #20 from web-of-things-open-source/chore/replace-g…
Browse files Browse the repository at this point in the history
…o-jsonschema

chore: replace json schema validation library
  • Loading branch information
hadjian authored Nov 28, 2023
2 parents 4fd9a87 + a4629b4 commit 65f3d2c
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 74 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/buger/jsonparser v1.1.1
github.com/kennygrant/sanitize v1.2.4
github.com/santhosh-tekuri/jsonschema/v5 v5.3.0
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.17.0
github.com/stretchr/testify v1.8.4
github.com/web-of-things-open-source/go-jsonschema v0.0.0-20231123170049-1c3cab186d29
)

require (
Expand Down
32 changes: 2 additions & 30 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ github.com/sagikazarmark/locafero v0.3.0 h1:zT7VEGWC2DTflmccN/5T1etyKvxSxpHsjb9c
github.com/sagikazarmark/locafero v0.3.0/go.mod h1:w+v7UsPNFwzF1cHuOajOOzoq4U7v/ig1mpRjqV+Bu1U=
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
github.com/santhosh-tekuri/jsonschema/v5 v5.3.0 h1:uIkTLo0AGRc8l7h5l9r+GcYi9qfVPt6lD4/bhmzfiKo=
github.com/santhosh-tekuri/jsonschema/v5 v5.3.0/go.mod h1:FKdcjfQW6rpZSnxxUvEA5H/cDPdvJ/SZJQLWWXWGrZ0=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spf13/afero v1.10.0 h1:EaGW2JJh15aKOejeuJ+wpFSHnbd7GE6Wvp3TsNhb6LY=
Expand All @@ -190,19 +192,10 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/web-of-things-open-source/go-jsonschema v0.0.0-20231123170049-1c3cab186d29 h1:jIY9WgwunMAiNCzIDPLD6vkUKNqFYT7u70Ep26Xo4CA=
github.com/web-of-things-open-source/go-jsonschema v0.0.0-20231123170049-1c3cab186d29/go.mod h1:qjqU56vW5YwhHIY7u48KloTM0UnzBQKsCxpTbXgvYjs=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
Expand All @@ -219,9 +212,7 @@ golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down Expand Up @@ -257,8 +248,6 @@ golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -291,9 +280,6 @@ golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg=
golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand All @@ -315,8 +301,6 @@ golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down Expand Up @@ -352,18 +336,10 @@ golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.14.0/go.mod h1:TySc+nGkYR6qt8km8wUhuFRTVSMIX3XPR58y2lC8vww=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand All @@ -372,8 +348,6 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down Expand Up @@ -426,8 +400,6 @@ golang.org/x/tools v0.0.0-20201208233053-a543418bbed2/go.mod h1:emZCQorbCU4vsT4f
golang.org/x/tools v0.0.0-20210105154028-b0ab187a4818/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20210108195828-e2f9c7f1fc8e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
58 changes: 32 additions & 26 deletions internal/commands/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,68 @@ import (
_ "embed"
"encoding/json"
"log/slog"
"strings"

"github.com/kennygrant/sanitize"
"github.com/web-of-things-open-source/go-jsonschema"
"github.com/santhosh-tekuri/jsonschema/v5"
"github.com/web-of-things-open-source/tm-catalog-cli/internal/model"
)

//go:embed tm-json-schema-validation.json
var tmValidationSchema []byte
var tmValidationSchema string

//go:embed modbus.schema.json
var modbusValidationSchema []byte
var modbusValidationSchema string

//go:embed tmc-mandatory.schema.json
var tmcMandatorySchema []byte
var tmcMandatorySchema string

var tmcMandatoryValidator *jsonschema.Schema
var tmValidator *jsonschema.Schema
var modbusValidator *jsonschema.Schema

const (
tmcMandatorySchemaUrl = "tmc-mandatory.schema.json"
tmSchemaUrl = "https://raw.githubusercontent.com/w3c/wot-thing-description/main/validation/tm-json-schema-validation.json"
modbusSchemaUrl = "modbus.schema.json"
)

func init() {
var err error
tmcMandatoryValidator, err = jsonschema.New(tmcMandatorySchema)
if err != nil {
panic(err)
}
tmcMandatoryValidator = jsonschema.MustCompileString(tmcMandatorySchemaUrl, tmcMandatorySchema)
tmValidator = jsonschema.MustCompileString(tmSchemaUrl, tmValidationSchema)

tmValidator, err = jsonschema.New(tmValidationSchema)
if err != nil {
panic(err)
}
modbusValidator, err = jsonschema.New(modbusValidationSchema)
modbusCompiler := jsonschema.NewCompiler()
err := modbusCompiler.AddResource(tmSchemaUrl, strings.NewReader(tmValidationSchema))
if err != nil {
panic(err)
}
err = modbusValidator.AddSchemaString(string(tmValidationSchema))
err = modbusCompiler.AddResource(modbusSchemaUrl, strings.NewReader(modbusValidationSchema))
if err != nil {
panic(err)
}
modbusValidator = modbusCompiler.MustCompile(modbusSchemaUrl)

}

func ValidateAsTM(raw []byte) error {
_, err := tmValidator.Validate(raw)
return err
func ValidateAsTM(_ []byte, parsed any) error {
return tmValidator.Validate(parsed)
}

// ValidateAsModbus validates a file against modbus protocol binding json schema, but only if it determines that
// the file purports to describe a modbus device.
// Returns a flag indicating whether validate has been attempted and an error if it was not successful
func ValidateAsModbus(raw []byte) (bool, error) {
func ValidateAsModbus(raw []byte, parsed any) (bool, error) {
if shouldTryModbus(raw) {
_, err := modbusValidator.Validate(raw)
return true, err
return true, modbusValidator.Validate(parsed)
}
return false, nil
}
func shouldTryModbus(raw []byte) bool {
return bytes.Index(raw, []byte("\"modbus:")) != -1
}

func ValidateAsTmcImportable(raw []byte) (*model.ThingModel, error) {
_, err := tmcMandatoryValidator.Validate(raw)
func ValidateAsTmcImportable(raw []byte, parsed any) (*model.ThingModel, error) {
err := tmcMandatoryValidator.Validate(parsed)
if err != nil {
return nil, err
}
Expand All @@ -86,19 +86,25 @@ func ValidateAsTmcImportable(raw []byte) (*model.ThingModel, error) {
func ValidateThingModel(raw []byte) (*model.ThingModel, error) {
log := slog.Default()

tm, err := ValidateAsTmcImportable(raw)
var parsed any
err := json.Unmarshal(raw, &parsed)
if err != nil {
return nil, err
}

tm, err := ValidateAsTmcImportable(raw, parsed)
if err != nil {
return nil, err
}
log.Info("required Thing Model metadata is present")

err = ValidateAsTM(raw)
err = ValidateAsTM(raw, parsed)
if err != nil {
return tm, err
}
log.Info("passed validation against JSON schema for Thing Models")

validated, err := ValidateAsModbus(raw)
validated, err := ValidateAsModbus(raw, parsed)
if validated {
if err != nil {
return tm, err
Expand Down
56 changes: 39 additions & 17 deletions internal/commands/validate/validate_test.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,60 @@
package validate

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
"github.com/web-of-things-open-source/tm-catalog-cli/internal"
)

func parseJsonFile(name string) ([]byte, any, error) {
_, raw, err := internal.ReadRequiredFile(name)
if err != nil {
return raw, nil, err
}
var js any
err = json.Unmarshal(raw, &js)
return raw, js, err
}
func parseString(content string) ([]byte, any, error) {
raw := []byte(content)
var js any
err := json.Unmarshal(raw, &js)
return raw, js, err
}
func TestValidateAsTM(t *testing.T) {
_, raw, err := internal.ReadRequiredFile("../../../test/data/validate/omnilamp.json")
raw, parsed, err := parseJsonFile("../../../test/data/validate/omnilamp.json")
assert.NoError(t, err)
err = ValidateAsTM(raw)
err = ValidateAsTM(raw, parsed)
assert.NoError(t, err)

_, raw, err = internal.ReadRequiredFile("../../../test/data/validate/omnilamp-broken.json")
raw, parsed, err = parseJsonFile("../../../test/data/validate/omnilamp-broken.json")
assert.NoError(t, err)
err = ValidateAsTM(raw)
err = ValidateAsTM(raw, parsed)
assert.Error(t, err)
assert.Contains(t, err.Error(), "$.properties.status.readOnly")
assert.Contains(t, err.Error(), "/properties/status/readOnly")

}
func TestValidateAsModbus(t *testing.T) {
_, raw, err := internal.ReadRequiredFile("../../../test/data/validate/omnilamp.json")
raw, parsed, err := parseJsonFile("../../../test/data/validate/omnilamp.json")
assert.NoError(t, err)
v, err := ValidateAsModbus(raw)
v, err := ValidateAsModbus(raw, parsed)
assert.False(t, v)
assert.NoError(t, err)

_, raw, err = internal.ReadRequiredFile("../../../test/data/validate/modbus-senseall.json")
raw, parsed, err = parseJsonFile("../../../test/data/validate/modbus-senseall.json")
assert.NoError(t, err)
v, err = ValidateAsModbus(raw)
v, err = ValidateAsModbus(raw, parsed)
assert.True(t, v)
assert.NoError(t, err)

_, raw, err = internal.ReadRequiredFile("../../../test/data/validate/modbus-senseall-broken.json")
raw, parsed, err = parseJsonFile("../../../test/data/validate/modbus-senseall-broken.json")
assert.NoError(t, err)
v, err = ValidateAsModbus(raw)
v, err = ValidateAsModbus(raw, parsed)
assert.True(t, v)
assert.Error(t, err)
assert.Contains(t, err.Error(), "$.properties.SERIAL_NUMBER.forms.0.modbus:zeroBasedAddressing")
assert.Contains(t, err.Error(), "/properties/SERIAL_NUMBER/forms/0/modbus:zeroBasedAddressing")

}

Expand All @@ -55,7 +71,9 @@ func TestParseRequiredMetadata(t *testing.T) {
"schema:name": "omnicorp R&D/research"
}
}`
tm, err := ValidateAsTmcImportable([]byte(tmFile))
raw, parsed, err := parseString(tmFile)
assert.NoError(t, err)
tm, err := ValidateAsTmcImportable(raw, parsed)
assert.NoError(t, err)
assert.Equal(t, "omnicorp-GmbH-Co-KG", tm.Manufacturer.Name)
assert.Equal(t, "omnicorp-R-D-research", tm.Author.Name)
Expand All @@ -73,9 +91,11 @@ func TestParseRequiredMetadata(t *testing.T) {
"schema:name": " .-"
}
}`
tm, err = ValidateAsTmcImportable([]byte(tmFile))
raw, parsed, err = parseString(tmFile)
assert.NoError(t, err)
_, err = ValidateAsTmcImportable(raw, parsed)
assert.Error(t, err)
assert.ErrorContains(t, err, "$.schema:author.schema:name")
assert.ErrorContains(t, err, "/schema:author/schema:name")

tmFile = `{
"@context": [{
Expand All @@ -86,7 +106,9 @@ func TestParseRequiredMetadata(t *testing.T) {
"schema:name": "omnicorp"
}
}`
_, err = ValidateAsTmcImportable([]byte(tmFile))
raw, parsed, err = parseString(tmFile)
assert.NoError(t, err)
_, err = ValidateAsTmcImportable(raw, parsed)
assert.Error(t, err)
assert.ErrorContains(t, err, "schema:manufacturer at \"$\"")
assert.ErrorContains(t, err, "missing properties: 'schema:manufacturer'")
}

0 comments on commit 65f3d2c

Please sign in to comment.