All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
validationError.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_USD_VALIDATION_ERROR_H
9#define PXR_USD_USD_VALIDATION_ERROR_H
10
11#include "pxr/pxr.h"
12#include "pxr/usd/sdf/path.h"
13#include "pxr/usd/usd/stage.h"
14#include "pxr/usd/usd/property.h"
15
16#include <string>
17
18PXR_NAMESPACE_OPEN_SCOPE
19
20class UsdPrim;
21class UsdValidator;
22
35enum class UsdValidationErrorType {
36 None = 0,
37 Error,
38 Warn,
39 Info
40};
41
52{
53public:
54 UsdValidationErrorSite() = default;
55
63 USD_API
64 UsdValidationErrorSite(const SdfLayerHandle &layer,
65 const SdfPath &objectPath);
66
77 USD_API
78 UsdValidationErrorSite(const UsdStagePtr &usdStage,
79 const SdfPath &objectPath,
80 const SdfLayerHandle &layer=SdfLayerHandle());
81
84 bool IsValid() const
85 {
86 return IsValidSpecInLayer() || IsPrim() || IsProperty();
87 }
88
91 bool IsValidSpecInLayer() const
92 {
93 if (!_layer || _objectPath.IsEmpty()) {
94 return false;
95 }
96 return _layer->HasSpec(_objectPath);
97 }
98
101 bool IsPrim() const
102 {
103 return GetPrim().IsValid();
104 }
105
108 bool IsProperty() const
109 {
110 return GetProperty().IsValid();
111 }
112
119 const SdfPropertySpecHandle GetPropertySpec() const
120 {
121 if (!_layer) {
122 return SdfPropertySpecHandle();
123 }
124 return _layer->GetPropertyAtPath(_objectPath);
125 }
126
132 const SdfPrimSpecHandle GetPrimSpec() const
133 {
134 if (!_layer) {
135 return SdfPrimSpecHandle();
136 }
137 return _layer->GetPrimAtPath(_objectPath);
138 }
139
141 const SdfLayerHandle& GetLayer() const
142 {
143 return _layer;
144 }
145
148 const UsdStagePtr& GetStage() const
149 {
150 return _usdStage;
151 }
152
157 {
158 if (_usdStage) {
159 return _usdStage->GetPrimAtPath(_objectPath);
160 }
161 return UsdPrim();
162 }
163
168 {
169 if (_usdStage) {
170 return _usdStage->GetPropertyAtPath(_objectPath);
171 }
172 return UsdProperty();
173 }
174
177 bool operator==(const UsdValidationErrorSite& other) const {
178 return (_layer == other._layer) &&
179 (_usdStage == other._usdStage) &&
180 (_objectPath == other._objectPath);
181 }
182
185 bool operator!=(const UsdValidationErrorSite& other) const {
186 return !(*this == other);
187 }
188
189private:
190 UsdStagePtr _usdStage;
191 SdfLayerHandle _layer;
192 SdfPath _objectPath;
193
194}; // UsdValidationErrorSite
195
196using UsdValidationErrorSites = std::vector<UsdValidationErrorSite>;
197
221public:
223 USD_API
225
228 USD_API
230 const UsdValidationErrorType &errorType,
231 const UsdValidationErrorSites &errorSites,
232 const std::string &errorMsg);
233
234 bool operator==(const UsdValidationError& other) const {
235 return (_name == other._name) &&
236 (_errorType == other._errorType) &&
237 (_errorSites == other._errorSites) &&
238 (_errorMsg == other._errorMsg) &&
239 (_validator == other._validator);
240 }
241
242 bool operator!=(const UsdValidationError& other) const {
243 return !(*this == other);
244 }
245
247 const TfToken& GetName() const &
248 {
249 return _name;
250 }
251
254 {
255 return std::move(_name);
256 }
257
261 {
262 return _errorType;
263 }
264
267 const UsdValidationErrorSites& GetSites() const &
268 {
269 return _errorSites;
270 }
271
274 UsdValidationErrorSites GetSites() &&
275 {
276 return std::move(_errorSites);
277 }
278
285 {
286 return _validator;
287 }
288
290 const std::string& GetMessage() const
291 {
292 return _errorMsg;
293 }
294
310 USD_API
312
314 USD_API
315 std::string GetErrorAsString() const;
316
319 bool HasNoError() const {
320 return _errorType == UsdValidationErrorType::None;
321 }
322
323private:
324 // UsdValidatorError holds a pointer to the UsdValidator that generated it, so
325 // we need to provide friend access to allow the necessary mutation.
326 friend class UsdValidator;
327
328 // Used by UsdValidator::Validate methods to embed itself to the reported
329 // errors.
330 void _SetValidator(const UsdValidator *validator);
331
332 // _validator is set when ValidationError is generated via a
333 // UsdValidator::Validate() call.
334 const UsdValidator *_validator;
335
336 // These data members should not be modified other than during
337 // initialization by the validate task functions.
338 TfToken _name;
339 UsdValidationErrorType _errorType;
340 UsdValidationErrorSites _errorSites;
341 std::string _errorMsg;
342
343 // TODO:(Subsequent iterations)
344 // - VtValue of a random value the error wants to propagate down to the
345 // fixer
346
347}; // UsdValidationError
348
349PXR_NAMESPACE_CLOSE_SCOPE
350
351#endif // PXR_USD_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 UsdValidato...
USD_API UsdValidationError()
A default constructed UsdValidationError signifies no error.
TfToken GetName() &&
Returns the name token of the UsdValidationError by-value.
const UsdValidationErrorSites & GetSites() const &
Returns the UsdValidationErrorSite associated with this UsdValidationError.
UsdValidationErrorType GetType() const
Returns the UsdValidationErrorType associated with this UsdValidationError.
USD_API TfToken GetIdentifier() const
An identifier for the error constructed from the validator name this error was generated from and its...
const TfToken & GetName() const &
Returns the name token of the UsdValidationError.
const std::string & GetMessage() const
Returns the message associated with this UsdValidationError.
bool HasNoError() const
Returns true if UsdValidationErrorType is UsdValidationErrorType::None, false otherwise.
const UsdValidator * GetValidator() const
Returns the UsdValidator that reported this error.
UsdValidationErrorSites GetSites() &&
Returns the UsdValidationErrorSite associated with this UsdValidationError by-value.
USD_API UsdValidationError(const TfToken &name, const UsdValidationErrorType &errorType, const UsdValidationErrorSites &errorSites, const std::string &errorMsg)
Instantiate a ValidationError by providing its name, errorType, errorSites and an errorMsg.
USD_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...
UsdValidator is a class describing a single test.
Definition: validator.h:120
UsdValidationErrorSite is important information available from a ValidationError, which annotates the...
bool operator!=(const UsdValidationErrorSite &other) const
Returns false if other UsdValidationErrorSite has same valued members as this UsdValidationErrorSite,...
bool IsProperty() const
Returns true if UsdValidationErrorSite represents a property on a stage, false otherwise.
bool IsValidSpecInLayer() const
Returns true if the objectPath and layer represent a spec in the layer; false otherwise.
UsdProperty GetProperty() const
Returns UsdProperty associated with this UsdValidationErrorSite, that is when UsdStage is present and...
bool IsPrim() const
Returns true if UsdValidationErrorSite represents a prim on a stage, false otherwise.
UsdPrim GetPrim() const
Returns UsdPrim associated with this UsdValidationErrorSite, that is when UsdStage is present and obj...
USD_API UsdValidationErrorSite(const UsdStagePtr &usdStage, const SdfPath &objectPath, const SdfLayerHandle &layer=SdfLayerHandle())
Initialize an UsdValidationErrorSite using a usdStage and an objectPath.
bool operator==(const UsdValidationErrorSite &other) const
Returns true if other UsdValidationErrorSite has same valued members as this UsdValidationErrorSite,...
USD_API UsdValidationErrorSite(const SdfLayerHandle &layer, const SdfPath &objectPath)
Initialize an UsdValidationErrorSite using a layer and an objectPath.
const SdfPrimSpecHandle GetPrimSpec() const
Returns the SdfPrimSpecHandle associated with this ValidationErrorSite's layer and objectPath.
const UsdStagePtr & GetStage() const
Returns the UsdStage associated with this UsdValidationErrorSite; nullptr othewrise.
bool IsValid() const
Returns true if UsdValidationErrorSite instance can either point to a prim or property spec in a laye...
const SdfLayerHandle & GetLayer() const
Returns the SdfLayerHandle associated with this UsdValidatorErrorSite.
const SdfPropertySpecHandle GetPropertySpec() const
Returns the SdfPropertySpecHandle associated with this ValidationErrorSite's layer and objectPath.