Skip to main content

jlens_schema/
lib.rs

1//! # jlens-schema
2//!
3//! Structural metadata for JSON documents, and the JSONata language service built on it.
4//!
5//! * **Inference.** [`Schema::infer`] derives a [`Schema`] from sample documents. It records
6//!   types, required and optional fields, array items, number ranges, string formats, small
7//!   enumerations and examples.
8//! * **Configuration.** [`Schema::apply_overrides`] adds descriptions, labels, allowed values and
9//!   hidden fields. Schemas convert to and from JSON Schema 2020-12
10//!   ([`Schema::to_json_schema`], [`Schema::from_json_schema`]), so they can be versioned and
11//!   edited by hand.
12//! * **Language service.** [`LanguageService`] offers completion, diagnostics (including
13//!   references to fields that the schema does not have), hover and signature help for JSONata
14//!   expressions. It types each part of an expression against the schema, following the
15//!   evaluator's rules for paths, filters and variables.
16//!
17//! ```
18//! use jlens_schema::{LanguageService, Schema};
19//! use serde_json::json;
20//!
21//! let schema = Schema::infer(&[json!({
22//!     "customer": {"login": "123", "name": "XYZ"},
23//!     "items": [{"code": "1", "type": "apparel", "unitPrice": 10.0, "quantity": 2}]
24//! })]);
25//! let service = LanguageService::new(Some(schema));
26//!
27//! // Fields of the items, inside a filter:
28//! let text = "items[ty";
29//! let labels: Vec<_> = service.complete(text, text.len()).items.into_iter()
30//!     .map(|i| i.label).collect();
31//! assert_eq!(labels, ["type", "$type"]); // fields first, then matching functions
32//!
33//! // A misspelt field is reported:
34//! let warnings = service.diagnostics("customer.Login");
35//! assert_eq!(warnings[0].code, "W0201");
36//! assert!(warnings[0].message.contains("did you mean `login`"));
37//! ```
38
39mod jsonschema;
40mod service;
41mod shape;
42mod typing;
43
44pub use service::{
45    CompletionItem, CompletionKind, Completions, FunctionDecl, Hover, LanguageService,
46    SignatureHelp,
47};
48pub use shape::{Field, JsonType, Override, Schema, Shape};