-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathvalidator.rs
More file actions
403 lines (368 loc) · 12.4 KB
/
validator.rs
File metadata and controls
403 lines (368 loc) · 12.4 KB
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
use crate::{Error, Result, Type, Version};
use fluent_uri::Uri;
use jsonschema::{Resource, Retrieve, ValidationOptions, Validator as JsonschemaValidator};
use reqwest::blocking::Client;
use serde::Serialize;
use serde_json::{Map, Value};
use std::collections::HashMap;
const SCHEMA_BASE: &str = "https://schemas.stacspec.org";
/// A structure for validating STAC.
#[derive(Debug)]
pub struct Validator {
validators: HashMap<Uri<String>, JsonschemaValidator>,
validation_options: ValidationOptions,
}
#[derive(Debug)]
struct Retriever(Client);
impl Validator {
/// Creates a new validator.
///
/// # Examples
///
/// ```
/// use stac::Validator;
///
/// let validator = Validator::new().unwrap();
/// ```
pub fn new() -> Result<Validator> {
let validation_options = jsonschema::options();
let validation_options = validation_options
.with_resources(prebuild_resources().into_iter())
.with_retriever(Retriever(
Client::builder().user_agent(crate::user_agent()).build()?,
));
Ok(Validator {
validators: prebuild_validators(&validation_options),
validation_options,
})
}
/// Validates a single value.
///
/// # Examples
///
/// ```
/// use stac::{Item, Validator};
///
/// let item = Item::new("an-id");
/// let mut validator = Validator::new().unwrap();
/// validator.validate(&item).unwrap();
/// ```
pub fn validate<T>(&mut self, value: &T) -> Result<()>
where
T: Serialize,
{
let value = serde_json::to_value(value)?;
let _ = self.validate_value(value)?;
Ok(())
}
/// If you have a [serde_json::Value], you can skip a deserialization step by using this method.
pub fn validate_value(&mut self, value: Value) -> Result<Value> {
if let Value::Object(object) = value {
self.validate_object(object).map(Value::Object)
} else if let Value::Array(array) = value {
self.validate_array(array).map(Value::Array)
} else {
Err(Error::ScalarJson(value))
}
}
fn validate_array(&mut self, array: Vec<Value>) -> Result<Vec<Value>> {
let mut errors = Vec::new();
let mut new_array = Vec::with_capacity(array.len());
for value in array {
match self.validate_value(value) {
Ok(value) => new_array.push(value),
Err(error) => {
if let Error::Validation(e) = error {
errors.extend(e);
} else {
return Err(error);
}
}
}
}
if errors.is_empty() {
Ok(new_array)
} else {
Err(Error::Validation(errors))
}
}
fn validate_object(&mut self, mut object: Map<String, Value>) -> Result<Map<String, Value>> {
let r#type = if let Some(r#type) = object.get("type").and_then(|v| v.as_str()) {
let r#type: Type = r#type.parse()?;
if r#type == Type::ItemCollection {
if let Some(features) = object.remove("features") {
let features = self.validate_value(features)?;
let _ = object.insert("features".to_string(), features);
}
return Ok(object);
}
r#type
} else if let Some(collections) = object.remove("collections") {
let collections = self.validate_value(collections)?;
let _ = object.insert("collections".to_string(), collections);
return Ok(object);
} else {
return Err(Error::MissingField("type"));
};
let version: Version = object
.get("stac_version")
.and_then(|v| v.as_str())
.map(|v| v.parse::<Version>())
.transpose()
.unwrap()
.ok_or(Error::MissingField("stac_version"))?;
let uri = build_uri(r#type, &version);
let validator = self.validator(uri)?;
let value = Value::Object(object);
let errors: Vec<_> = validator.iter_errors(&value).collect();
let object = if errors.is_empty() {
if let Value::Object(object) = value {
object
} else {
unreachable!()
}
} else {
return Err(Error::from_validation_errors(
errors.into_iter(),
Some(&value),
));
};
self.validate_extensions(object)
}
fn validate_extensions(&mut self, object: Map<String, Value>) -> Result<Map<String, Value>> {
if let Some(stac_extensions) = object
.get("stac_extensions")
.and_then(|value| value.as_array())
.cloned()
{
let uris = stac_extensions
.into_iter()
.filter_map(|value| {
if let Value::String(s) = value {
Some(Uri::parse(s))
} else {
None
}
})
.collect::<std::result::Result<Vec<_>, _>>()?;
self.ensure_validators(&uris)?;
let mut errors = Vec::new();
let value = Value::Object(object);
for uri in uris {
let validator = self
.validator_opt(&uri)
.expect("We already ensured they're present");
errors.extend(validator.iter_errors(&value));
}
if errors.is_empty() {
if let Value::Object(object) = value {
Ok(object)
} else {
unreachable!()
}
} else {
Err(Error::from_validation_errors(
errors.into_iter(),
Some(&value),
))
}
} else {
Ok(object)
}
}
fn validator(&mut self, uri: Uri<String>) -> Result<&JsonschemaValidator> {
self.ensure_validator(&uri)?;
Ok(self.validator_opt(&uri).unwrap())
}
fn ensure_validators(&mut self, uris: &[Uri<String>]) -> Result<()> {
for uri in uris {
self.ensure_validator(uri)?;
}
Ok(())
}
fn ensure_validator(&mut self, uri: &Uri<String>) -> Result<()> {
if !self.validators.contains_key(uri) {
let response = reqwest::blocking::get(uri.as_str())?.error_for_status()?;
let validator = self.validation_options.build(&response.json()?)?;
let _ = self.validators.insert(uri.clone(), validator);
}
Ok(())
}
fn validator_opt(&self, uri: &Uri<String>) -> Option<&JsonschemaValidator> {
self.validators.get(uri)
}
}
impl Retrieve for Retriever {
fn retrieve(
&self,
uri: &Uri<String>,
) -> std::result::Result<Value, Box<dyn std::error::Error + Send + Sync>> {
let response = self.0.get(uri.as_str()).send()?.error_for_status()?;
let value = response.json()?;
Ok(value)
}
}
fn build_uri(r#type: Type, version: &Version) -> Uri<String> {
Uri::parse(format!(
"{}{}",
SCHEMA_BASE,
r#type
.spec_path(version)
.expect("we shouldn't get here with an item collection")
))
.unwrap()
}
fn prebuild_validators(
validation_options: &ValidationOptions,
) -> HashMap<Uri<String>, JsonschemaValidator> {
use Type::*;
use Version::*;
let mut schemas = HashMap::new();
macro_rules! schema {
($t:expr, $v:expr, $path:expr, $schemas:expr) => {
let url = build_uri($t, &$v);
let value = serde_json::from_str(include_str!($path)).unwrap();
let validator = validation_options.build(&value).unwrap();
let _ = schemas.insert(url, validator);
};
}
schema!(Item, v1_0_0, "schemas/v1.0.0/item.json", schemas);
schema!(Catalog, v1_0_0, "schemas/v1.0.0/catalog.json", schemas);
schema!(
Collection,
v1_0_0,
"schemas/v1.0.0/collection.json",
schemas
);
schema!(Item, v1_1_0, "schemas/v1.1.0/item.json", schemas);
schema!(Catalog, v1_1_0, "schemas/v1.1.0/catalog.json", schemas);
schema!(
Collection,
v1_1_0,
"schemas/v1.1.0/collection.json",
schemas
);
schemas
}
fn prebuild_resources() -> Vec<(String, Resource)> {
let mut resources = Vec::new();
macro_rules! resolve {
($url:expr, $path:expr) => {
let _ = resources.push((
$url.to_string(),
Resource::from_contents(serde_json::from_str(include_str!($path)).unwrap())
.unwrap(),
));
};
}
// General
resolve!(
"https://geojson.org/schema/Feature.json",
"schemas/geojson/Feature.json"
);
resolve!(
"https://geojson.org/schema/Geometry.json",
"schemas/geojson/Geometry.json"
);
resolve!(
"http://json-schema.org/draft-07/schema",
"schemas/json-schema/draft-07.json"
);
// STAC v1.0.0
resolve!(
"https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/basics.json",
"schemas/v1.0.0/basics.json"
);
resolve!(
"https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/datetime.json",
"schemas/v1.0.0/datetime.json"
);
resolve!(
"https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/instrument.json",
"schemas/v1.0.0/instrument.json"
);
resolve!(
"https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/item.json",
"schemas/v1.0.0/item.json"
);
resolve!(
"https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/licensing.json",
"schemas/v1.0.0/licensing.json"
);
resolve!(
"https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/provider.json",
"schemas/v1.0.0/provider.json"
);
// STAC v1.1.0
resolve!(
"https://schemas.stacspec.org/v1.1.0/item-spec/json-schema/bands.json",
"schemas/v1.1.0/bands.json"
);
resolve!(
"https://schemas.stacspec.org/v1.1.0/item-spec/json-schema/basics.json",
"schemas/v1.1.0/basics.json"
);
resolve!(
"https://schemas.stacspec.org/v1.1.0/item-spec/json-schema/common.json",
"schemas/v1.1.0/common.json"
);
resolve!(
"https://schemas.stacspec.org/v1.1.0/item-spec/json-schema/data-values.json",
"schemas/v1.1.0/data-values.json"
);
resolve!(
"https://schemas.stacspec.org/v1.1.0/item-spec/json-schema/datetime.json",
"schemas/v1.1.0/datetime.json"
);
resolve!(
"https://schemas.stacspec.org/v1.1.0/item-spec/json-schema/instrument.json",
"schemas/v1.1.0/instrument.json"
);
resolve!(
"https://schemas.stacspec.org/v1.1.0/item-spec/json-schema/item.json",
"schemas/v1.1.0/item.json"
);
resolve!(
"https://schemas.stacspec.org/v1.1.0/item-spec/json-schema/licensing.json",
"schemas/v1.1.0/licensing.json"
);
resolve!(
"https://schemas.stacspec.org/v1.1.0/item-spec/json-schema/provider.json",
"schemas/v1.1.0/provider.json"
);
resources
}
#[cfg(test)]
mod tests {
use super::Validator;
use crate::{Collection, Item, Validate};
use serde_json::json;
#[test]
fn validate_simple_item() {
let item: Item = crate::read("examples/simple-item.json").unwrap();
item.validate().unwrap();
}
#[tokio::test]
#[ignore = "can't validate in a tokio runtime yet: https://github.com/Stranger6667/jsonschema/issues/385"]
async fn validate_inside_tokio_runtime() {
let item: Item = crate::read("examples/extended-item.json").unwrap();
item.validate().unwrap();
}
#[test]
fn validate_array() {
let items: Vec<_> = (0..100)
.map(|i| Item::new(format!("item-{}", i)))
.map(|i| serde_json::to_value(i).unwrap())
.collect();
let mut validator = Validator::new().unwrap();
validator.validate(&items).unwrap();
}
#[test]
fn validate_collections() {
let collection: Collection = crate::read("examples/collection.json").unwrap();
let collections = json!({
"collections": [collection]
});
collections.validate().unwrap();
}
}