Loading...
Searching...
No Matches
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
218public:
220 USD_API
222
225 USD_API
227 const UsdValidationErrorSites &errorSites,
228 const std::string &errorMsg);
229
230 bool operator==(const UsdValidationError& other) const {
231 return (_errorType == other._errorType) &&
232 (_errorSites == other._errorSites) &&
233 (_errorMsg == other._errorMsg) &&
234 (_validator == other._validator);
235 }
236
237 bool operator!=(const UsdValidationError& other) const {
238 return !(*this == other);
239 }
240
244 {
245 return _errorType;
246 }
247
250 const UsdValidationErrorSites& GetSites() const
251 {
252 return _errorSites;
253 }
254
261 {
262 return _validator;
263 }
264
266 const std::string& GetMessage() const
267 {
268 return _errorMsg;
269 }
270
272 USD_API
273 std::string GetErrorAsString() const;
274
277 bool HasNoError() const {
278 return _errorType == UsdValidationErrorType::None;
279 }
280
281private:
282 // UsdValidatorError holds a pointer to the UsdValidator that generated it, so
283 // we need to provide friend access to allow the necessary mutation.
284 friend class UsdValidator;
285
286 // Used by UsdValidator::Validate methods to embed itself to the reported
287 // errors.
288 void _SetValidator(const UsdValidator *validator);
289
290 // _validator is set when ValidationError is generated via a
291 // UsdValidator::Validate() call.
292 const UsdValidator *_validator;
293
294 // These data members should not be modified other than during
295 // initialization by the validate task functions.
296 UsdValidationErrorType _errorType;
297 UsdValidationErrorSites _errorSites;
298 std::string _errorMsg;
299
300 // TODO:(Subsequent iterations)
301 // - VtValue of a random value the error wants to propagate down to the
302 // fixer
303
304}; // UsdValidationError
305
306PXR_NAMESPACE_CLOSE_SCOPE
307
308#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
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.
USD_API UsdValidationError(const UsdValidationErrorType &errorType, const UsdValidationErrorSites &errorSites, const std::string &errorMsg)
Instantiate a ValidationError by providing its errorType, errorSites and an errorMsg.
UsdValidationErrorType GetType() const
Returns the UsdValidationErrorType associated with this 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.
USD_API std::string GetErrorAsString() const
Returns UsdValidationErrorType and ErrorMessage concatenated as a string.
const UsdValidationErrorSites & GetSites() const
Returns the UsdValidationErrorSite associated with this UsdValidationError.
UsdValidationErrorType reflects severity of a validation error, which can then be reported appropriat...
UsdValidator is a class describing a single test.
Definition: validator.h:117
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.