HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
validator.h
Go to the documentation of this file.
1 //
2 // Copyright 2024 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_USD_VALIDATION_USD_VALIDATION_VALIDATOR_H
8 #define PXR_USD_VALIDATION_USD_VALIDATION_VALIDATOR_H
9 
10 /// \file
11 
12 #include "pxr/pxr.h"
13 #include "pxr/base/gf/interval.h"
14 #include "pxr/base/tf/token.h"
15 #include "pxr/usd/usd/stage.h"
16 #include "pxr/usd/usd/timeCode.h"
20 
21 #include <functional>
22 #include <string>
23 #include <variant>
24 #include <vector>
25 
27 
28 class UsdValidationError;
29 using UsdValidationErrorVector = std::vector<UsdValidationError>;
30 class UsdPrim;
31 
32 /// \class UsdValidationValidatorMetadata
33 ///
34 /// A structure which describes metadata for a UsdValidationValidator.
35 ///
36 /// The metadata values are populated from the plugInfo.json associated with a
37 /// validator's plugin. PlugInfo can provide the following validator metadata:
38 ///
39 /// - name: A required field. This metadatum stores the validator name. For
40 /// validators defined in a plugin, the name must be a fully qualified name
41 /// which includes the pluginName as well, separated by ":". This ensures,
42 /// plugin provided validator names are guaranteed to be unique.
43 /// - pluginPtr: Pointer to the plugin where a plugin based validator is
44 /// defined. nullptr for a non-plugin based validator.
45 /// - keywords: Keywords associated with this validator.
46 /// - doc: Doc string explaining the purpose of the validator.
47 /// - schemaTypes: If the validator is associated with specific schemaTypes.
48 /// - isTimeDependent: If the validator is testing rules which are time
49 /// dependent. Note that only PrimValidators and StageValidators can be time
50 /// dependent, as these are the ones which can evaluate time dependent
51 /// properties. Note that its still recommended to use PrimValidators for
52 /// time dependent properties. Default is false.
53 /// - isSuite: If the validator represents a suite of validators.
54 ///
56 {
57  /// Name of the validator.
58  ///
59  /// For plugin provided validators, this is prefixed with the pluginName,
60  /// like "pluginName:testName" in order to uniquely identify these plugin
61  /// provided validators.
62  ///
63  /// This is a mandatory field for a ValidatorMetadata.
65 
66  /// Pointer to the plugin to which a plugin based validator belongs.
67  ///
68  /// For non-plugin based validator, pluginPtr is nullptr.
69  PlugPluginPtr pluginPtr;
70 
71  /// list of keywords extracted for this test from the plugInfo.json
73 
74  /// doc string extracted from plugInfo.json
75  /// This is a mandatory field for a ValidatorMetadata.
76  std::string doc;
77 
78  /// list of schemaTypes names this test applies to, extracted from
79  /// plugInfo.json
81 
82  /// whether this test is time dependent or not.
83  /// Only PrimValidators and StageValidators can be time dependent.
84  /// By default this is false.
86 
87  /// whether this test represents a test suite or not
88  bool isSuite;
89 }; // UsdValidationValidatorMetadata
90 
92  = std::vector<UsdValidationValidatorMetadata>;
93 
94 /// \defgroup UsdValidateTaskFn_group Validating Task Functions
95 ///
96 /// UsdValidateLayerTaskFn, UsdValidateStageTaskFn and UsdValidatePrimTaskFn
97 /// represent the callbacks associated with each validator's validation logic.
98 ///
99 /// Clients must provide implementation for these in their respective plugin
100 /// registration code.
101 /// @{
102 
103 /// UsdValidateLayerTaskFn: Validation logic operating on a given SdfLayerHandle
105  const SdfLayerHandle &)>;
106 /// UsdValidateStageTaskFn: Validation logic operating on a given UsdStage
108  const UsdStagePtr &, const UsdValidationTimeRange)>;
109 /// UsdValidatePrimTaskFn: Validation logic operating on a given UsdPrim
110 using UsdValidatePrimTaskFn = std::function<UsdValidationErrorVector(
111  const UsdPrim &, const UsdValidationTimeRange)>;
112 
113 /// @}
114 
115 /// \class UsdValidationValidator
116 ///
117 /// UsdValidationValidator is a class describing a single test.
118 ///
119 /// An instance of UsdValidationValidator is created when plugins are loaded and
120 /// tests are registered and cached in the UsdValidationRegistry.
121 /// UsdValidationValidator can consist of any one of the 3 testing tasks:
122 /// LayerTestingTask, StageTestingTask or PrimTestingTask, which correspond to
123 /// testing the given SdfLayer, an entire UsdStage or a UsdPrim respectively.
124 ///
125 /// UsdValidationValidator instances are immutable and non-copyable. Note that
126 /// all validators which are registered with the UsdValidationRegistry are
127 /// immortal.
128 ///
129 /// A UsdValidationValidator can optionally provide a list of fixers, which
130 /// clients can use to fix specific errors reported by the validator. Note that
131 /// these fixers must have unique names within the context of a validator. If
132 /// not unique, a coding error is issued during the construction of the
133 /// UsdValidationValidator instance.
134 ///
135 /// \sa UsdValidationRegistry
136 /// \sa UsdValidationFixer
138 {
139 public:
140  /// Instantiate a UsdValidationValidator which has no validation logic
141  /// implementation.
142  ///
143  /// This is primarily used by UsdValidationValidatorSuite.
145  explicit UsdValidationValidator(
146  const UsdValidationValidatorMetadata &metadata);
147 
148  UsdValidationValidator(const UsdValidationValidator &other) = delete;
149  UsdValidationValidator &operator=(const UsdValidationValidator &) = delete;
150 
151  UsdValidationValidator(UsdValidationValidator &&other) noexcept = default;
152  UsdValidationValidator &operator=(UsdValidationValidator &&) noexcept
153  = default;
154 
155  /// Instantiate a UsdValidationValidator which has its validation logic
156  /// implemented by a UsdValidateLayerTaskFn.
159  const UsdValidateLayerTaskFn &validateLayerTaskFn,
160  std::vector<UsdValidationFixer> fixers = {});
161 
162  /// Instantiate a UsdValidationValidator which has its validation logic
163  /// implemented by a UsdValidateStageTaskFn.
166  const UsdValidateStageTaskFn &validateStageTaskFn,
167  std::vector<UsdValidationFixer> fixers = {});
168 
169  /// Instantiate a UsdValidationValidator which has its validation logic
170  /// implemented by a UsdValidatePrimTaskFn.
173  const UsdValidatePrimTaskFn &validatePrimTaskFn,
174  std::vector<UsdValidationFixer> fixers = {});
175 
176  /// Return metadata associated with this Validator.
178  {
179  return _metadata;
180  }
181 
182  /// Return metadata associated with this validator by-value.
184  {
185  return std::move(_metadata);
186  }
187 
188  /// Return a vector of immutable fixers associated with this Validator.
189  ///
191  const std::vector<const UsdValidationFixer *> GetFixers() const;
192 
193  /// Return an immutable fixer given its \p name if it exists, else return
194  /// nullptr.
195  ///
197  const UsdValidationFixer *GetFixerByName(const TfToken &name) const;
198 
199  /// Return a vector of immutable fixers catering to a specific
200  /// \p errorName.
201  ///
203  const std::vector<const UsdValidationFixer *> GetFixersByErrorName(
204  const TfToken &errorName) const;
205 
206  /// Return an immutable fixer given its \p name and catering to a specific
207  /// \p errorName if it exists, else return nullptr.
208  ///
211  const TfToken &name, const TfToken &errorName) const;
212 
213  /// Return a vector of immutable fixers catering to any of the given
214  /// \p keywords.
215  ///
216  /// Fixers can be associated with keywords, like show, department, etc.
217  /// which can be used to filter fixers based on the context in which they
218  /// are being queried.
219  ///
220  /// Note that the returned list contains fixers which have any of the
221  /// provided \p keywords, the fixers do not have to have all of the
222  /// keywords to be returned.
224  const std::vector<const UsdValidationFixer *> GetFixersByKeywords(
225  const TfTokenVector &keywords) const;
226 
227  /// Run validation on the given \p layer by executing the contained
228  /// validateTaskFn and returns UsdValidationErrorVector.
229  ///
230  /// If this Validator doesn't provide a UsdValidateLayerTaskFn, then an
231  /// empty vector is returned, which signifies no error.
233  UsdValidationErrorVector Validate(const SdfLayerHandle &layer) const;
234 
235  /// Run validation on the given \p usdStage by executing the contained
236  /// validateTaskFn and returns UsdValidationErrorVector.
237  ///
238  /// \p timeRange is used to evaluate the prims and their properties in
239  /// the stage at a specific time or interval. If no \p timeRange is
240  /// provided, then full time interval is used by validation callback's
241  /// implementation.
242  ///
243  /// If this Validator doesn't provide a UsdValidateStageTaskFn, then an
244  /// empty vector is returned, which signifies no error.
247  const UsdStagePtr &usdStage,
248  const UsdValidationTimeRange &timeRange = {}) const;
249 
250  /// Run validation on the given \p usdPrim by executing the contained
251  /// validateTaskFn and returns UsdValidationErrorVector.
252  ///
253  /// \p timeRange is used to evaluate the prims and their properties in
254  /// the stage at a specific time or interval. If no \p timeRange is
255  /// provided, then full time interval is used by validation callback's
256  /// implementation.
257  ///
258  /// If this Validator doesn't provide a UsdValidatePrimTaskFn, then an
259  /// empty vector is returned, which signifies no error.
262  const UsdPrim &usdPrim,
263  const UsdValidationTimeRange &timeRange = {}) const;
264 
265 private:
266  // To make sure registry can query task types on a validator.
267  // Registry needs access to _GetValidatorPrimTasks, in order to determine if
268  // the contained validators in a suite, which provides schemaTypes metadata
269  // are compliant.
270  friend class UsdValidationRegistry;
271  // In order to distribute validators into different sets based on the type
272  // of validation to be performed, ValidationContext needs to access
273  // _GetValidateLayerTask, _GetValidateStageTask and _GetValidatePrimTask.
274  friend class UsdValidationContext;
275 
279  _validateTaskFn;
280 
281  std::vector<UsdValidationFixer> _fixers;
282 
283  // Return UsdValidateLayerTaskFn if provided by the validator, else a
284  // nullptr.
285  const UsdValidateLayerTaskFn *_GetValidateLayerTask() const;
286 
287  // Return UsdValidateStageTaskFn if provided by the validator, else a
288  // nullptr.
289  const UsdValidateStageTaskFn *_GetValidateStageTask() const;
290 
291  // Return UsdValidatePrimTaskFn if provided by the validator, else a
292  // nullptr.
293  const UsdValidatePrimTaskFn *_GetValidatePrimTask() const;
294 
295  // Helper to validate that all fixers have unique names. Issues a coding
296  // error if not unique.
297  void _ValidateFixerNames() const;
298 
299 }; // UsdValidationValidator
300 
301 /// \class UsdValidationValidatorSuite
302 ///
303 /// UsdValidationValidatorSuite acts like a suite for a collection of tests,
304 /// which clients can use to bundle all tests relevant to test their concepts.
305 ///
306 /// If client failed to provide isSuite metadata for a
307 /// UsdValidationValidatorSuite instance then the validatorSuite will not be
308 /// registered, and client will appropriately be warned.
309 ///
310 /// UsdValidationValidatorSuite instances are immutable and non-copyable. Note
311 /// that all validator suites which are registered with the
312 /// UsdValidationRegistry are immortal.
313 ///
314 /// isTimeDependent metadata is a no-op for a UsdValidationValidatorSuite.
315 ///
316 /// \sa UsdValidationRegistry
318 {
319 public:
320  /// Instantiate UsdValidationValidatorSuite using \p metadata and a vector
321  /// of \p validators.
324  const UsdValidationValidatorMetadata &metadata,
325  const std::vector<const UsdValidationValidator *> &validators);
326 
327  UsdValidationValidatorSuite(UsdValidationValidatorSuite &&other) noexcept
328  = default;
329 
330  UsdValidationValidatorSuite &
331  operator=(UsdValidationValidatorSuite &&) noexcept
332  = default;
333 
334  /// Returns a vector of const UsdValidationValidator pointers, which make
335  /// this UsdValidationValidatorSuite. Note that the validators are
336  /// guaranteed to be valid, since their lifetime is managed by the
337  /// UsdValidationRegistry, which has a higher scope than individual
338  /// validators.
339  const std::vector<const UsdValidationValidator *> &
341  {
342  return _containedValidators;
343  }
344 
345  /// Returns a vector of const UsdValidationValidator pointers, which make
346  /// this UsdValidationValidatorSuite. Note that the validators are
347  /// guaranteed to be valid, since their lifetime is managed by the
348  /// UsdValidationRegistry, which has a higher scope than individual
349  /// validators.
350  std::vector<const UsdValidationValidator *> GetContainedValidators() &&
351  {
352  return std::move(_containedValidators);
353  }
354 
355  /// Return metadata associated with this validator.
357  {
358  return _metadata;
359  }
360 
361  /// Return metadata associated with this validator.
363  {
364  return std::move(_metadata);
365  }
366 
367 private:
369  std::vector<const UsdValidationValidator *> _containedValidators;
370 }; // UsdValidationValidatorSuite
371 
373 
374 #endif // PXR_USD_VALIDATION_USD_VALIDATION_VALIDATOR_H
USDVALIDATION_API UsdValidationValidatorSuite(const UsdValidationValidatorMetadata &metadata, const std::vector< const UsdValidationValidator * > &validators)
USDVALIDATION_API UsdValidationValidator(const UsdValidationValidatorMetadata &metadata)
std::vector< UsdValidationValidatorMetadata > UsdValidationValidatorMetadataVector
Definition: validator.h:92
const UsdValidationValidatorMetadata & GetMetadata() const &
Return metadata associated with this Validator.
Definition: validator.h:177
USDVALIDATION_API const UsdValidationFixer * GetFixerByName(const TfToken &name) const
TfTokenVector keywords
list of keywords extracted for this test from the plugInfo.json
Definition: validator.h:72
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
std::function< UsdValidationErrorVector(const UsdStagePtr &, const UsdValidationTimeRange)> UsdValidateStageTaskFn
UsdValidateStageTaskFn: Validation logic operating on a given UsdStage.
Definition: validator.h:108
GLenum GLuint GLint GLint layer
Definition: glcorearb.h:1299
#define USDVALIDATION_API
Definition: api.h:25
USDVALIDATION_API UsdValidationErrorVector Validate(const SdfLayerHandle &layer) const
UsdValidationValidatorMetadata GetMetadata()&&
Return metadata associated with this validator.
Definition: validator.h:362
std::function< UsdValidationErrorVector(const SdfLayerHandle &)> UsdValidateLayerTaskFn
UsdValidateLayerTaskFn: Validation logic operating on a given SdfLayerHandle.
Definition: validator.h:105
Definition: token.h:70
USDVALIDATION_API const std::vector< const UsdValidationFixer * > GetFixersByKeywords(const TfTokenVector &keywords) const
std::vector< const UsdValidationValidator * > GetContainedValidators()&&
Definition: validator.h:350
USDVALIDATION_API const std::vector< const UsdValidationFixer * > GetFixersByErrorName(const TfToken &errorName) const
Definition: prim.h:116
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440
UsdValidationValidatorMetadata GetMetadata()&&
Return metadata associated with this validator by-value.
Definition: validator.h:183
GLuint const GLchar * name
Definition: glcorearb.h:786
const std::vector< const UsdValidationValidator * > & GetContainedValidators() const &
Definition: validator.h:340
std::function< UsdValidationErrorVector(const UsdPrim &, const UsdValidationTimeRange)> UsdValidatePrimTaskFn
UsdValidatePrimTaskFn: Validation logic operating on a given UsdPrim.
Definition: validator.h:111
UsdValidationValidatorSuite & operator=(UsdValidationValidatorSuite &&) noexcept=default
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
std::vector< UsdValidationError > UsdValidationErrorVector
Definition: validator.h:29
USDVALIDATION_API const UsdValidationFixer * GetFixerByNameAndErrorName(const TfToken &name, const TfToken &errorName) const
USDVALIDATION_API const std::vector< const UsdValidationFixer * > GetFixers() const
bool isSuite
whether this test represents a test suite or not
Definition: validator.h:88
const UsdValidationValidatorMetadata & GetMetadata() const &
Return metadata associated with this validator.
Definition: validator.h:356
UsdValidationValidator & operator=(const UsdValidationValidator &)=delete