p2panda_rs/schema/validate/
error.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3//! Error types for validating operation fields against schemas.
4use thiserror::Error;
5
6use crate::operation::error::PlainValueError;
7
8/// Custom error types for validating raw operations with schemas.
9#[derive(Error, Debug)]
10pub enum ValidationError {
11    /// Field with this name is required by schema.
12    #[error("missing required field: '{0}' of type {1}")]
13    MissingField(String, String),
14
15    /// One or more fields which do not belong to the schema.
16    #[error("unexpected fields found: {0}")]
17    UnexpectedFields(String),
18
19    /// Raw operation field did not match schema.
20    #[error("field '{0}' does not match schema: {1}")]
21    InvalidField(String, String),
22
23    /// Field type and schema do not match.
24    #[error("expected field name '{1}'")]
25    InvalidName(String, String),
26
27    /// Field type and schema do not match.
28    #[error("invalid field type '{0}', expected '{1}'")]
29    InvalidType(String, String),
30
31    /// Field value is not correctly formatted.
32    #[error("{0}")]
33    InvalidValue(String),
34
35    /// Field value is not in canonic format.
36    #[error("non-canonic document view id, {0}")]
37    InvalidDocumentViewId(String),
38
39    /// Error from validating system schema: `schema_definition_v1`.
40    #[error("invalid 'schema_definition_v1' operation: {0}")]
41    InvalidSchemaDefinition(#[from] SchemaDefinitionError),
42
43    /// Error from validating system schema: `schema_field_definition_v1`.
44    #[error("invalid 'schema_field_definition_v1' operation: {0}")]
45    InvalidSchemaFieldDefinition(#[from] SchemaFieldDefinitionError),
46
47    /// Error from conversion of PlainValues.
48    #[error(transparent)]
49    NotStringValue(#[from] PlainValueError),
50
51    /// Error from validating system schema: `blob_v1`.
52    #[error("invalid 'blob_v1' operation: {0}")]
53    InvalidBlob(#[from] BlobError),
54
55    /// Error from validating system schema: `blob_piece_v1`.
56    #[error("invalid 'blob_piece_v1' operation: {0}")]
57    InvalidBlobPiece(#[from] BlobPieceError),
58}
59
60/// Custom error types for validating operations against `schema_field_definition_v1` schema.
61#[derive(Error, Debug)]
62#[allow(missing_copy_implementations)]
63pub enum SchemaFieldDefinitionError {
64    /// "name" is not correctly formatted as per specification.
65    #[error("'name' field in schema field definitions is wrongly formatted")]
66    NameInvalid,
67
68    /// "type" is not correctly formatted as per specification.
69    #[error("'type' field in schema field definitions is wrongly formatted")]
70    TypeInvalid,
71
72    /// Error from conversion of PlainValues.
73    #[error(transparent)]
74    NotStringValue(#[from] PlainValueError),
75}
76
77/// Custom error types for validating operations against `schema_field_definition_v1` schema.
78#[derive(Error, Debug)]
79#[allow(missing_copy_implementations)]
80pub enum SchemaDefinitionError {
81    /// "name" is not correctly formatted as per specification.
82    #[error("'name' field in schema field definitions is wrongly formatted")]
83    NameInvalid,
84
85    /// "description" is not correctly formatted as per specification.
86    #[error("'description' field in schema field definitions is wrongly formatted")]
87    DescriptionInvalid,
88
89    /// "fields" is not correctly formatted as per specification.
90    #[error("'fields' field in schema field definitions is wrongly formatted")]
91    FieldsInvalid,
92
93    /// Error from conversion of PlainValues.
94    #[error(transparent)]
95    NotStringValue(#[from] PlainValueError),
96}
97
98/// Custom error types for validating operations against `blob_v1` schema.
99#[derive(Error, Debug)]
100#[allow(missing_copy_implementations)]
101pub enum BlobError {
102    /// "mime_type" is not correctly formatted as per specification.
103    #[error("'mime_type' field in blob is wrongly formatted")]
104    MimeTypeInvalid,
105
106    /// "pieces" can not be empty as per specification.
107    #[error("'pieces' field can not be empty")]
108    PiecesEmpty,
109}
110
111/// Custom error types for validating operations against `blob_piece_v1` schema.
112#[derive(Error, Debug)]
113#[allow(missing_copy_implementations)]
114pub enum BlobPieceError {
115    /// "data" is greater than the maximum allowed as per specification.
116    #[error("'data' field in blob is over maximum allowed length")]
117    DataInvalid,
118}