Loading...
Searching...
No Matches
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
19PXR_NAMESPACE_OPEN_SCOPE
20
21class UsdPrim;
24
38{
39 None = 0,
40 Error,
41 Warn,
42 Info
43};
44
55{
56public:
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
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
194private:
195 UsdStagePtr _usdStage;
196 SdfLayerHandle _layer;
197 SdfPath _objectPath;
198
199}; // UsdValidationErrorSite
200
201using UsdValidationErrorSites = std::vector<UsdValidationErrorSite>;
202
237{
238public:
240 USDVALIDATION_API
242
245 USDVALIDATION_API
247 const UsdValidationErrorType &errorType,
248 const UsdValidationErrorSites &errorSites,
249 const std::string &errorMsg,
250 const VtValue &metadata = VtValue());
251
252 bool operator==(const UsdValidationError &other) const
253 {
254 return (_name == other._name) && (_errorType == other._errorType)
255 && (_errorSites == other._errorSites)
256 && (_errorMsg == other._errorMsg)
257 && (_validator == other._validator);
258 }
259
260 bool operator!=(const UsdValidationError &other) const
261 {
262 return !(*this == other);
263 }
264
266 const TfToken &GetName() const &
267 {
268 return _name;
269 }
270
273 {
274 return std::move(_name);
275 }
276
280 {
281 return _errorType;
282 }
283
286 const UsdValidationErrorSites &GetSites() const &
287 {
288 return _errorSites;
289 }
290
293 UsdValidationErrorSites GetSites() &&
294 {
295 return std::move(_errorSites);
296 }
297
304 {
305 return _validator;
306 }
307
309 const std::string &GetMessage() const
310 {
311 return _errorMsg;
312 }
313
319 const VtValue &GetMetadata() const
320 {
321 return _metadata;
322 }
323
339 USDVALIDATION_API
341
343 USDVALIDATION_API
344 std::string GetErrorAsString() const;
345
348 bool HasNoError() const
349 {
350 return _errorType == UsdValidationErrorType::None;
351 }
352
355 USDVALIDATION_API
356 const std::vector<const UsdValidationFixer *> GetFixers() const;
357
361 USDVALIDATION_API
362 const UsdValidationFixer *GetFixerByName(const TfToken &name) const;
363
367 USDVALIDATION_API
368 const std::vector<const UsdValidationFixer *>
370
374 USDVALIDATION_API
376 const TfToken &name) const;
377
384 USDVALIDATION_API
385 const std::vector<const UsdValidationFixer *> GetFixersByKeywords(
386 const TfTokenVector &keywords) const;
387
388
389private:
390 // UsdValidationValidatorError holds a pointer to the UsdValidationValidator
391 // that generated it, so we need to provide friend access to allow the
392 // necessary mutation.
393 friend class UsdValidationValidator;
394
395 // Used by UsdValidationValidator::Validate methods to embed itself to the
396 // reported errors.
397 void _SetValidator(const UsdValidationValidator *validator);
398
399 // _validator is set when ValidationError is generated via a
400 // UsdValidationValidator::Validate() call.
401 const UsdValidationValidator *_validator;
402
403 // These data members should not be modified other than during
404 // initialization by the validate task functions.
405 TfToken _name;
406 UsdValidationErrorType _errorType;
407 UsdValidationErrorSites _errorSites;
408 std::string _errorMsg;
409 VtValue _metadata;
410
411}; // UsdValidationError
412
413PXR_NAMESPACE_CLOSE_SCOPE
414
415#endif // PXR_USD_VALIDATION_USD_VALIDATION_ERROR_H
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:274
bool IsEmpty() const noexcept
Returns true if this is the empty path (SdfPath::EmptyPath()).
Definition: path.h:398
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
bool IsValid() const
Return true if this is a valid object, false otherwise.
Definition: object.h:126
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a "Prim" as ...
Definition: prim.h:117
Base class for UsdAttribute and UsdRelationship scenegraph objects.
Definition: property.h:38
UsdValidationError is an entity returned by a validation task, which is associated with a UsdValidati...
Definition: error.h:237
USDVALIDATION_API UsdValidationError(const TfToken &name, const UsdValidationErrorType &errorType, const UsdValidationErrorSites &errorSites, const std::string &errorMsg, const VtValue &metadata=VtValue())
Instantiate a ValidationError by providing its name, errorType, errorSites and an errorMsg.
USDVALIDATION_API UsdValidationError()
A default constructed UsdValidationError signifies no error.
TfToken GetName() &&
Returns the name token of the UsdValidationError by-value.
Definition: error.h:272
USDVALIDATION_API const std::vector< const UsdValidationFixer * > GetFixersByErrorName() const
Return a vector of immutable fixers catering to a specific errorName.
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 * > GetFixers() const
Return a vector of fixers associated with this Validator.
const UsdValidationErrorSites & GetSites() const &
Returns the UsdValidationErrorSite associated with this UsdValidationError.
Definition: error.h:286
UsdValidationErrorType GetType() const
Returns the UsdValidationErrorType associated with this UsdValidationError.
Definition: error.h:279
const TfToken & GetName() const &
Returns the name token of the UsdValidationError.
Definition: error.h:266
const std::string & GetMessage() const
Returns the message associated with this UsdValidationError.
Definition: error.h:309
bool HasNoError() const
Returns true if UsdValidationErrorType is UsdValidationErrorType::None, false otherwise.
Definition: error.h:348
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.
UsdValidationErrorSites GetSites() &&
Returns the UsdValidationErrorSite associated with this UsdValidationError by-value.
Definition: error.h:293
const UsdValidationValidator * GetValidator() const
Returns the UsdValidationValidator that reported this error.
Definition: error.h:303
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,...
USDVALIDATION_API TfToken GetIdentifier() const
An identifier for the error constructed from the validator name this error was generated from and its...
const VtValue & GetMetadata() const
Returns the metadata associated with this UsdValidationError.
Definition: error.h:319
USDVALIDATION_API std::string GetErrorAsString() const
Returns UsdValidationErrorType and ErrorMessage concatenated as a string.
UsdValidationErrorType reflects severity of a validation error, which can then be reported appropriat...
A UsdValidationFixer represents a fix that can be applied to fix a specific validation error.
Definition: fixer.h:63
UsdValidationValidator is a class describing a single test.
Definition: validator.h:138
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:152
UsdValidationErrorSite is important information available from a ValidationError, which annotates the...
Definition: error.h:55
bool operator!=(const UsdValidationErrorSite &other) const
Returns false if other UsdValidationErrorSite has same valued members as this UsdValidationErrorSite,...
Definition: error.h:189
bool IsProperty() const
Returns true if UsdValidationErrorSite represents a property on a stage, false otherwise.
Definition: error.h:111
USDVALIDATION_API UsdValidationErrorSite(const UsdStagePtr &usdStage, const SdfPath &objectPath, const SdfLayerHandle &layer=SdfLayerHandle())
Initialize an UsdValidationErrorSite using a usdStage and an objectPath.
bool IsValidSpecInLayer() const
Returns true if the objectPath and layer represent a spec in the layer; false otherwise.
Definition: error.h:94
UsdProperty GetProperty() const
Returns UsdProperty associated with this UsdValidationErrorSite, that is when UsdStage is present and...
Definition: error.h:171
bool IsPrim() const
Returns true if UsdValidationErrorSite represents a prim on a stage, false otherwise.
Definition: error.h:104
USDVALIDATION_API UsdValidationErrorSite(const SdfLayerHandle &layer, const SdfPath &objectPath)
Initialize an UsdValidationErrorSite using a layer and an objectPath.
UsdPrim GetPrim() const
Returns UsdPrim associated with this UsdValidationErrorSite, that is when UsdStage is present and obj...
Definition: error.h:160
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
const UsdStagePtr & GetStage() const
Returns the UsdStage associated with this UsdValidationErrorSite; nullptr othewrise.
Definition: error.h:152
bool IsValid() const
Returns true if UsdValidationErrorSite instance can either point to a prim or property spec in a laye...
Definition: error.h:87
const SdfLayerHandle & GetLayer() const
Returns the SdfLayerHandle associated with this UsdValidationValidatorErrorSite.
Definition: error.h:145
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:440