Skip to content

Commit 007a834

Browse files
committed
Disallow dot in term
1 parent d519373 commit 007a834

File tree

4 files changed

+52
-38
lines changed

4 files changed

+52
-38
lines changed

schema/v2/term.schema.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
"$schema": "https://json-schema.org/draft/2020-12/schema",
33
"$id": "https://pgxn.org/meta/v2/term.schema.json",
44
"title": "Term",
5-
"description": "A *Term* is a string that **MUST** be at least two characters long, and contain no slash (`/`), backslash (`\\`), control, or space characters.",
5+
"description": "A *Term* is a string that **MUST** be at least two characters long, and contain no slash (`/`), backslash (`\\`), dot (.), control, or space characters.",
66
"type": "string",
77
"minLength": 2,
8-
"pattern": "^[^/\\\\\\p{Space}\\p{Cntrl}]{2,}$",
8+
"pattern": "^[^./\\\\\\p{Space}\\p{Cntrl}]{2,}$",
99
"examples": ["hi", "go-on", "pg_vectorize"]
1010
}

tests/common/mod.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use std::{collections::HashMap, error::Error};
44
use boon::{Compiler, Schemas};
55
use serde_json::{json, Value};
66

7+
const SCHEMA_BASE: &str = "https://pgxn.org/meta/v";
8+
79
// https://regex101.com/r/Ly7O1x/3/
810
pub const VALID_SEMVERS: &[&str] = &[
911
"0.0.4",
@@ -81,6 +83,10 @@ pub const INVALID_SEMVERS: &[&str] = &[
8183
"99999999999999999999999.999999999999999999.99999999999999999----RC-SNAPSHOT.12.09.1--------------------------------..12",
8284
];
8385

86+
pub fn id_for(version: u8, schema: &str) -> String {
87+
format!("{SCHEMA_BASE}{version}/{schema}.schema.json")
88+
}
89+
8490
pub fn new_compiler(dir: &str) -> Result<Compiler, Box<dyn Error>> {
8591
let mut compiler = Compiler::new();
8692
compiler.enable_format_assertions();
@@ -104,19 +110,20 @@ pub fn new_compiler(dir: &str) -> Result<Compiler, Box<dyn Error>> {
104110
Ok(compiler)
105111
}
106112

107-
pub fn test_term_schema(mut compiler: Compiler, id: &str) -> Result<(), Box<dyn Error>> {
113+
pub fn test_term_schema(mut compiler: Compiler, version: u8) -> Result<(), Box<dyn Error>> {
108114
let mut schemas = Schemas::new();
109-
let idx = compiler.compile(id, &mut schemas)?;
115+
let id = id_for(version, "term");
116+
let idx = compiler.compile(&id, &mut schemas)?;
110117

111118
for valid_term in [
112119
("two chars", json!("hi")),
113120
("underscores", json!("hi_this_is_a_valid_term")),
114121
("dashes", json!("hi-this-is-a-valid-term")),
115-
("punctuation", json!("!@#$%^&*()-=+{}<>,.?")),
122+
("punctuation", json!("!@#$%^&*()-=+{}<>,?")),
116123
("unicode", json!("😀🍒📸")),
117124
] {
118125
if let Err(e) = schemas.validate(&valid_term.1, idx) {
119-
panic!("extension {} failed: {e}", valid_term.0);
126+
panic!("term {} failed: {e}", valid_term.0);
120127
}
121128
}
122129

@@ -137,12 +144,25 @@ pub fn test_term_schema(mut compiler: Compiler, id: &str) -> Result<(), Box<dyn
137144
panic!("{} unexpectedly passed!", invalid_term.0)
138145
}
139146
}
147+
148+
// Schema v1 allows a dot but v2 does not.
149+
let dot_term = json!("this.that");
150+
let res = schemas.validate(&dot_term, idx);
151+
if version == 1 {
152+
if let Err(e) = res {
153+
panic!("term with dot failed: {e}");
154+
}
155+
} else if res.is_ok() {
156+
panic!("term with dot unexpectedly passed!")
157+
}
158+
140159
Ok(())
141160
}
142161

143-
pub fn test_tags_schema(mut compiler: Compiler, id: &str) -> Result<(), Box<dyn Error>> {
162+
pub fn test_tags_schema(mut compiler: Compiler, version: u8) -> Result<(), Box<dyn Error>> {
144163
// Load the schemas and compile the tags schema.
145164
let mut schemas = Schemas::new();
165+
let id = id_for(version, "tags");
146166
let idx = compiler.compile(&id, &mut schemas)?;
147167

148168
for valid_tags in [

tests/v1_schema_test.rs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ use serde_json::{json, Map, Value};
1010
mod common;
1111
use common::*;
1212

13-
const SCHEMA_BASE: &str = "https://pgxn.org/meta/v1";
14-
const SCHEMA_ID: &str = "https://pgxn.org/meta/v1/distribution.schema.json";
15-
1613
#[test]
1714
fn test_schema_v1() -> Result<(), Box<dyn Error>> {
1815
test_schema_version(1)
@@ -30,7 +27,8 @@ fn test_corpus_v1_valid() -> Result<(), Box<dyn Error>> {
3027
// Load the schemas and compile the root schema.
3128
let mut compiler = new_compiler("schema/v1")?;
3229
let mut schemas = Schemas::new();
33-
let index = compiler.compile(SCHEMA_ID, &mut schemas)?;
30+
let id = id_for(1, "distribution");
31+
let index = compiler.compile(&id, &mut schemas)?;
3432

3533
// Test each meta JSON in the corpus.
3634
let file = File::open("tests/corpus/v1/valid.txt")?;
@@ -52,7 +50,8 @@ fn test_corpus_v1_invalid() -> Result<(), Box<dyn Error>> {
5250
// Load the schemas and compile the root schema.
5351
let mut compiler = new_compiler("schema/v1")?;
5452
let mut schemas = Schemas::new();
55-
let index = compiler.compile(SCHEMA_ID, &mut schemas)?;
53+
let id = id_for(1, "distribution");
54+
let index = compiler.compile(&id, &mut schemas)?;
5655

5756
// Test each meta JSON in the corpus.
5857
let file = File::open("tests/corpus/v1/invalid.txt")?;
@@ -76,24 +75,22 @@ fn test_corpus_v1_invalid() -> Result<(), Box<dyn Error>> {
7675
fn test_v1_term() -> Result<(), Box<dyn Error>> {
7776
// Load the schemas and compile the term schema.
7877
let compiler = new_compiler("schema/v1")?;
79-
let id = format!("{SCHEMA_BASE}/term.schema.json");
80-
test_term_schema(compiler, &id)
78+
test_term_schema(compiler, 1)
8179
}
8280

8381
#[test]
8482
fn test_v1_tags() -> Result<(), Box<dyn Error>> {
8583
// Load the schemas and compile the tags schema.
8684
let compiler = new_compiler("schema/v1")?;
87-
let id = format!("{SCHEMA_BASE}/tags.schema.json");
88-
test_tags_schema(compiler, &id)
85+
test_tags_schema(compiler, 1)
8986
}
9087

9188
#[test]
9289
fn test_v1_version() -> Result<(), Box<dyn Error>> {
9390
// Load the schemas and compile the version schema.
9491
let mut compiler = new_compiler("schema/v1")?;
9592
let mut schemas = Schemas::new();
96-
let id = format!("{SCHEMA_BASE}/version.schema.json");
93+
let id = id_for(1, "version");
9794
let idx = compiler.compile(&id, &mut schemas)?;
9895

9996
for valid_version in VALID_SEMVERS {
@@ -118,7 +115,7 @@ fn test_v1_version_range() -> Result<(), Box<dyn Error>> {
118115
// Load the schemas and compile the version_range schema.
119116
let mut compiler = new_compiler("schema/v1")?;
120117
let mut schemas = Schemas::new();
121-
let id = format!("{SCHEMA_BASE}/version_range.schema.json");
118+
let id = id_for(1, "version_range");
122119
let idx = compiler.compile(&id, &mut schemas)?;
123120

124121
for valid_version in VALID_SEMVERS {
@@ -189,7 +186,7 @@ fn test_v1_license() -> Result<(), Box<dyn Error>> {
189186
// Load the schemas and compile the license schema.
190187
let mut compiler = new_compiler("schema/v1")?;
191188
let mut schemas = Schemas::new();
192-
let id = format!("{SCHEMA_BASE}/license.schema.json");
189+
let id = id_for(1, "license");
193190
let idx = compiler.compile(&id, &mut schemas)?;
194191

195192
// Test valid license values.
@@ -257,7 +254,7 @@ fn test_v1_provides() -> Result<(), Box<dyn Error>> {
257254
// Load the schemas and compile the provides schema.
258255
let mut compiler = new_compiler("schema/v1")?;
259256
let mut schemas = Schemas::new();
260-
let id = format!("{SCHEMA_BASE}/provides.schema.json");
257+
let id = id_for(1, "provides");
261258
let idx = compiler.compile(&id, &mut schemas)?;
262259

263260
for valid_provides in [
@@ -356,7 +353,7 @@ fn test_v1_extension() -> Result<(), Box<dyn Error>> {
356353
// Load the schemas and compile the extension schema.
357354
let mut compiler = new_compiler("schema/v1")?;
358355
let mut schemas = Schemas::new();
359-
let id = format!("{SCHEMA_BASE}/extension.schema.json");
356+
let id = id_for(1, "extension");
360357
let idx = compiler.compile(&id, &mut schemas)?;
361358

362359
for valid_extension in [
@@ -532,7 +529,7 @@ fn test_v1_maintainer() -> Result<(), Box<dyn Error>> {
532529
// Load the schemas and compile the maintainer schema.
533530
let mut compiler = new_compiler("schema/v1")?;
534531
let mut schemas = Schemas::new();
535-
let id = format!("{SCHEMA_BASE}/maintainer.schema.json");
532+
let id = id_for(1, "maintainer");
536533
let idx = compiler.compile(&id, &mut schemas)?;
537534

538535
for valid_maintainer in [
@@ -581,7 +578,7 @@ fn test_v1_meta_spec() -> Result<(), Box<dyn Error>> {
581578
// Load the schemas and compile the maintainer schema.
582579
let mut compiler = new_compiler("schema/v1")?;
583580
let mut schemas = Schemas::new();
584-
let id = format!("{SCHEMA_BASE}/meta-spec.schema.json");
581+
let id = id_for(1, "meta-spec");
585582
let idx = compiler.compile(&id, &mut schemas)?;
586583

587584
for valid_meta_spec in [
@@ -635,7 +632,7 @@ fn test_v1_bugtracker() -> Result<(), Box<dyn Error>> {
635632
// Load the schemas and compile the maintainer schema.
636633
let mut compiler = new_compiler("schema/v1")?;
637634
let mut schemas = Schemas::new();
638-
let id = format!("{SCHEMA_BASE}/bugtracker.schema.json");
635+
let id = id_for(1, "bugtracker");
639636
let idx = compiler.compile(&id, &mut schemas)?;
640637

641638
for valid_bugtracker in [
@@ -689,7 +686,7 @@ fn test_v1_no_index() -> Result<(), Box<dyn Error>> {
689686
// Load the schemas and compile the maintainer schema.
690687
let mut compiler = new_compiler("schema/v1")?;
691688
let mut schemas = Schemas::new();
692-
let id = format!("{SCHEMA_BASE}/no_index.schema.json");
689+
let id = id_for(1, "no_index");
693690
let idx = compiler.compile(&id, &mut schemas)?;
694691

695692
for valid_no_index in [
@@ -754,7 +751,7 @@ fn test_v1_prereq_relationship() -> Result<(), Box<dyn Error>> {
754751
// Load the schemas and compile the maintainer schema.
755752
let mut compiler = new_compiler("schema/v1")?;
756753
let mut schemas = Schemas::new();
757-
let id = format!("{SCHEMA_BASE}/prereq_relationship.schema.json");
754+
let id = id_for(1, "prereq_relationship");
758755
let idx = compiler.compile(&id, &mut schemas)?;
759756

760757
for valid_prereq_relationship in [
@@ -807,7 +804,7 @@ fn test_v1_prereq_phase() -> Result<(), Box<dyn Error>> {
807804
// Load the schemas and compile the maintainer schema.
808805
let mut compiler = new_compiler("schema/v1")?;
809806
let mut schemas = Schemas::new();
810-
let id = format!("{SCHEMA_BASE}/prereq_phase.schema.json");
807+
let id = id_for(1, "prereq_phase");
811808
let idx = compiler.compile(&id, &mut schemas)?;
812809

813810
for valid_prereq_phase in [
@@ -919,7 +916,7 @@ fn test_v1_prereqs() -> Result<(), Box<dyn Error>> {
919916
// Load the schemas and compile the maintainer schema.
920917
let mut compiler = new_compiler("schema/v1")?;
921918
let mut schemas = Schemas::new();
922-
let id = format!("{SCHEMA_BASE}/prereqs.schema.json");
919+
let id = id_for(1, "prereqs");
923920
let idx = compiler.compile(&id, &mut schemas)?;
924921

925922
for valid_prereqs in [
@@ -1067,7 +1064,7 @@ fn test_v1_repository() -> Result<(), Box<dyn Error>> {
10671064
// Load the schemas and compile the repository schema.
10681065
let mut compiler = new_compiler("schema/v1")?;
10691066
let mut schemas = Schemas::new();
1070-
let id = format!("{SCHEMA_BASE}/repository.schema.json");
1067+
let id = id_for(1, "repository");
10711068
let idx = compiler.compile(&id, &mut schemas)?;
10721069

10731070
for valid_repository in [
@@ -1144,7 +1141,7 @@ fn test_v1_resources() -> Result<(), Box<dyn Error>> {
11441141
// Load the schemas and compile the resources schema.
11451142
let mut compiler = new_compiler("schema/v1")?;
11461143
let mut schemas = Schemas::new();
1147-
let id = format!("{SCHEMA_BASE}/resources.schema.json");
1144+
let id = id_for(1, "resources");
11481145
let idx = compiler.compile(&id, &mut schemas)?;
11491146

11501147
for valid_resources in [
@@ -1281,7 +1278,8 @@ fn test_v1_distribution() -> Result<(), Box<dyn Error>> {
12811278
// Load the schemas and compile the distribution schema.
12821279
let mut compiler = new_compiler("schema/v1")?;
12831280
let mut schemas = Schemas::new();
1284-
let idx = compiler.compile(SCHEMA_ID, &mut schemas)?;
1281+
let id = id_for(1, "distribution");
1282+
let idx = compiler.compile(&id, &mut schemas)?;
12851283

12861284
// Make sure the valid distribution is in fact valid.
12871285
let meta = valid_distribution();

tests/v2_schema_test.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ use serde_json::json;
77
mod common;
88
use common::*;
99

10-
const SCHEMA_BASE: &str = "https://pgxn.org/meta/v2";
11-
1210
#[test]
1311
fn test_schema_v1() -> Result<(), Box<dyn Error>> {
1412
test_schema_version(2)
@@ -18,24 +16,22 @@ fn test_schema_v1() -> Result<(), Box<dyn Error>> {
1816
fn test_v2_term() -> Result<(), Box<dyn Error>> {
1917
// Load the schemas and compile the term schema.
2018
let compiler = new_compiler("schema/v2")?;
21-
let id = format!("{SCHEMA_BASE}/term.schema.json");
22-
test_term_schema(compiler, &id)
19+
test_term_schema(compiler, 2)
2320
}
2421

2522
#[test]
2623
fn test_v2_tags() -> Result<(), Box<dyn Error>> {
2724
// Load the schemas and compile the tags schema.
2825
let compiler = new_compiler("schema/v2")?;
29-
let id = format!("{SCHEMA_BASE}/tags.schema.json");
30-
test_tags_schema(compiler, &id)
26+
test_tags_schema(compiler, 2)
3127
}
3228

3329
#[test]
3430
fn test_v2_semver() -> Result<(), Box<dyn Error>> {
3531
// Load the schemas and compile the semver schema.
3632
let mut compiler = new_compiler("schema/v2")?;
3733
let mut schemas = Schemas::new();
38-
let id = format!("{SCHEMA_BASE}/semver.schema.json");
34+
let id = id_for(2, "semver");
3935
let idx = compiler.compile(&id, &mut schemas)?;
4036

4137
for valid_version in VALID_SEMVERS {

0 commit comments

Comments
 (0)