Skip to content

Commit 82ec5df

Browse files
authored
chore(config): externalize entity schema (#56)
1 parent bcef7e1 commit 82ec5df

4 files changed

Lines changed: 94 additions & 92 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2020-12/schema#",
3+
"type": "object",
4+
"properties": {
5+
"key": { "type": "string" },
6+
"allowed_models": {
7+
"type": "array",
8+
"items": { "type": "string" }
9+
},
10+
"rate_limit": { "type": "object" }
11+
},
12+
"required": ["key", "allowed_models"],
13+
"additionalProperties": false
14+
}

src/config/entities/apikeys.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::sync::{Arc, LazyLock};
22

33
use serde::{Deserialize, Serialize};
4-
use serde_json::json;
54
use utoipa::ToSchema;
65

76
use super::{EntityStore, IndexFn, ResourceEntry};
@@ -14,20 +13,8 @@ use crate::{
1413
};
1514

1615
static SCHEMA: LazyLock<serde_json::Value> = LazyLock::new(|| {
17-
json!({
18-
"$schema": "https://json-schema.org/draft/2020-12/schema#",
19-
"type": "object",
20-
"properties": {
21-
"key": {"type": "string"},
22-
"allowed_models": {
23-
"type": "array",
24-
"items": { "type": "string" }
25-
},
26-
"rate_limit": {"type": "object"}
27-
},
28-
"required": ["key", "allowed_models"],
29-
"additionalProperties": false
30-
})
16+
serde_json::from_str(include_str!("apikeys-schema.json"))
17+
.expect("Invalid JSON document for API Key schema")
3118
});
3219
pub static SCHEMA_VALIDATOR: LazyLock<jsonschema::Validator> =
3320
LazyLock::new(|| jsonschema::validator_for(&SCHEMA).expect("Invalid JSON schema for API Key"));
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2020-12/schema#",
3+
"type": "object",
4+
"properties": {
5+
"name": { "type": "string" },
6+
"model": {
7+
"type": "string",
8+
"pattern": "^(anthropic|bedrock|deepseek|gemini|openai)/.+$"
9+
},
10+
"provider_config": { "type": "object" },
11+
"timeout": {
12+
"type": "integer",
13+
"minimum": 0
14+
},
15+
"rate_limit": { "type": "object" }
16+
},
17+
"required": ["name", "model", "provider_config"],
18+
"additionalProperties": false,
19+
"allOf": [
20+
{
21+
"if": {
22+
"properties": {
23+
"model": {
24+
"type": "string",
25+
"pattern": "^(anthropic|deepseek|gemini|openai)/.+$"
26+
}
27+
},
28+
"required": ["model"]
29+
},
30+
"then": {
31+
"properties": {
32+
"provider_config": { "$ref": "#/$defs/openai_compatible" }
33+
}
34+
}
35+
},
36+
{
37+
"if": {
38+
"properties": {
39+
"model": {
40+
"type": "string",
41+
"pattern": "^bedrock/.+$"
42+
}
43+
},
44+
"required": ["model"]
45+
},
46+
"then": {
47+
"properties": {
48+
"provider_config": { "$ref": "#/$defs/bedrock" }
49+
}
50+
}
51+
}
52+
],
53+
"$defs": {
54+
"openai_compatible": {
55+
"type": "object",
56+
"required": ["api_key"],
57+
"properties": {
58+
"api_key": { "type": "string" },
59+
"api_base": { "type": "string" }
60+
},
61+
"additionalProperties": false
62+
},
63+
"bedrock": {
64+
"type": "object",
65+
"required": ["region", "access_key_id", "secret_access_key"],
66+
"properties": {
67+
"region": { "type": "string" },
68+
"access_key_id": { "type": "string" },
69+
"secret_access_key": { "type": "string" },
70+
"session_token": { "type": "string" },
71+
"endpoint": { "type": "string" }
72+
},
73+
"additionalProperties": false
74+
}
75+
}
76+
}

src/config/entities/models.rs

Lines changed: 2 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::{
44
};
55

66
use serde::{Deserialize, Serialize, de::Error};
7-
use serde_json::json;
87
use utoipa::ToSchema;
98

109
use super::{ConfigProvider, EntityStore, IndexFn};
@@ -18,82 +17,8 @@ use crate::{
1817
};
1918

2019
static SCHEMA: LazyLock<serde_json::Value> = LazyLock::new(|| {
21-
json!({
22-
"$schema": "https://json-schema.org/draft/2020-12/schema#",
23-
"type": "object",
24-
"properties": {
25-
"name": {"type": "string"},
26-
"model": {
27-
"type": "string",
28-
"pattern": MODELS_PATTERN
29-
},
30-
"provider_config": {"type": "object"},
31-
"timeout": {
32-
"type": "integer",
33-
"minimum": 0
34-
},
35-
"rate_limit": {"type": "object"}
36-
},
37-
"required": ["name", "model", "provider_config"],
38-
"additionalProperties": false,
39-
"allOf": [
40-
{
41-
"if": {
42-
"properties": {
43-
"model": {
44-
"type": "string",
45-
"pattern": "^(anthropic|deepseek|gemini|openai)/.+$"
46-
}
47-
},
48-
"required": ["model"]
49-
},
50-
"then": {
51-
"properties": {
52-
"provider_config": { "$ref": "#/$defs/openai_compatible" }
53-
}
54-
}
55-
},
56-
{
57-
"if": {
58-
"properties": {
59-
"model": {
60-
"type": "string",
61-
"pattern": "^bedrock/.+$"
62-
}
63-
},
64-
"required": ["model"]
65-
},
66-
"then": {
67-
"properties": {
68-
"provider_config": { "$ref": "#/$defs/bedrock" }
69-
}
70-
}
71-
}
72-
],
73-
"$defs": {
74-
"openai_compatible": {
75-
"type": "object",
76-
"required": ["api_key"],
77-
"properties": {
78-
"api_key": {"type": "string"},
79-
"api_base": {"type": "string"}
80-
},
81-
"additionalProperties": false
82-
},
83-
"bedrock": {
84-
"type": "object",
85-
"required": ["region", "access_key_id", "secret_access_key"],
86-
"properties": {
87-
"region": {"type": "string"},
88-
"access_key_id": {"type": "string"},
89-
"secret_access_key": {"type": "string"},
90-
"session_token": {"type": "string"},
91-
"endpoint": {"type": "string"}
92-
},
93-
"additionalProperties": false
94-
}
95-
}
96-
})
20+
serde_json::from_str(include_str!("models-schema.json"))
21+
.expect("Invalid JSON document for Model schema")
9722
});
9823
pub static SCHEMA_VALIDATOR: LazyLock<jsonschema::Validator> =
9924
LazyLock::new(|| jsonschema::validator_for(&SCHEMA).expect("Invalid JSON schema for Model"));

0 commit comments

Comments
 (0)