improve the error messages for the immutable fields

This commit is contained in:
Tamo 2023-01-19 16:45:10 +01:00
parent a1e9c44fe5
commit e3742a38d4
No known key found for this signature in database
GPG key ID: 20CD8020AFA88D69
4 changed files with 43 additions and 33 deletions

View file

@ -11,6 +11,7 @@ use time::macros::{format_description, time};
use time::{Date, OffsetDateTime, PrimitiveDateTime};
use uuid::Uuid;
use crate::deserr::error_messages::immutable_field_error;
use crate::deserr::DeserrJsonError;
use crate::error::deserr_codes::*;
use crate::error::{unwrap_any, Code, ParseOffsetDateTimeError};
@ -57,22 +58,19 @@ fn deny_immutable_fields_api_key(
accepted: &[&str],
location: ValuePointerRef,
) -> DeserrJsonError {
let mut error = unwrap_any(DeserrJsonError::<BadRequest>::error::<Infallible>(
None,
deserr::ErrorKind::UnknownKey { key: field, accepted },
location,
));
error.code = match field {
"uid" => Code::ImmutableApiKeyUid,
"actions" => Code::ImmutableApiKeyActions,
"indexes" => Code::ImmutableApiKeyIndexes,
"expiresAt" => Code::ImmutableApiKeyExpiresAt,
"createdAt" => Code::ImmutableApiKeyCreatedAt,
"updatedAt" => Code::ImmutableApiKeyUpdatedAt,
_ => Code::BadRequest,
};
error
match field {
"uid" => immutable_field_error(field, accepted, Code::ImmutableApiKeyUid),
"actions" => immutable_field_error(field, accepted, Code::ImmutableApiKeyActions),
"indexes" => immutable_field_error(field, accepted, Code::ImmutableApiKeyIndexes),
"expiresAt" => immutable_field_error(field, accepted, Code::ImmutableApiKeyExpiresAt),
"createdAt" => immutable_field_error(field, accepted, Code::ImmutableApiKeyCreatedAt),
"updatedAt" => immutable_field_error(field, accepted, Code::ImmutableApiKeyUpdatedAt),
_ => unwrap_any(DeserrJsonError::<BadRequest>::error::<Infallible>(
None,
deserr::ErrorKind::UnknownKey { key: field, accepted },
location,
)),
}
}
#[derive(Debug, DeserializeFromValue)]