121-call table checked against the OpenAI OpenAPI document, admin-key-only client, destructive guards, WebhookVerifier (Standard Webhooks HMAC-SHA256), demo routes under /ai/openai/admin, local and opt-in live tests. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
70 lines
2.5 KiB
C++
70 lines
2.5 KiB
C++
#include "openai/admin/validate.h"
|
|
|
|
#include "llm/api_client.h"
|
|
#include "llm/error.h"
|
|
#include "openai/admin/caps.h"
|
|
|
|
namespace molo::openai::admin {
|
|
|
|
void Validate::fail(const std::string& message) {
|
|
throw llm::ApiError::invalid("openai", message);
|
|
}
|
|
|
|
void Validate::text(const std::string& what, const std::string& value, std::size_t max,
|
|
bool allow_empty) {
|
|
if (value.empty() && !allow_empty) fail(what + " is required");
|
|
if (value.size() > max) fail(what + " is over " + std::to_string(max) + " chars");
|
|
for (unsigned char c : value)
|
|
if (c < 0x20 || c == 0x7f) fail(what + " has a control character");
|
|
}
|
|
|
|
void Validate::id(const std::string& what, const std::string& value) {
|
|
if (!llm::Service::valid_id(value)) fail("bad " + what);
|
|
}
|
|
|
|
void Validate::one_of(const std::string& what, const std::string& value,
|
|
std::initializer_list<const char*> allowed) {
|
|
std::string list;
|
|
for (const char* a : allowed) {
|
|
if (value == a) return;
|
|
list += (list.empty() ? "" : ", ") + std::string(a);
|
|
}
|
|
fail(what + " must be one of: " + list);
|
|
}
|
|
|
|
void Validate::email(const std::string& value) {
|
|
text("email", value, AdminCaps::kMaxEmailChars);
|
|
const std::size_t at = value.find('@');
|
|
if (at == std::string::npos || at == 0 || at + 1 >= value.size() ||
|
|
value.find('@', at + 1) != std::string::npos || value.find(' ') != std::string::npos ||
|
|
value.find('.', at) == std::string::npos)
|
|
fail("email is not an address");
|
|
}
|
|
|
|
void Validate::range(const std::string& what, std::int64_t value, std::int64_t lo,
|
|
std::int64_t hi) {
|
|
if (value < lo || value > hi)
|
|
fail(what + " must be " + std::to_string(lo) + ".." + std::to_string(hi));
|
|
}
|
|
|
|
void Validate::count(const std::string& what, std::size_t n, std::size_t max, bool allow_empty) {
|
|
if (n == 0 && !allow_empty) fail(what + " needs at least one entry");
|
|
if (n > max) fail(what + " has more than " + std::to_string(max) + " entries");
|
|
}
|
|
|
|
void Validate::epoch(const std::string& what, std::int64_t value) {
|
|
range(what, value, 1, AdminCaps::kMaxTimeSeconds);
|
|
}
|
|
|
|
void Validate::https_url(const std::string& what, const std::string& value, std::size_t max) {
|
|
text(what, value, max);
|
|
if (value.rfind("https://", 0) != 0 || value.size() < 12 || value.find(' ') != std::string::npos)
|
|
fail(what + " must be an https:// URL");
|
|
}
|
|
|
|
void Validate::confirmed(const std::string& action, bool confirm) {
|
|
if (!confirm) fail(action + " is destructive: pass confirm=true to run it");
|
|
}
|
|
|
|
} // namespace molo::openai::admin
|