aboutsummaryrefslogtreecommitdiffstats
path: root/lib/api-config/src/schema/create_schema.rs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/api-config/src/schema/create_schema.rs')
-rw-r--r--lib/api-config/src/schema/create_schema.rs70
1 files changed, 70 insertions, 0 deletions
diff --git a/lib/api-config/src/schema/create_schema.rs b/lib/api-config/src/schema/create_schema.rs
new file mode 100644
index 0000000..493fb09
--- /dev/null
+++ b/lib/api-config/src/schema/create_schema.rs
@@ -0,0 +1,70 @@
+use serde::{Deserialize, Serialize};
+use tracing::debug;
+
+use crate::{
+ ConfigurationError,
+ schema::{SchemaService, TransactionSchema},
+};
+
+#[derive(Deserialize, Serialize)]
+#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
+#[cfg_attr(feature = "utoipa", schema(example = json!({
+ "schemaType": "custom.schema",
+ "schemaVersion": "1.0.0",
+ "schema": {
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
+ "title": "FinancialTransaction",
+ "type": "object",
+ "required": ["transactionId", "amount", "currency", "timestamp"],
+ "properties": {
+ "transactionId": {
+ "type": "string",
+ "format": "uuid"
+ },
+ "amount": {
+ "type": "number",
+ "exclusiveMinimum": 0
+ },
+ "currency": {
+ "type": "string",
+ "pattern": "^[A-Z]{3}$",
+ "description": "ISO 4217 Alpha-3 code (e.g., USD, EUR)"
+ },
+ "timestamp": {
+ "type": "string",
+ "format": "date-time"
+ },
+ }
+ }
+})))]
+#[serde(rename_all = "camelCase")]
+/// The json schema to validate for each transaction of this type and version
+pub struct CreateSchema {
+ /// Transaction schema type
+ pub schema_type: String,
+ /// The schema's version
+ pub schema_version: String,
+ /// The json schema
+ pub schema: serde_json::Value,
+}
+
+pub(super) async fn create_schema(
+ state: &SchemaService,
+ kind: &str,
+ version: &str,
+ schema: &serde_json::Value,
+) -> Result<TransactionSchema, ConfigurationError> {
+ debug!("creating transaction schema");
+ sqlx::query_as!(
+ TransactionSchema,
+ "insert into transaction_schema (schema_type, schema_version, schema) values ($1, $2, $3)
+ returning *
+ ",
+ kind,
+ version,
+ sqlx::types::Json(&schema) as _
+ )
+ .fetch_one(&state.database)
+ .await
+ .map_err(|e| e.into())
+}