1use std::collections::HashSet;
5
6use serde::{Deserialize, Serialize};
7use serde_json::{json, Map, Value};
8
9use super::policy::matches_pattern;
10use super::query::parse_records_json;
11use super::record_store::RECORDS_SCHEME;
12use super::records::{is_record_kind, validate_record_schema};
13use super::sources::parse_source;
14use super::exposes::{validate_package_exposes, validate_upstreams};
15use super::tools::validate_tools;
16use crate::catalog::{A2UI_BASIC_CATALOG_ID, A2UI_VERSION, CORE_CATALOG_ID, CORE_LITE_CATALOG_ID};
17use crate::types::ExtensionDef;
18
19pub const PACKAGE_FORMAT_VERSION: u32 = 3;
24pub const EXTENSION_VERSION_PATTERN: &str = "^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)$";
26
27#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
29pub struct AvailableExtension {
30 pub id: String,
31 #[serde(default, skip_serializing_if = "Option::is_none")]
33 pub version: Option<String>,
34 #[serde(default, skip_serializing_if = "Option::is_none")]
36 pub components: Option<Vec<String>>,
37}
38
39pub fn available_extensions(defs: &[ExtensionDef]) -> Vec<AvailableExtension> {
43 defs.iter()
44 .map(|d| AvailableExtension {
45 id: d.id.clone(),
46 version: d.version.clone().filter(|v| !v.is_empty()),
47 components: Some(d.components.keys().chain(d.macros.iter().flat_map(|m| m.keys())).cloned().collect()),
48 })
49 .collect()
50}
51
52fn semver(v: &str) -> Option<[f64; 3]> {
55 let mut parts = v.split('.');
56 let mut out = [0.0; 3];
57 for slot in &mut out {
58 let p = parts.next()?;
59 if p.is_empty() || !p.bytes().all(|b| b.is_ascii_digit()) || (p.len() > 1 && p.starts_with('0')) {
60 return None;
61 }
62 *slot = p.parse().ok()?;
63 }
64 parts.next().is_none().then_some(out)
65}
66
67pub fn version_satisfies(have: &str, want: &str) -> bool {
69 let (Some([h_major, h_minor, h_patch]), Some([w_major, w_minor, w_patch])) = (semver(have), semver(want)) else { return false };
70 h_major == w_major && (h_minor > w_minor || (h_minor == w_minor && h_patch >= w_patch))
71}
72
73pub fn supported_catalog_ids<S: AsRef<str>>(extension_ids: &[S]) -> Vec<String> {
76 let mut out: Vec<String> = Vec::new();
77 for id in [CORE_CATALOG_ID, CORE_LITE_CATALOG_ID, A2UI_BASIC_CATALOG_ID].into_iter().chain(extension_ids.iter().map(AsRef::as_ref)) {
78 if !out.iter().any(|x| x == id) {
79 out.push(id.to_string());
80 }
81 }
82 out
83}
84
85pub fn client_capabilities<S: AsRef<str>>(extension_ids: &[S]) -> Value {
87 json!({"v0.9": {"supportedCatalogIds": supported_catalog_ids(extension_ids)}})
88}
89
90#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
91pub struct PackageIssue {
92 pub path: String,
93 pub message: String,
94}
95
96fn issue(path: impl Into<String>, message: impl Into<String>) -> PackageIssue {
97 PackageIssue { path: path.into(), message: message.into() }
98}
99
100fn is_pointer(s: &str) -> bool {
103 if s.is_empty() {
104 return true;
105 }
106 if !s.starts_with('/') {
107 return false;
108 }
109 let mut chars = s.chars().peekable();
110 while let Some(c) = chars.next() {
111 if c == '~' && !matches!(chars.next(), Some('0' | '1')) {
112 return false;
113 }
114 }
115 true
116}
117
118pub fn validate_package<S: AsRef<str>>(pkg: &Value, catalog_ids: &[S]) -> Vec<PackageIssue> {
123 validate_package_with(pkg, catalog_ids, None)
124}
125
126pub fn validate_package_with<S: AsRef<str>>(pkg: &Value, catalog_ids: &[S], extensions: Option<&[AvailableExtension]>) -> Vec<PackageIssue> {
130 validate_package_in(pkg, catalog_ids, extensions, &crate::catalog::CatalogView::core())
131}
132
133pub fn validate_package_in<S: AsRef<str>>(pkg: &Value, catalog_ids: &[S], extensions: Option<&[AvailableExtension]>, view: &std::sync::Arc<crate::catalog::CatalogView>) -> Vec<PackageIssue> {
138 let mut issues = Vec::new();
139 let Some(p) = pkg.as_object() else { return vec![issue("", "a package is an object")] };
140 match p.get("formatVersion").map(|version| version.as_f64().filter(|v| v.fract() == 0.0 && *v >= 1.0)) {
141 None => issues.push(issue("/formatVersion", format!("formatVersion is required ({PACKAGE_FORMAT_VERSION})"))),
142 Some(None) => issues.push(issue("/formatVersion", "formatVersion is a positive integer")),
143 Some(Some(v)) if v > f64::from(PACKAGE_FORMAT_VERSION) => {
144 issues.push(issue("/formatVersion", format!("format version {} is newer than this host ({PACKAGE_FORMAT_VERSION})", crate::json::number_to_string(v))))
145 }
146 Some(Some(v)) if v < f64::from(PACKAGE_FORMAT_VERSION) => {
147 issues.push(issue("/formatVersion", format!("format version {} is no longer read: write {PACKAGE_FORMAT_VERSION}", crate::json::number_to_string(v))))
148 }
149 Some(Some(_)) => {}
150 }
151 for key in ["id", "name", "version", "catalogId"] {
152 if p.get(key).and_then(Value::as_str).is_none_or(str::is_empty) {
153 issues.push(issue(format!("/{key}"), format!("{key} is a required string")));
154 }
155 }
156 if let Some(catalog_id) = p.get("catalogId").and_then(Value::as_str).filter(|s| !s.is_empty()) {
157 if !catalog_ids.iter().any(|c| c.as_ref() == catalog_id) {
158 issues.push(issue("/catalogId", format!("unsupported catalog {catalog_id}")));
159 }
160 }
161 match p.get("templates").and_then(Value::as_object).filter(|t| !t.is_empty()) {
162 None => issues.push(issue("/templates", "templates is a non-empty object")),
163 Some(templates) => {
164 for (id, template) in sorted_entries(templates) {
167 let at = format!("/templates/{id}");
168 let Some(template) = template.as_object() else {
169 issues.push(issue(at, "a template is an object"));
170 continue;
171 };
172 match template.get("components").and_then(Value::as_array).filter(|c| !c.is_empty()) {
173 None => issues.push(issue(format!("{at}/components"), "components is a non-empty array")),
174 Some(components) => {
175 let mut ids: HashSet<&str> = HashSet::new();
176 for (i, c) in components.iter().enumerate() {
177 let id = c.as_object().filter(|o| o.get("component").is_some_and(Value::is_string)).and_then(|o| o.get("id")).and_then(Value::as_str);
178 match id {
179 None => issues.push(issue(format!("{at}/components/{i}"), "a component needs an id and a component")),
180 Some(id) if ids.contains(id) => issues.push(issue(format!("{at}/components/{i}/id"), format!("duplicate id {id}"))),
181 Some(id) => {
182 ids.insert(id);
183 }
184 }
185 }
186 if !ids.contains("root") {
187 issues.push(issue(format!("{at}/components"), "no component has the id root"));
188 }
189 }
190 }
191 if let Some(bindings) = template.get("bindings") {
192 match bindings.as_array() {
193 None => issues.push(issue(format!("{at}/bindings"), "bindings is an array")),
194 Some(list) => {
195 for (i, b) in list.iter().enumerate() {
196 let bp = format!("{at}/bindings/{i}");
197 let b = b.as_object();
198 if b.and_then(|o| o.get("path")).and_then(Value::as_str).is_none_or(|p| !is_pointer(p)) {
199 issues.push(issue(format!("{bp}/path"), "path is a JSON pointer"));
200 }
201 if b.and_then(|o| o.get("source")).and_then(Value::as_str).is_none_or(|s| parse_source(s).is_none()) {
202 issues.push(issue(format!("{bp}/source"), "source is a scheme:name URI"));
203 }
204 }
205 }
206 }
207 }
208 }
209 }
210 }
211 if let Some(functions) = p.get("functions") {
212 let ok = functions.as_array().is_some_and(|list| list.iter().all(|f| f.as_str().is_some_and(|s| !s.is_empty())));
213 if !ok {
214 issues.push(issue("/functions", "functions is a list of names or prefix* patterns"));
215 }
216 }
217 if let Some(records) = p.get("records") {
219 match records.as_object() {
220 None => issues.push(issue("/records", "records is an object of kinds")),
221 Some(kinds) => {
222 let mut names: Vec<&String> = kinds.keys().collect();
223 names.sort();
224 for kind in names {
225 let def = &kinds[kind];
226 let at = format!("/records/{kind}");
227 if !is_record_kind(kind) {
228 issues.push(issue(&at, "a kind is lowercase letters, digits and underscores"));
229 }
230 match def.as_object() {
231 None => issues.push(issue(&at, "a kind is {schema, title?, policy?}")),
232 Some(def) => {
233 issues.extend(validate_record_schema(def.get("schema").unwrap_or(&Value::Null), &format!("{at}/schema")));
234 if let Some(policy) = def.get("policy") {
236 issues.extend(super::record_policy::validate_record_policy(policy, &format!("{at}/policy")));
237 }
238 issues.extend(super::expressions::validate_record_computed(&Value::Object(def.clone()), &at));
240 }
241 }
242 }
243 }
244 }
245 }
246 if let Some(sources) = p.get("sources") {
249 match sources.as_array().filter(|list| list.iter().all(|f| f.as_str().is_some_and(|s| !s.is_empty()))) {
250 None => issues.push(issue("/sources", "sources is a list of scheme:name sources or prefix* patterns")),
251 Some(list) => {
252 let allowed: Vec<String> = list.iter().filter_map(Value::as_str).map(str::to_string).collect();
253 for (id, template) in p.get("templates").and_then(Value::as_object).map(sorted_entries).into_iter().flatten() {
254 for (i, b) in template.get("bindings").and_then(Value::as_array).into_iter().flatten().enumerate() {
255 if let Some(source) = b.get("source").and_then(Value::as_str).filter(|s| parse_source(s).is_some()) {
256 if !source_allowed(Some(&allowed), source) {
257 issues.push(issue(format!("/templates/{id}/bindings/{i}/source"), format!("source {source} is not in the package's sources")));
258 }
259 }
260 }
261 }
262 }
263 }
264 }
265 for (id, template) in p.get("templates").and_then(Value::as_object).map(sorted_entries).into_iter().flatten() {
267 let Some(bindings) = template.as_object().and_then(|t| t.get("bindings")).and_then(Value::as_array) else { continue };
268 for (i, b) in bindings.iter().enumerate() {
269 let Some(parsed) = b.as_object().and_then(|o| o.get("source")).and_then(Value::as_str).and_then(parse_source) else { continue };
270 if parsed.scheme != RECORDS_SCHEME {
271 continue;
272 }
273 if !p.get("records").and_then(Value::as_object).is_some_and(|r| r.contains_key(&parsed.name)) {
274 issues.push(issue(format!("/templates/{id}/bindings/{i}/source"), format!("records:{} names no declared kind", parsed.name)));
275 }
276 }
277 }
278 if let Some(queries) = p.get("queries") {
281 match queries.as_object() {
282 None => issues.push(issue("/queries", "queries is an object of named queries")),
283 Some(queries) => {
284 let mut names: Vec<&String> = queries.keys().collect();
285 names.sort_by(|a, b| a.encode_utf16().cmp(b.encode_utf16()));
286 for name in names {
287 let at = format!("/queries/{name}");
288 if !is_query_name(name) {
289 issues.push(issue(at.clone(), "a query name is a letter, then letters, digits and underscores"));
290 }
291 let q = &queries[name];
292 for i in parse_records_json(q, None).1 {
293 issues.push(issue(format!("{at}{}", i.path), i.message));
294 }
295 let from = q.as_object().and_then(|o| o.get("from")).and_then(Value::as_str).map(super::js_trim).unwrap_or("");
296 if !from.is_empty() && !from.contains("${") && !p.get("records").and_then(Value::as_object).is_some_and(|r| r.contains_key(from)) {
297 issues.push(issue(format!("{at}/from"), format!("records:{from} names no declared kind")));
298 }
299 }
300 }
301 }
302 }
303 for (id, template) in p.get("templates").and_then(Value::as_object).map(sorted_entries).into_iter().flatten() {
304 let Some(bindings) = template.as_object().and_then(|t| t.get("bindings")).and_then(Value::as_array) else { continue };
305 for (i, b) in bindings.iter().enumerate() {
306 let Some(parsed) = b.as_object().and_then(|o| o.get("source")).and_then(Value::as_str).and_then(parse_source) else { continue };
307 let Some(name) = parsed.params.get("query") else { continue };
308 if parsed.scheme != RECORDS_SCHEME {
309 continue;
310 }
311 if !p.get("queries").and_then(Value::as_object).is_some_and(|q| q.contains_key(name)) {
312 issues.push(issue(format!("/templates/{id}/bindings/{i}/source"), format!("query {name} names no package query")));
313 }
314 }
315 }
316 issues.extend(super::components::validate_package_components(p, catalog_ids, view));
318 issues.extend(super::expressions::validate_package_expressions(p));
320 issues.extend(super::actions::validate_package_effects(p));
322 issues.extend(super::actions::validate_package_actions(p));
323 if let Some(tools) = p.get("tools") {
324 issues.extend(validate_tools(tools));
325 }
326 if let Some(upstreams) = p.get("upstreams") {
327 issues.extend(validate_upstreams(upstreams));
328 }
329 if let Some(exposes) = p.get("exposes") {
330 issues.extend(validate_package_exposes(exposes, p.get("records"), p.get("upstreams")));
331 }
332 if let Some(list) = p.get("extensions") {
333 issues.extend(validate_extensions(list, catalog_ids, extensions));
334 }
335 issues.extend(super::tests::validate_package_tests(p));
338 if issues.is_empty() {
339 issues.extend(super::tests::package_test_issues(pkg));
340 }
341 issues
342}
343
344fn validate_extensions<S: AsRef<str>>(list: &Value, catalog_ids: &[S], available: Option<&[AvailableExtension]>) -> Vec<PackageIssue> {
345 let Some(list) = list.as_array() else { return vec![issue("/extensions", "extensions is a list of {id, version, components?}")] };
346 let mut issues = Vec::new();
347 let mut seen: HashSet<&str> = HashSet::new();
348 for (i, ext) in list.iter().enumerate() {
349 let at = format!("/extensions/{i}");
350 let Some(ext) = ext.as_object() else {
351 issues.push(issue(at, "an extension is {id, version, components?}"));
352 continue;
353 };
354 let id = ext.get("id").and_then(Value::as_str).filter(|s| !s.is_empty());
355 if id.is_none() {
356 issues.push(issue(format!("{at}/id"), "id is a required string"));
357 }
358 let version = ext.get("version").and_then(Value::as_str).filter(|v| semver(v).is_some());
359 if version.is_none() {
360 issues.push(issue(format!("{at}/version"), "version is MAJOR.MINOR.PATCH"));
361 }
362 let components: Option<Vec<&str>> = match ext.get("components") {
363 None => Some(Vec::new()),
364 Some(c) => c.as_array().and_then(|l| l.iter().map(|c| c.as_str().filter(|s| !s.is_empty())).collect()),
365 };
366 if components.is_none() {
367 issues.push(issue(format!("{at}/components"), "components is a list of component names"));
368 }
369 let Some(id) = id else { continue };
370 if !seen.insert(id) {
371 issues.push(issue(format!("{at}/id"), format!("duplicate extension {id}")));
372 continue;
373 }
374 let have = available.and_then(|a| a.iter().find(|a| a.id == id));
375 if !catalog_ids.iter().any(|c| c.as_ref() == id) || (available.is_some() && have.is_none()) {
376 issues.push(issue(format!("{at}/id"), format!("extension {id} is not available on this host")));
377 continue;
378 }
379 let Some(have) = have else { continue };
380 if let (Some(version), Some(have_version)) = (version, have.version.as_deref()) {
381 if !version_satisfies(have_version, version) {
382 issues.push(issue(format!("{at}/version"), format!("extension {id} {have_version} does not satisfy ^{version}")));
383 }
384 }
385 if let Some(have_components) = &have.components {
386 for (j, name) in components.iter().flatten().enumerate() {
387 if !have_components.iter().any(|c| c == name) {
388 issues.push(issue(format!("{at}/components/{j}"), format!("extension {id} has no component {name}")));
389 }
390 }
391 }
392 }
393 issues
394}
395
396pub fn source_allowed(allowed: Option<&[String]>, source: &str) -> bool {
401 let Some(allowed) = allowed else { return true };
402 let Some(parsed) = parse_source(source) else { return false };
403 let name = format!("{}:{}", parsed.scheme, parsed.name);
404 allowed.iter().any(|p| matches_pattern(p, &name) || matches_pattern(p, source))
405}
406
407pub fn package_sources(pkg: &Value) -> Option<Vec<String>> {
409 pkg.get("sources").and_then(Value::as_array).map(|l| l.iter().filter_map(Value::as_str).map(str::to_string).collect())
410}
411
412pub fn template_messages(pkg: &Value, template_id: &str, surface_id: &str, data: Option<&Value>) -> Option<Vec<Value>> {
417 let template = pkg.get("templates").and_then(|t| t.get(template_id)).filter(|t| super::js_truthy(t))?;
418 let version = A2UI_VERSION;
419 let mut create = Map::new();
420 create.insert("surfaceId".into(), Value::String(surface_id.into()));
421 if let Some(catalog_id) = pkg.get("catalogId") {
422 create.insert("catalogId".into(), catalog_id.clone());
423 }
424 let mut components = Map::new();
425 components.insert("surfaceId".into(), Value::String(surface_id.into()));
426 if let Some(list) = template.get("components") {
427 components.insert("components".into(), list.clone());
428 }
429 let mut out = vec![json!({"version": version, "createSurface": create}), json!({"version": version, "updateComponents": components})];
430 if let Some(merged) = merge_data(template.get("data"), data) {
431 out.push(json!({"version": version, "updateDataModel": {"surfaceId": surface_id, "path": "/", "value": merged}}));
432 }
433 for b in template.get("bindings").and_then(Value::as_array).into_iter().flatten() {
434 let mut bind = Map::new();
435 bind.insert("surfaceId".into(), Value::String(surface_id.into()));
436 for key in ["path", "source"] {
437 if let Some(v) = b.get(key) {
438 bind.insert(key.into(), v.clone());
439 }
440 }
441 out.push(json!({"version": version, "bindDataModel": bind}));
442 }
443 Some(out)
444}
445
446pub fn merge_data(base: Option<&Value>, over: Option<&Value>) -> Option<Value> {
449 match (base, over) {
450 (base, None) => base.cloned(),
451 (Some(Value::Object(b)), Some(Value::Object(o))) => {
452 let mut merged = b.clone();
453 for (k, v) in o {
454 merged.insert(k.clone(), v.clone());
455 }
456 Some(Value::Object(merged))
457 }
458 (_, Some(over)) => Some(over.clone()),
459 }
460}
461
462pub const QUERY_NAME_PATTERN: &str = "^[a-z][A-Za-z0-9_]{0,31}$";
464
465pub fn is_query_name(name: &str) -> bool {
467 let b = name.as_bytes();
468 !b.is_empty() && b.len() <= 32 && b[0].is_ascii_lowercase() && b[1..].iter().all(|c| c.is_ascii_alphanumeric() || *c == b'_')
469}
470
471fn sorted_entries(map: &serde_json::Map<String, Value>) -> Vec<(&String, &Value)> {
474 let mut entries: Vec<(&String, &Value)> = map.iter().collect();
475 entries.sort_by(|a, b| a.0.cmp(b.0));
476 entries
477}
478