Skip to content

Commit

Permalink
feat(oscal): create the multiModelValidate method for use in oscal co…
Browse files Browse the repository at this point in the history
…nstructors and updated all relavant constructors
  • Loading branch information
mike-winberry committed May 13, 2024
1 parent 96c3dfa commit 8a5ecf3
Show file tree
Hide file tree
Showing 8 changed files with 324,926 additions and 253,404 deletions.
7 changes: 6 additions & 1 deletion src/pkg/common/oscal/assessment-results.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ const OSCAL_VERSION = "1.1.2"
func NewAssessmentResults(data []byte) (*oscalTypes_1_1_2.AssessmentResults, error) {
var oscalModels oscalTypes_1_1_2.OscalModels

err := yaml.Unmarshal(data, &oscalModels)
err := multiModelValidate(data)
if err != nil {
return nil, err
}

err = yaml.Unmarshal(data, &oscalModels)
if err != nil {
fmt.Printf("Error marshalling yaml: %s\n", err.Error())
return nil, err
Expand Down
8 changes: 1 addition & 7 deletions src/pkg/common/oscal/catalog.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package oscal

import (
"github.com/defenseunicorns/go-oscal/src/pkg/validation"
oscalTypes_1_1_2 "github.com/defenseunicorns/go-oscal/src/types/oscal-1-1-2"
"github.com/defenseunicorns/lula/src/pkg/message"
"gopkg.in/yaml.v3"
Expand All @@ -11,13 +10,8 @@ import (
func NewCatalog(data []byte) (catalog *oscalTypes_1_1_2.Catalog, err error) {
var oscalModels oscalTypes_1_1_2.OscalModels

// create a new validator
validator, err := validation.NewValidator(data)
if err != nil {
return catalog, err
}
// validate the catalog
err = validator.Validate()
err = multiModelValidate(data)
if err != nil {
return catalog, err
}
Expand Down
8 changes: 7 additions & 1 deletion src/pkg/common/oscal/complete-schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ import (

func NewOscalModel(data []byte) (*oscalTypes_1_1_2.OscalModels, error) {
oscalModel := oscalTypes_1_1_2.OscalModels{}
err := yaml.Unmarshal(data, &oscalModel)

err := multiModelValidate(data)
if err != nil {
return nil, err
}

err = yaml.Unmarshal(data, &oscalModel)
if err != nil {
return nil, err
}
Expand Down
9 changes: 2 additions & 7 deletions src/pkg/common/oscal/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"time"

"github.com/defenseunicorns/go-oscal/src/pkg/uuid"
"github.com/defenseunicorns/go-oscal/src/pkg/validation"
oscalTypes_1_1_2 "github.com/defenseunicorns/go-oscal/src/types/oscal-1-1-2"
"github.com/defenseunicorns/lula/src/pkg/message"

Expand All @@ -30,12 +29,8 @@ type parameter struct {
func NewOscalComponentDefinition(data []byte) (componentDefinition *oscalTypes_1_1_2.ComponentDefinition, err error) {
var oscalModels oscalTypes_1_1_2.OscalModels

validator, err := validation.NewValidator(data)
if err != nil {
return componentDefinition, err
}

err = validator.Validate()
// validate the data
err = multiModelValidate(data)
if err != nil {
return componentDefinition, err
}
Expand Down
34 changes: 34 additions & 0 deletions src/pkg/common/oscal/multi-validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package oscal

import (
"errors"

"github.com/defenseunicorns/go-oscal/src/pkg/utils"
"github.com/defenseunicorns/go-oscal/src/pkg/validation"
)

func multiModelValidate(data []byte) (err error) {
jsonMap, err := utils.CoerceToJsonMap(data)
if err != nil {
return err
}

if len(jsonMap) == 0 {
return errors.New("no models found")
}

for key, value := range jsonMap {
jsonModel := make(map[string]interface{})
jsonModel[key] = value
validator, err := validation.NewValidator(jsonModel)
if err != nil {
return err
}

err = validator.Validate()
if err != nil {
return err
}
}
return nil
}
20 changes: 20 additions & 0 deletions src/pkg/common/oscal/multi-validate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package oscal

import (
"os"
"testing"
)

func TestMultiValidate(t *testing.T) {
multiModelData, err := os.ReadFile("../../../test/unit/common/oscal/multi-model.yaml")
if err != nil {
t.Fatalf("os.ReadFile failed: %v", err)
}
t.Run("Test Multi Validate", func(t *testing.T) {
data := multiModelData
err := multiModelValidate(data)
if err != nil {
t.Fatalf("multiModelValidate failed: %v", err)
}
})
}
Loading

0 comments on commit 8a5ecf3

Please sign in to comment.