Skip to content

Commit 5b3d714

Browse files
committed
Add platform schema
1 parent 639cee5 commit 5b3d714

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

schema/v2/platform.schema.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2020-12/schema",
3+
"$id": "https://pgxn.org/meta/v2/platform.schema.json",
4+
"title": "Platform",
5+
"description": "A *Platform* identifies a computing platform as a one to three dash-delimited substrings: An OS name, the OS version, and the architecture: `$os-$version-$architecture`.\n\nIf the string contains no dash, it represents only the OS. If it contains a single dash, the first value represents the OS and second value is a version if it starts with an integer followed by a dot and the architecture if it does not start with a digit.",
6+
"type": "string",
7+
"pattern": "^(?:(any)|([a-zA-Z][a-zA-Z0-9]+)(?:-(?:0|[1-9]\\d*)[.][^\\s-]+[^-\\s]*)?(?:-([a-zA-Z0-9]+))?)$",
8+
"$comment": "https://regex101.com/r/vJv9cK",
9+
"examples": [
10+
"any",
11+
"linux",
12+
"gnulinux",
13+
"musllinux",
14+
"linux-amd64",
15+
"gnulinux-amd64",
16+
"musllinux-1.2",
17+
"musllinux-1.2-arm64",
18+
"darwin",
19+
"darwin-arm64",
20+
"darwin-23.5.0",
21+
"darwin-23.5.0-arm64"
22+
]
23+
}

tests/v2_schema_test.rs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,87 @@ fn test_v2_purl() -> Result<(), Box<dyn Error>> {
280280
}
281281
Ok(())
282282
}
283+
284+
#[test]
285+
fn test_v2_platform() -> Result<(), Box<dyn Error>> {
286+
// Load the schemas and compile the semver schema.
287+
let mut compiler = new_compiler("schema/v2")?;
288+
let mut schemas = Schemas::new();
289+
let id = id_for(SCHEMA_VERSION, "platform");
290+
let idx = compiler.compile(&id, &mut schemas)?;
291+
292+
// Test valid relative platforms.
293+
for os in [
294+
"any",
295+
"linux",
296+
"darwin",
297+
"freebsd",
298+
"openbsd",
299+
"android",
300+
"dragonfly",
301+
"illumos",
302+
"ios",
303+
"js",
304+
"netbsd",
305+
"plan9",
306+
"solaris",
307+
"wasip1",
308+
"windows",
309+
] {
310+
let platform = json!(os);
311+
if let Err(e) = schemas.validate(&platform, idx) {
312+
panic!("path pattern {} failed: {e}", platform);
313+
}
314+
315+
let architectures = [
316+
"386", "amd64", "arm", "arm64", "loong64", "mips", "mips64", "mips64le", "mipsle",
317+
"ppc64", "ppc64le", "riscv64", "s390x", "sparc64", "wasm", "ppc64", "arm64", "amd64",
318+
];
319+
320+
for arch in architectures {
321+
let platform = json!(format!("{os}-{arch}"));
322+
if let Err(e) = schemas.validate(&platform, idx) {
323+
panic!("path pattern {} failed: {e}", platform);
324+
}
325+
}
326+
327+
for version in [
328+
VALID_SEMVERS,
329+
&["1.0", "3.2.5", "2.1+beta", "3.14", "16.beta1", "17.+foo"],
330+
]
331+
.concat()
332+
{
333+
if version.contains('-') {
334+
continue;
335+
}
336+
let platform = json!(format!("{os}-{version}"));
337+
if let Err(e) = schemas.validate(&platform, idx) {
338+
panic!("path pattern {} failed: {e}", platform);
339+
}
340+
341+
for arch in architectures {
342+
let platform = json!(format!("{os}-{version}-{arch}"));
343+
if let Err(e) = schemas.validate(&platform, idx) {
344+
panic!("path pattern {} failed: {e}", platform);
345+
}
346+
}
347+
}
348+
}
349+
350+
// Test invalid platforms.
351+
for invalid_platform in [
352+
json!("darwin amd64"),
353+
json!("linux/amd64"),
354+
json!("darwin_23.5.0_arm64"),
355+
json!(null),
356+
json!("0"),
357+
json!(0),
358+
json!("\n\t"),
359+
json!("()"),
360+
] {
361+
if schemas.validate(&invalid_platform, idx).is_ok() {
362+
panic!("{} unexpectedly passed!", invalid_platform)
363+
}
364+
}
365+
Ok(())
366+
}

0 commit comments

Comments
 (0)