From 6babea0e69c1da5b9877db2d4bf2aa6a122952a4 Mon Sep 17 00:00:00 2001 From: Stas Dm Date: Tue, 6 Aug 2024 15:50:31 +0200 Subject: [PATCH] fix: lint --- doc/ld/validator/testdata/extended_model.json | 21 ++++++ doc/ld/validator/types.go | 12 +++ doc/ld/validator/validate.go | 9 +-- doc/ld/validator/validate_test.go | 75 ++----------------- 4 files changed, 42 insertions(+), 75 deletions(-) create mode 100644 doc/ld/validator/testdata/extended_model.json create mode 100644 doc/ld/validator/types.go diff --git a/doc/ld/validator/testdata/extended_model.json b/doc/ld/validator/testdata/extended_model.json new file mode 100644 index 0000000..2ff44d7 --- /dev/null +++ b/doc/ld/validator/testdata/extended_model.json @@ -0,0 +1,21 @@ +{ + "@context": [ + "https://www.w3.org/2018/credentials/v1", + "%s" + ], + "id": "http://example.com/credentials/4643", + "type": [ + "VerifiableCredential", + "CustomExt12" + ], + "issuer": "https://example.com/issuers/14", + "issuanceDate": "2018-02-24T05:28:04Z", + "referenceNumber": 83294847, + "credentialSubject": [ + { + "id": "did:example:abcdef1234567", + "name": "Jane Doe", + "favoriteFood": "Papaya" + } + ] +} \ No newline at end of file diff --git a/doc/ld/validator/types.go b/doc/ld/validator/types.go new file mode 100644 index 0000000..7e51467 --- /dev/null +++ b/doc/ld/validator/types.go @@ -0,0 +1,12 @@ +/* +Copyright SecureKey Technologies Inc. All Rights Reserved. +SPDX-License-Identifier: Apache-2.0 +*/ + +package validator + +// Diff represents the difference between two objects. +type Diff struct { + OriginalValue interface{} + CompactedValue interface{} +} diff --git a/doc/ld/validator/validate.go b/doc/ld/validator/validate.go index 9e815c8..27e4ef7 100644 --- a/doc/ld/validator/validate.go +++ b/doc/ld/validator/validate.go @@ -36,6 +36,7 @@ func WithDocumentLoader(jsonldDocumentLoader ld.DocumentLoader) ValidateOpts { } } +// WithJSONLDIncludeDetailedStructureDiffOnError option is for including detailed structure diff in error message. func WithJSONLDIncludeDetailedStructureDiffOnError() ValidateOpts { return func(opts *validateOpts) { opts.jsonldIncludeDetailedStructureDiffOnError = true @@ -105,7 +106,7 @@ func ValidateJSONLDMap(docMap map[string]interface{}, options ...ValidateOpts) e errText := "JSON-LD doc has different structure after compaction" if opts.jsonldIncludeDetailedStructureDiffOnError { - diff, _ := json2.Marshal(mapDiff) + diff, _ := json2.Marshal(mapDiff) // nolint:errcheck errText = fmt.Sprintf("%s. Details: %v", errText, string(diff)) } @@ -153,11 +154,7 @@ func validateContextURIPosition(contextURIPositions []string, docMap map[string] return nil } -type Diff struct { - OriginalValue interface{} - CompactedValue interface{} -} - +// nolint: gocyclo func mapsHaveSameStructure( originalMap, compactedMap map[string]interface{}, diff --git a/doc/ld/validator/validate_test.go b/doc/ld/validator/validate_test.go index 8578ef8..1c905aa 100644 --- a/doc/ld/validator/validate_test.go +++ b/doc/ld/validator/validate_test.go @@ -30,6 +30,9 @@ var ( //go:embed testdata/context/wallet_v1.jsonld walletV1Context []byte + + //go:embed testdata/extended_model.json + extendedModel string ) func Test_ValidateJSONLD(t *testing.T) { @@ -187,29 +190,7 @@ func Test_ValidateJSONLDWithExtraUndefinedSubjectFields(t *testing.T) { t.Run("Extended basic VC model, credentialSubject is defined as object - undefined fields present", func(t *testing.T) { // Use a different VC to verify the case when credentialSubject is an array. - vcJSONTemplate := ` -{ - "@context": [ - "https://www.w3.org/2018/credentials/v1", - "%s" - ], - "id": "http://example.com/credentials/4643", - "type": [ - "VerifiableCredential", - "CustomExt12" - ], - "issuer": "https://example.com/issuers/14", - "issuanceDate": "2018-02-24T05:28:04Z", - "referenceNumber": 83294847, - "credentialSubject": [ - { - "id": "did:example:abcdef1234567", - "name": "Jane Doe", - "favoriteFood": "Papaya" - } - ] -} -` + vcJSONTemplate := extendedModel vcJSON := fmt.Sprintf(vcJSONTemplate, contextURL) @@ -221,29 +202,7 @@ func Test_ValidateJSONLDWithExtraUndefinedSubjectFields(t *testing.T) { t.Run("Extended basic VC model, credentialSubject is defined as object - undefined fields present and details", func(t *testing.T) { // Use a different VC to verify the case when credentialSubject is an array. - vcJSONTemplate := ` -{ - "@context": [ - "https://www.w3.org/2018/credentials/v1", - "%s" - ], - "id": "http://example.com/credentials/4643", - "type": [ - "VerifiableCredential", - "CustomExt12" - ], - "issuer": "https://example.com/issuers/14", - "issuanceDate": "2018-02-24T05:28:04Z", - "referenceNumber": 83294847, - "credentialSubject": [ - { - "id": "did:example:abcdef1234567", - "name": "Jane Doe", - "favoriteFood": "Papaya" - } - ] -} -` + vcJSONTemplate := extendedModel vcJSON := fmt.Sprintf(vcJSONTemplate, contextURL) @@ -254,29 +213,7 @@ func Test_ValidateJSONLDWithExtraUndefinedSubjectFields(t *testing.T) { t.Run("Extended basic VC model, credentialSubject is defined as array - undefined fields present", func(t *testing.T) { // Use a different VC to verify the case when credentialSubject is an array. - vcJSONTemplate := ` -{ - "@context": [ - "https://www.w3.org/2018/credentials/v1", - "%s" - ], - "id": "http://example.com/credentials/4643", - "type": [ - "VerifiableCredential", - "CustomExt12" - ], - "issuer": "https://example.com/issuers/14", - "issuanceDate": "2018-02-24T05:28:04Z", - "referenceNumber": 83294847, - "credentialSubject": [ - { - "id": "did:example:abcdef1234567", - "name": "Jane Doe", - "favoriteFood": "Papaya" - } - ] -} -` + vcJSONTemplate := extendedModel vcJSON := fmt.Sprintf(vcJSONTemplate, contextURL)