Skip to content

Commit 33d83be

Browse files
committed
Add a content-based spec validator
Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent 778a530 commit 33d83be

1 file changed

Lines changed: 28 additions & 1 deletion

File tree

pkg/cdi/spec.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (s *Spec) write(overwrite bool) error {
119119
producer.WithOutputFormat("json"),
120120
producer.WithOverwrite(overwrite),
121121
producer.WithPermissions(0),
122-
producer.WithValidatorFunction(validateSpec),
122+
producer.WithValidator(getSpecValidator()),
123123
)
124124
}
125125

@@ -219,6 +219,13 @@ func SetSpecValidator(v validator) {
219219
specValidator = v
220220
}
221221

222+
// getSpecValidator returns a reference to the current validator.
223+
func getSpecValidator() validator {
224+
validatorLock.Lock()
225+
defer validatorLock.Unlock()
226+
return specValidator
227+
}
228+
222229
// validateSpec validates the Spec using the external validator.
223230
func validateSpec(raw *cdi.Spec) error {
224231
validatorLock.RLock()
@@ -298,3 +305,23 @@ func GenerateNameForTransientSpec(raw *cdi.Spec, transientID string) (string, er
298305

299306
return GenerateTransientSpecName(vendor, class, transientID), nil
300307
}
308+
309+
type contentSpecValidator string
310+
311+
const (
312+
// SpecContentValidator validates the CDI spec based on the content and
313+
// not the JSON schema.
314+
SpecContentValidator = contentSpecValidator("default")
315+
)
316+
317+
func (v contentSpecValidator) Validate(raw *cdi.Spec) error {
318+
spec := &Spec{
319+
Spec: raw,
320+
}
321+
spec.vendor, spec.class = parser.ParseQualifier(spec.Kind)
322+
_, err := spec.validate()
323+
if err != nil {
324+
return fmt.Errorf("invalid CDI Spec: %w", err)
325+
}
326+
return nil
327+
}

0 commit comments

Comments
 (0)