Skip to content

Commit b4d2f5d

Browse files
committed
Add v2 semver schema
1 parent e32058b commit b4d2f5d

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed

schema/v2/semver.schema.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2020-12/schema",
3+
"$id": "https://pgxn.org/meta/v2/semver.schema.json",
4+
"title": "SemVer",
5+
"description": "A *SemVer* is a [String](#string) containing a value that describes the version number of extensions or distributions, and adheres to the format of the [Semantic Versioning 2.0.0 Specification][semver] with the exception of [build metadata], which is reserved for use by downstream packaging systems.",
6+
"type": "string",
7+
"pattern": "^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?$",
8+
"$comment": "https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string",
9+
"examples": [
10+
"1.3.6",
11+
"10.20.30",
12+
"1.0.0-alpha",
13+
"1.0.0-alpha-a.b-c-something-long+build.1-aef.1-its-okay"
14+
]
15+
}

tests/v2_schema_test.rs

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
use std::error::Error;
2+
use std::fs::{self, File};
3+
4+
use boon::{Compiler, Schemas};
5+
use serde_json::{json, Value};
6+
7+
const SCHEMA_BASE: &str = "https://pgxn.org/meta/v2";
8+
// const SCHEMA_ID: &str = "https://pgxn.org/meta/v2/distribution.schema.json";
9+
10+
fn new_compiler(dir: &str) -> Result<Compiler, Box<dyn Error>> {
11+
let mut compiler = Compiler::new();
12+
compiler.enable_format_assertions();
13+
let paths = fs::read_dir(dir)?;
14+
for path in paths {
15+
let path = path?.path();
16+
let bn = path.file_name().unwrap().to_str().unwrap();
17+
if bn.ends_with(".schema.json") {
18+
let schema: Value = serde_json::from_reader(File::open(path.clone())?)?;
19+
if let Value::String(s) = &schema["$id"] {
20+
// Add the schema to the compiler.
21+
compiler.add_resource(s, schema.to_owned())?;
22+
} else {
23+
panic!("Unable to find ID in {}", path.display());
24+
}
25+
} else {
26+
println!("Skipping {}", path.display());
27+
}
28+
}
29+
30+
Ok(compiler)
31+
}
32+
33+
// https://regex101.com/r/Ly7O1x/3/
34+
const VALID_VERSIONS: &[&str] = &[
35+
"0.0.4",
36+
"1.2.3",
37+
"10.20.30",
38+
// "1.1.2-prerelease+meta",
39+
// "1.1.2+meta",
40+
// "1.1.2+meta-valid",
41+
"1.0.0-alpha",
42+
"1.0.0-beta",
43+
"1.0.0-alpha.beta",
44+
"1.0.0-alpha.beta.1",
45+
"1.0.0-alpha.1",
46+
"1.0.0-alpha0.valid",
47+
"1.0.0-alpha.0valid",
48+
// "1.0.0-alpha-a.b-c-somethinglong+build.1-aef.1-its-okay",
49+
// "1.0.0-rc.1+build.1",
50+
// "2.0.0-rc.1+build.123",
51+
"1.2.3-beta",
52+
"10.2.3-DEV-SNAPSHOT",
53+
"1.2.3-SNAPSHOT-123",
54+
"1.0.0",
55+
"2.0.0",
56+
"1.1.7",
57+
// "2.0.0+build.1848",
58+
"2.0.1-alpha.1227",
59+
// "1.0.0-alpha+beta",
60+
// "1.2.3----RC-SNAPSHOT.12.9.1--.12+788",
61+
// "1.2.3----R-S.12.9.1--.12+meta",
62+
"1.2.3----RC-SNAPSHOT.12.9.1--.12",
63+
// "1.0.0+0.build.1-rc.10000aaa-kk-0.1",
64+
"1.0.0-0A.is.legal",
65+
];
66+
67+
const INVALID_VERSIONS: &[&str] = &[
68+
"1",
69+
"1.2",
70+
"1.2.3-0123",
71+
"1.2.3-0123.0123",
72+
"1.1.2+.123",
73+
"+invalid",
74+
"-invalid",
75+
"-invalid+invalid",
76+
"-invalid.01",
77+
"alpha",
78+
"alpha.beta",
79+
"alpha.beta.1",
80+
"alpha.1",
81+
"alpha+beta",
82+
"alpha_beta",
83+
"alpha.",
84+
"alpha..",
85+
"beta",
86+
"1.0.0-alpha_beta",
87+
"-alpha.",
88+
"1.0.0-alpha..",
89+
"1.0.0-alpha..1",
90+
"1.0.0-alpha...1",
91+
"1.0.0-alpha....1",
92+
"1.0.0-alpha.....1",
93+
"1.0.0-alpha......1",
94+
"1.0.0-alpha.......1",
95+
"01.1.1",
96+
"1.01.1",
97+
"1.1.01",
98+
"1.2",
99+
"1.2.3.DEV",
100+
"1.2-SNAPSHOT",
101+
"1.2.31.2.3----RC-SNAPSHOT.12.09.1--..12+788",
102+
"1.2-RC-SNAPSHOT",
103+
"-1.0.3-gamma+b7718",
104+
"+justmeta",
105+
"9.8.7+meta+meta",
106+
"9.8.7-whatever+meta+meta",
107+
"99999999999999999999999.999999999999999999.99999999999999999----RC-SNAPSHOT.12.09.1--------------------------------..12",
108+
109+
// metadata cases copied from above, disallowed by the spec.
110+
"1.1.2-prerelease+meta",
111+
"1.1.2+meta",
112+
"1.1.2+meta-valid",
113+
"1.0.0-alpha-a.b-c-somethinglong+build.1-aef.1-its-okay",
114+
"1.0.0-rc.1+build.1",
115+
"2.0.0-rc.1+build.123",
116+
"2.0.0+build.1848",
117+
"1.0.0-alpha+beta",
118+
"1.2.3----RC-SNAPSHOT.12.9.1--.12+788",
119+
"1.2.3----R-S.12.9.1--.12+meta",
120+
"1.0.0+0.build.1-rc.10000aaa-kk-0.1",
121+
];
122+
123+
#[test]
124+
fn test_v2_semver() -> Result<(), Box<dyn Error>> {
125+
// Load the schemas and compile the semver schema.
126+
let mut compiler = new_compiler("schema/v2")?;
127+
let mut schemas = Schemas::new();
128+
let id = format!("{SCHEMA_BASE}/semver.schema.json");
129+
let idx = compiler.compile(&id, &mut schemas)?;
130+
131+
for valid_version in VALID_VERSIONS {
132+
let vv = json!(valid_version);
133+
if let Err(e) = schemas.validate(&vv, idx) {
134+
panic!("extension {} failed: {e}", valid_version);
135+
}
136+
}
137+
138+
for invalid_version in INVALID_VERSIONS {
139+
let iv = json!(invalid_version);
140+
if schemas.validate(&iv, idx).is_ok() {
141+
panic!("{} unexpectedly passed!", invalid_version)
142+
}
143+
}
144+
145+
Ok(())
146+
}

0 commit comments

Comments
 (0)