error.h
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 
8 #ifndef PXR_USD_VALIDATION_USD_VALIDATION_ERROR_H
9 #define PXR_USD_VALIDATION_USD_VALIDATION_ERROR_H
10 
11 #include "pxr/pxr.h"
12 #include "pxr/usd/sdf/path.h"
13 #include "pxr/usd/usd/property.h"
14 #include "pxr/usd/usd/stage.h"
15 #include "pxr/usdValidation/usdValidation/api.h"
16 
17 #include <string>
18 
19 PXR_NAMESPACE_OPEN_SCOPE
20 
21 class UsdPrim;
24 
37 enum class UsdValidationErrorType
38 {
39  None = 0,
40  Error,
41  Warn,
42  Info
43 };
44 
55 {
56 public:
57  UsdValidationErrorSite() = default;
58 
66  USDVALIDATION_API
67  UsdValidationErrorSite(const SdfLayerHandle &layer,
68  const SdfPath &objectPath);
69 
80  USDVALIDATION_API
81  UsdValidationErrorSite(const UsdStagePtr &usdStage,
82  const SdfPath &objectPath,
83  const SdfLayerHandle &layer = SdfLayerHandle());
84 
87  bool IsValid() const
88  {
89  return IsValidSpecInLayer() || IsPrim() || IsProperty();
90  }
91 
94  bool IsValidSpecInLayer() const
95  {
96  if (!_layer || _objectPath.IsEmpty()) {
97  return false;
98  }
99  return _layer->HasSpec(_objectPath);
100  }
101 
104  bool IsPrim() const
105  {
106  return GetPrim().IsValid();
107  }
108 
111  bool IsProperty() const
112  {
113  return GetProperty().IsValid();
114  }
115 
122  const SdfPropertySpecHandle GetPropertySpec() const
123  {
124  if (!_layer) {
125  return SdfPropertySpecHandle();
126  }
127  return _layer->GetPropertyAtPath(_objectPath);
128  }
129 
135  const SdfPrimSpecHandle GetPrimSpec() const
136  {
137  if (!_layer) {
138  return SdfPrimSpecHandle();
139  }
140  return _layer->GetPrimAtPath(_objectPath);
141  }
142 
145  const SdfLayerHandle &GetLayer() const
146  {
147  return _layer;
148  }
149 
152  const UsdStagePtr &GetStage() const
153  {
154  return _usdStage;
155  }
156 
160  UsdPrim GetPrim() const
161  {
162  if (_usdStage) {
163  return _usdStage->GetPrimAtPath(_objectPath);
164  }
165  return UsdPrim();
166  }
167 
172  {
173  if (_usdStage) {
174  return _usdStage->GetPropertyAtPath(_objectPath);
175  }
176  return UsdProperty();
177  }
178 
181  bool operator==(const UsdValidationErrorSite &other) const
182  {
183  return (_layer == other._layer) && (_usdStage == other._usdStage)
184  && (_objectPath == other._objectPath);
185  }
186 
189  bool operator!=(const UsdValidationErrorSite &other) const
190  {
191  return !(*this == other);
192  }
193 
194 private:
195  UsdStagePtr _usdStage;
196  SdfLayerHandle _layer;
197  SdfPath _objectPath;
198 
199 }; // UsdValidationErrorSite
200 
201 using UsdValidationErrorSites = std::vector<UsdValidationErrorSite>;
202 
240 {
241 public:
243  USDVALIDATION_API
245 
249  USDVALIDATION_API
250  UsdValidationError(const TfToken &name,
251  const UsdValidationErrorType &errorType,
252  const UsdValidationErrorSites &errorSites,
253  const std::string &errorMsg,
254  const VtValue &data = VtValue());
255 
256  bool operator==(const UsdValidationError &other) const
257  {
258  return (_name == other._name) && (_errorType == other._errorType)
259  && (_errorSites == other._errorSites)
260  && (_errorMsg == other._errorMsg)
261  && (_validator == other._validator);
262  }
263 
264  bool operator!=(const UsdValidationError &other) const
265  {
266  return !(*this == other);
267  }
268 
270  const TfToken &GetName() const &
271  {
272  return _name;
273  }
274 
277  {
278  return std::move(_name);
279  }
280 
284  {
285  return _errorType;
286  }
287 
290  const UsdValidationErrorSites &GetSites() const &
291  {
292  return _errorSites;
293  }
294 
297  UsdValidationErrorSites GetSites() &&
298  {
299  return std::move(_errorSites);
300  }
301 
308  {
309  return _validator;
310  }
311 
313  const std::string &GetMessage() const
314  {
315  return _errorMsg;
316  }
317 
323  const VtValue &GetData() const
324  {
325  return _data;
326  }
327 
343  USDVALIDATION_API
344  TfToken GetIdentifier() const;
345 
347  USDVALIDATION_API
348  std::string GetErrorAsString() const;
349 
352  bool HasNoError() const
353  {
354  return _errorType == UsdValidationErrorType::None;
355  }
356 
359  USDVALIDATION_API
360  const std::vector<const UsdValidationFixer *> GetFixers() const;
361 
365  USDVALIDATION_API
366  const UsdValidationFixer *GetFixerByName(const TfToken &name) const;
367 
371  USDVALIDATION_API
372  const std::vector<const UsdValidationFixer *>
373  GetFixersByErrorName() const;
374 
378  USDVALIDATION_API
380  const TfToken &name) const;
381 
388  USDVALIDATION_API
389  const std::vector<const UsdValidationFixer *> GetFixersByKeywords(
390  const TfTokenVector &keywords) const;
391 
392 
393 private:
394  // UsdValidationValidatorError holds a pointer to the UsdValidationValidator
395  // that generated it, so we need to provide friend access to allow the
396  // necessary mutation.
397  friend class UsdValidationValidator;
398 
399  // Used by UsdValidationValidator::Validate methods to embed itself to the
400  // reported errors.
401  void _SetValidator(const UsdValidationValidator *validator);
402 
403  // _validator is set when ValidationError is generated via a
404  // UsdValidationValidator::Validate() call.
405  const UsdValidationValidator *_validator;
406 
407  // These data members should not be modified other than during
408  // initialization by the validate task functions.
409  TfToken _name;
410  UsdValidationErrorType _errorType;
411  UsdValidationErrorSites _errorSites;
412  std::string _errorMsg;
413  VtValue _data;
414 
415 }; // UsdValidationError
416 
417 PXR_NAMESPACE_CLOSE_SCOPE
418 
419 #endif // PXR_USD_VALIDATION_USD_VALIDATION_ERROR_H
USDVALIDATION_API const UsdValidationFixer * GetFixerByName(const TfToken &name) const
Return an immutable fixer given its name if it exists, else return nullptr.
USDVALIDATION_API const std::vector< const UsdValidationFixer * > GetFixersByKeywords(const TfTokenVector &keywords) const
Return a vector of immutable fixers catering to any of the given keywords.
const std::string & GetMessage() const
Returns the message associated with this UsdValidationError.
Definition: error.h:313
UsdPrim GetPrim() const
Returns UsdPrim associated with this UsdValidationErrorSite, that is when UsdStage is present and obj...
Definition: error.h:160
bool HasNoError() const
Returns true if UsdValidationErrorType is UsdValidationErrorType::None, false otherwise.
Definition: error.h:352
bool IsValidSpecInLayer() const
Returns true if the objectPath and layer represent a spec in the layer; false otherwise.
Definition: error.h:94
const SdfLayerHandle & GetLayer() const
Returns the SdfLayerHandle associated with this UsdValidationValidatorErrorSite.
Definition: error.h:145
UsdValidationErrorType reflects severity of a validation error, which can then be reported appropriat...
Definition: error.h:23
bool IsValid() const
Returns true if UsdValidationErrorSite instance can either point to a prim or property spec in a laye...
Definition: error.h:87
bool operator==(const UsdValidationErrorSite &other) const
Returns true if other UsdValidationErrorSite has same valued members as this UsdValidationErrorSite,...
Definition: error.h:181
const SdfPrimSpecHandle GetPrimSpec() const
Returns the SdfPrimSpecHandle associated with this ValidationErrorSite's layer and objectPath.
Definition: error.h:135
A UsdValidationFixer represents a fix that can be applied to fix a specific validation error.
Definition: fixer.h:62
UsdValidationValidator is a class describing a single test.
Definition: validator.h:137
const TfToken & GetName() const &
Returns the name token of the UsdValidationError.
Definition: error.h:270
bool IsProperty() const
Returns true if UsdValidationErrorSite represents a property on a stage, false otherwise.
Definition: error.h:111
UsdValidationError is an entity returned by a validation task, which is associated with a UsdValidati...
Definition: error.h:239
TfToken GetName() &&
Returns the name token of the UsdValidationError by-value.
Definition: error.h:276
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:80
USDVALIDATION_API TfToken GetIdentifier() const
An identifier for the error constructed from the validator name this error was generated from and its...
const VtValue & GetData() const
Returns the data associated with this UsdValidationError.
Definition: error.h:323
const UsdStagePtr & GetStage() const
Returns the UsdStage associated with this UsdValidationErrorSite; nullptr othewrise.
Definition: error.h:152
USDVALIDATION_API UsdValidationError()
A default constructed UsdValidationError signifies no error.
bool IsPrim() const
Returns true if UsdValidationErrorSite represents a prim on a stage, false otherwise.
Definition: error.h:104
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a "Prim" as ...
Definition: prim.h:116
const SdfPropertySpecHandle GetPropertySpec() const
Returns the SdfPropertySpecHandle associated with this ValidationErrorSite's layer and objectPath.
Definition: error.h:122
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:489
const UsdValidationErrorSites & GetSites() const &
Returns the UsdValidationErrorSite associated with this UsdValidationError.
Definition: error.h:290
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:280
UsdValidationErrorType GetType() const
Returns the UsdValidationErrorType associated with this UsdValidationError.
Definition: error.h:283
UsdProperty GetProperty() const
Returns UsdProperty associated with this UsdValidationErrorSite, that is when UsdStage is present and...
Definition: error.h:171
Base class for UsdAttribute and UsdRelationship scenegraph objects.
Definition: property.h:38
USDVALIDATION_API const UsdValidationFixer * GetFixerByNameAndErrorName(const TfToken &name) const
Return an immutable fixer given its name and catering to a specific error name if it exists,...
UsdValidationErrorSite is important information available from a ValidationError, which annotates the...
Definition: error.h:54
bool operator!=(const UsdValidationErrorSite &other) const
Returns false if other UsdValidationErrorSite has same valued members as this UsdValidationErrorSite,...
Definition: error.h:189
USDVALIDATION_API const std::vector< const UsdValidationFixer * > GetFixers() const
Return a vector of fixers associated with this Validator.
bool IsValid() const
Return true if this is a valid object, false otherwise.
Definition: object.h:127
bool IsEmpty() const noexcept
Returns true if this is the empty path (SdfPath::EmptyPath()).
Definition: path.h:405
const UsdValidationValidator * GetValidator() const
Returns the UsdValidationValidator that reported this error.
Definition: error.h:307
USDVALIDATION_API std::string GetErrorAsString() const
Returns UsdValidationErrorType and ErrorMessage concatenated as a string.
UsdValidationErrorSites GetSites() &&
Returns the UsdValidationErrorSite associated with this UsdValidationError by-value.
Definition: error.h:297
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:89
USDVALIDATION_API const std::vector< const UsdValidationFixer * > GetFixersByErrorName() const
Return a vector of immutable fixers catering to a specific errorName.