foundation_jsonschema 0.0.1

Self-contained JSON Schema validation for ewe_platform
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//! Reference resolver with base URI context and dynamic scope tracking.
//!
//! WHY: JSON Schema reference resolution depends on context: the current
//! base URI (set by `$id`), the dynamic scope (for `$dynamicRef`), and the
//! registry of known resources. `Resolver` carries all this context.
//!
//! WHAT: `Resolver` resolves `$ref` strings to concrete JSON values.
//! `Resolved` pairs the resolved value with a new resolver positioned at
//! the resolved location.
//!
//! HOW: `lookup()` parses the reference, splits it into URI + fragment,
//! looks up the resource in the registry, and traverses any JSON Pointer
//! fragment.

use alloc::string::String;
use alloc::vec::Vec;

use foundation_errstacks::IntoErrorTrace;
use serde_json::Value;

use crate::draft::Draft;
use crate::error::{ValidationError, ValidationErrorKind};

use super::anchor::Anchor;
use super::pointer;
use super::registry::Registry;
use super::resource::ResourceRef;

/// The result of resolving a reference: the JSON value plus a new resolver
/// positioned at the resolved location.
///
/// WHY: After resolving a `$ref`, the compiler needs both the target schema
/// and a resolver that can handle further references relative to that target.
#[derive(Debug)]
pub struct Resolved<'r> {
    contents: &'r Value,
    resolver: Resolver<'r>,
    draft: Draft,
}

impl<'r> Resolved<'r> {
    pub(crate) fn new(contents: &'r Value, resolver: Resolver<'r>, draft: Draft) -> Self {
        Self {
            contents,
            resolver,
            draft,
        }
    }

    /// The resolved schema contents.
    #[must_use]
    pub fn contents(&self) -> &'r Value {
        self.contents
    }

    /// The resolver positioned at the resolved location.
    #[must_use]
    pub fn resolver(&self) -> &Resolver<'r> {
        &self.resolver
    }

    /// The draft of the resolved schema.
    #[must_use]
    pub fn draft(&self) -> Draft {
        self.draft
    }

    /// Decompose into parts.
    #[must_use]
    pub fn into_inner(self) -> (&'r Value, Resolver<'r>, Draft) {
        (self.contents, self.resolver, self.draft)
    }
}

/// Reference resolver with base URI context and dynamic scope tracking.
///
/// WHY: `$ref` resolution is context-dependent — relative URIs resolve
/// against the current base URI, and `$dynamicRef` walks the dynamic scope.
///
/// WHAT: Holds a reference to the registry, the current base URI, and the
/// dynamic scope stack.
///
/// HOW: `lookup()` resolves a reference string by:
/// 1. Parsing the reference into URI + fragment
/// 2. Resolving the URI against the current base
/// 3. Looking up the resource in the registry
/// 4. Traversing any JSON Pointer or resolving any anchor in the fragment
#[derive(Clone)]
pub struct Resolver<'r> {
    registry: &'r Registry,
    base_uri: String,
    dynamic_scope: Vec<String>,
}

impl<'r> Resolver<'r> {
    /// Create a new resolver rooted at the given base URI.
    pub(crate) fn new(registry: &'r Registry, base_uri: String) -> Self {
        Self {
            registry,
            base_uri,
            dynamic_scope: Vec::new(),
        }
    }

    /// The current base URI.
    #[must_use]
    pub fn base_uri(&self) -> &str {
        &self.base_uri
    }

    /// The current dynamic scope (stack of base URIs).
    #[must_use]
    pub fn dynamic_scope(&self) -> &[String] {
        &self.dynamic_scope
    }

    /// Resolve a `$ref` string to a JSON value.
    ///
    /// WHY: This is the core resolution method used by the compiler for all
    /// `$ref` keywords.
    ///
    /// WHAT: Returns the resolved schema plus a new resolver positioned at
    /// the resolved location.
    ///
    /// HOW: Handles fragment-only references (`#/defs/Foo`), absolute URIs,
    /// relative URIs, anchor references (`#my-anchor`), and JSON Pointer
    /// fragments (`#/properties/name`).
    ///
    /// # Errors
    ///
    /// Returns `ValidationError` if the reference cannot be resolved.
    pub fn lookup(&self, reference: &str) -> Result<Resolved<'r>, ValidationError> {
        let (target_uri, fragment) = if let Some(frag) = reference.strip_prefix('#') {
            (self.base_uri.clone(), frag.to_string())
        } else if let Some((uri_part, frag)) = reference.rsplit_once('#') {
            let resolved = self
                .registry
                .resolve_uri(&self.base_uri, uri_part)
                .map_err(|e| {
                    ValidationErrorKind::Schema {
                        reason: alloc::format!("failed to resolve URI '{reference}': {e}"),
                    }
                    .into_error_trace()
                })?;
            (resolved, frag.to_string())
        } else {
            let resolved = self
                .registry
                .resolve_uri(&self.base_uri, reference)
                .map_err(|e| {
                    ValidationErrorKind::Schema {
                        reason: alloc::format!("failed to resolve URI '{reference}': {e}"),
                    }
                    .into_error_trace()
                })?;
            (resolved, String::new())
        };

        let resource = self.registry.get_resource(&target_uri).ok_or_else(|| {
            ValidationErrorKind::Schema {
                reason: alloc::format!("resource '{target_uri}' not found in registry"),
            }
            .into_error_trace()
        })?;

        // JSON Pointer fragment
        if fragment.starts_with('/') {
            let target = pointer::resolve_pointer(resource.contents(), &fragment).map_err(|e| {
                ValidationErrorKind::Schema {
                    reason: alloc::format!("pointer resolution failed: {e}"),
                }
                .into_error_trace()
            })?;
            let resolver = self.evolve(&target_uri);
            return Ok(Resolved::new(target, resolver, resource.draft()));
        }

        // Anchor fragment
        if !fragment.is_empty() {
            if fragment.contains('/') {
                return Err(ValidationErrorKind::Schema {
                    reason: alloc::format!("anchor '{fragment}' is invalid"),
                }
                .into_error_trace());
            }

            let anchor = self
                .registry
                .get_anchor(&target_uri, &fragment)
                .ok_or_else(|| {
                    ValidationErrorKind::Schema {
                        reason: alloc::format!("anchor '{fragment}' does not exist"),
                    }
                    .into_error_trace()
                })?;

            let resolver = self.evolve(&target_uri);
            return match anchor {
                Anchor::Default { resource: r, .. } => {
                    Ok(Resolved::new(r.contents(), resolver, r.draft()))
                }
                Anchor::Dynamic { resource: r, name } => {
                    // Walk dynamic scope for outermost matching dynamic anchor
                    let mut last_resource = r;
                    for scope_uri in &self.dynamic_scope {
                        if let Some(Anchor::Dynamic {
                            resource: scoped_r, ..
                        }) = self.registry.get_anchor(scope_uri, name)
                        {
                            last_resource = scoped_r;
                        }
                    }
                    Ok(Resolved::new(
                        last_resource.contents(),
                        resolver,
                        last_resource.draft(),
                    ))
                }
            };
        }

        // No fragment — return the resource itself
        let resolver = self.evolve(&target_uri);
        Ok(Resolved::new(
            resource.contents(),
            resolver,
            resource.draft(),
        ))
    }

    /// Resolve `$recursiveRef` (Draft 2019-09).
    ///
    /// WHY: `$recursiveRef` walks the dynamic scope looking for schemas with
    /// `$recursiveAnchor: true`, stopping at the outermost one.
    ///
    /// # Errors
    ///
    /// Returns `ValidationError` if resolution fails.
    pub fn lookup_recursive_ref(&self) -> Result<Resolved<'r>, ValidationError> {
        let mut resolved = self.lookup("#")?;

        if let Value::Object(obj) = resolved.contents {
            if obj
                .get("$recursiveAnchor")
                .and_then(Value::as_bool)
                .unwrap_or(false)
            {
                for scope_uri in &self.dynamic_scope {
                    let next = self.lookup(scope_uri)?;
                    match next.contents {
                        Value::Object(next_obj) => {
                            if !next_obj
                                .get("$recursiveAnchor")
                                .and_then(Value::as_bool)
                                .unwrap_or(false)
                            {
                                break;
                            }
                        }
                        _ => break,
                    }
                    resolved = next;
                }
            }
        }

        Ok(resolved)
    }

    /// Create a new resolver with an updated base URI.
    ///
    /// WHY: When entering a sub-resource (via `$id`), the base URI changes.
    /// The previous base is pushed onto the dynamic scope stack.
    #[must_use]
    pub fn evolve(&self, new_base_uri: &str) -> Resolver<'r> {
        let mut dynamic_scope = self.dynamic_scope.clone();
        if !self.base_uri.is_empty() && (dynamic_scope.is_empty() || new_base_uri != self.base_uri)
        {
            dynamic_scope.push(self.base_uri.clone());
        }
        Resolver {
            registry: self.registry,
            base_uri: new_base_uri.to_string(),
            dynamic_scope,
        }
    }

    /// Create a resolver for a sub-resource.
    ///
    /// WHY: When the compiler encounters a `$id`, it needs a new resolver
    /// with the updated base URI for that sub-resource.
    ///
    /// # Errors
    ///
    /// Returns `ValidationError` if the resource ID cannot be resolved.
    pub fn in_subresource(
        &self,
        subresource: ResourceRef<'_>,
    ) -> Result<Resolver<'r>, ValidationError> {
        if let Some(id) = subresource.id() {
            let base_uri = self.registry.resolve_uri(&self.base_uri, id).map_err(|e| {
                ValidationErrorKind::Schema {
                    reason: alloc::format!("failed to resolve subresource ID '{id}': {e}"),
                }
                .into_error_trace()
            })?;
            Ok(Resolver {
                registry: self.registry,
                base_uri,
                dynamic_scope: self.dynamic_scope.clone(),
            })
        } else {
            Ok(self.clone())
        }
    }
}

impl core::fmt::Debug for Resolver<'_> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("Resolver")
            .field("base_uri", &self.base_uri)
            .field("dynamic_scope", &self.dynamic_scope)
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::referencing::registry::Registry;
    use serde_json::json;

    fn build_registry(pairs: &[(&str, Value)]) -> Registry {
        let mut builder = Registry::builder();
        for (uri, schema) in pairs {
            builder = builder.add_resource(*uri, schema.clone());
        }
        builder.build().unwrap()
    }

    #[test]
    fn lookup_self_ref() {
        let registry = build_registry(&[("http://example.com", json!({"type": "object"}))]);
        let resolver = registry.resolver("http://example.com");
        let resolved = resolver.lookup("#").unwrap();
        assert_eq!(resolved.contents(), &json!({"type": "object"}));
    }

    #[test]
    fn lookup_empty_ref() {
        let registry = build_registry(&[("http://example.com", json!({"type": "object"}))]);
        let resolver = registry.resolver("http://example.com");
        let resolved = resolver.lookup("").unwrap();
        assert_eq!(resolved.contents(), &json!({"type": "object"}));
    }

    #[test]
    fn lookup_pointer_ref() {
        let registry = build_registry(&[(
            "http://example.com",
            json!({
                "$defs": {
                    "foo": {"type": "string"}
                }
            }),
        )]);
        let resolver = registry.resolver("http://example.com");
        let resolved = resolver.lookup("#/$defs/foo").unwrap();
        assert_eq!(resolved.contents(), &json!({"type": "string"}));
    }

    #[test]
    fn lookup_absolute_uri() {
        let registry = build_registry(&[
            ("http://example.com/a", json!({"type": "string"})),
            ("http://example.com/b", json!({"type": "integer"})),
        ]);
        let resolver = registry.resolver("http://example.com/a");
        let resolved = resolver.lookup("http://example.com/b").unwrap();
        assert_eq!(resolved.contents(), &json!({"type": "integer"}));
    }

    #[test]
    fn lookup_missing_resource() {
        let registry = build_registry(&[("http://example.com", json!({"type": "string"}))]);
        let resolver = registry.resolver("http://example.com");
        let err = resolver.lookup("http://missing.com").unwrap_err();
        let msg = alloc::format!("{}", err);
        assert!(msg.contains("not found"));
    }

    #[test]
    fn lookup_missing_anchor() {
        let registry = build_registry(&[("http://example.com", json!({"type": "string"}))]);
        let resolver = registry.resolver("http://example.com");
        let err = resolver.lookup("#noSuchAnchor").unwrap_err();
        let msg = alloc::format!("{}", err);
        assert!(msg.contains("does not exist"));
    }

    #[test]
    fn evolve_pushes_scope() {
        let registry = build_registry(&[("http://example.com", json!({"type": "string"}))]);
        let resolver = registry.resolver("http://example.com");
        assert!(resolver.dynamic_scope().is_empty());

        let evolved = resolver.evolve("http://example.com/sub");
        assert_eq!(evolved.base_uri(), "http://example.com/sub");
        assert_eq!(evolved.dynamic_scope(), &["http://example.com"]);
    }

    #[test]
    fn recursive_ref_trivial() {
        let registry = build_registry(&[(
            "http://example.com",
            json!({"$recursiveAnchor": true, "type": "object"}),
        )]);
        let resolver = registry.resolver("http://example.com");
        let first = resolver.lookup("").unwrap();
        let resolved = first.resolver().lookup_recursive_ref().unwrap();
        assert_eq!(
            resolved.contents(),
            &json!({"$recursiveAnchor": true, "type": "object"})
        );
    }
}