github.com/CycloneDX/sbom-utility@v0.16.0/schema/schema_custom_validation.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 /* 3 * Licensed to the Apache Software Foundation (ASF) under one or more 4 * contributor license agreements. See the NOTICE file distributed with 5 * this work for additional information regarding copyright ownership. 6 * The ASF licenses this file to You under the Apache License, Version 2.0 7 * (the "License"); you may not use this file except in compliance with 8 * the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 package schema 20 21 import ( 22 "encoding/json" 23 "fmt" 24 "os" 25 26 "github.com/CycloneDX/sbom-utility/utils" 27 ) 28 29 // Globals 30 var CustomValidationChecks CustomValidationConfig 31 32 // --------------------------------------------------------------- 33 // Custom Validation 34 // --------------------------------------------------------------- 35 36 func LoadCustomValidationConfig(filename string) (err error) { 37 getLogger().Enter() 38 defer getLogger().Exit() 39 40 cfgFilename, err := utils.FindVerifyConfigFileAbsPath(getLogger(), filename) 41 42 if err != nil { 43 return fmt.Errorf("unable to find custom validation config file: `%s`", filename) 44 } 45 46 // Note we actively supply informative error messages to help user 47 // understand exactly how the load failed 48 getLogger().Infof("Loading custom validation config file: `%s`...", cfgFilename) 49 // #nosec G304 (suppress warning) 50 buffer, err := os.ReadFile(cfgFilename) 51 if err != nil { 52 return fmt.Errorf("unable to `ReadFile`: `%s`", cfgFilename) 53 } 54 55 err = json.Unmarshal(buffer, &CustomValidationChecks) 56 if err != nil { 57 return fmt.Errorf("cannot `Unmarshal`: `%s`", cfgFilename) 58 } 59 60 return 61 } 62 63 // TODO: return copies 64 func (config *CustomValidationConfig) GetCustomValidationConfig() *CustomValidation { 65 return &config.Validation 66 } 67 68 func (config *CustomValidationConfig) GetCustomValidationMetadata() *CustomValidationMetadata { 69 70 if cfg := config.GetCustomValidationConfig(); cfg != nil { 71 return &cfg.Metadata 72 } 73 return nil 74 } 75 76 func (config *CustomValidationConfig) GetCustomValidationMetadataProperties() []CustomValidationProperty { 77 78 if metadata := config.GetCustomValidationMetadata(); metadata != nil { 79 return metadata.Properties 80 } 81 return nil 82 } 83 84 type CustomValidationConfig struct { 85 Validation CustomValidation `json:"validation"` 86 } 87 88 type CustomValidation struct { 89 Metadata CustomValidationMetadata `json:"metadata"` 90 } 91 92 type CustomValidationMetadata struct { 93 Properties []CustomValidationProperty `json:"properties"` 94 //Tools []CustomValidationTool `json:"tools"` 95 } 96 97 // NOTE: Assumes property "key" is the value in the "name" field 98 type CustomValidationProperty struct { 99 CDXProperty 100 Description string `json:"_validate_description"` 101 Key string `json:"_validate_key"` 102 CheckUnique string `json:"_validate_unique"` 103 CheckRegex string `json:"_validate_regex"` 104 } 105 106 // TODO: if we keep using "custom" structs, then this needs to be updated to handle the new Creation Tools object 107 // type CustomValidationTool struct { 108 // CDXLegacyCreationTool 109 // Description string `json:"_validate_description"` 110 // }