this repo has no description
1use crate::validation::{RecordValidator, ValidationError, ValidationStatus};
2use axum::{
3 Json,
4 http::StatusCode,
5 response::{IntoResponse, Response},
6};
7use serde_json::json;
8
9pub fn validate_record(record: &serde_json::Value, collection: &str) -> Result<(), Box<Response>> {
10 validate_record_with_rkey(record, collection, None)
11}
12
13pub fn validate_record_with_rkey(
14 record: &serde_json::Value,
15 collection: &str,
16 rkey: Option<&str>,
17) -> Result<(), Box<Response>> {
18 let validator = RecordValidator::new();
19 validation_error_to_response(validator.validate_with_rkey(record, collection, rkey))
20}
21
22pub fn validate_record_with_status(
23 record: &serde_json::Value,
24 collection: &str,
25 rkey: Option<&str>,
26 require_lexicon: bool,
27) -> Result<ValidationStatus, Box<Response>> {
28 let validator = RecordValidator::new().require_lexicon(require_lexicon);
29 match validator.validate_with_rkey(record, collection, rkey) {
30 Ok(status) => Ok(status),
31 Err(e) => Err(validation_error_to_box_response(e)),
32 }
33}
34
35fn validation_error_to_response(
36 result: Result<ValidationStatus, ValidationError>,
37) -> Result<(), Box<Response>> {
38 match result {
39 Ok(_) => Ok(()),
40 Err(e) => Err(validation_error_to_box_response(e)),
41 }
42}
43
44fn validation_error_to_box_response(e: ValidationError) -> Box<Response> {
45 match e {
46 ValidationError::MissingType => Box::new(
47 (
48 StatusCode::BAD_REQUEST,
49 Json(json!({"error": "InvalidRecord", "message": "Record must have a $type field"})),
50 )
51 .into_response(),
52 ),
53 ValidationError::TypeMismatch { expected, actual } => Box::new(
54 (
55 StatusCode::BAD_REQUEST,
56 Json(json!({"error": "InvalidRecord", "message": format!("Record $type '{}' does not match collection '{}'", actual, expected)})),
57 )
58 .into_response(),
59 ),
60 ValidationError::MissingField(field) => Box::new(
61 (
62 StatusCode::BAD_REQUEST,
63 Json(json!({"error": "InvalidRecord", "message": format!("Missing required field: {}", field)})),
64 )
65 .into_response(),
66 ),
67 ValidationError::InvalidField { path, message } => Box::new(
68 (
69 StatusCode::BAD_REQUEST,
70 Json(json!({"error": "InvalidRecord", "message": format!("Invalid field '{}': {}", path, message)})),
71 )
72 .into_response(),
73 ),
74 ValidationError::InvalidDatetime { path } => Box::new(
75 (
76 StatusCode::BAD_REQUEST,
77 Json(json!({"error": "InvalidRecord", "message": format!("Invalid datetime format at '{}'", path)})),
78 )
79 .into_response(),
80 ),
81 ValidationError::BannedContent { path } => Box::new(
82 (
83 StatusCode::BAD_REQUEST,
84 Json(json!({"error": "InvalidRecord", "message": format!("Unacceptable slur in record at '{}'", path)})),
85 )
86 .into_response(),
87 ),
88 ValidationError::UnknownType(type_name) => Box::new(
89 (
90 StatusCode::BAD_REQUEST,
91 Json(json!({"error": "InvalidRecord", "message": format!("Lexicon not found: lex:{}", type_name)})),
92 )
93 .into_response(),
94 ),
95 e => Box::new(
96 (
97 StatusCode::BAD_REQUEST,
98 Json(json!({"error": "InvalidRecord", "message": e.to_string()})),
99 )
100 .into_response(),
101 ),
102 }
103}