Skip to content

Commit 8e2d49d

Browse files
committed
Add Path schema
1 parent 11845c8 commit 8e2d49d

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

schema/v2/path.schema.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2020-12/schema",
3+
"$id": "https://pgxn.org/meta/v2/path.schema.json",
4+
"title": "Path",
5+
"description": "A *Path* is a string with a relative file path that identifies a file in the Distribution. The path **MUST** be specified with Unix conventions.",
6+
"type": "string",
7+
"minLength": 2,
8+
"pattern": "^(?:[^/\\\\]|\\\\\\\\)(?:[^\\\\]|\\\\\\\\)+$",
9+
"$comment": "https://regex101.com/r/d49AVj",
10+
"examples": [".git", "src/pair.c", "doc/pair.md"]
11+
}

tests/v2_schema_test.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use common::*;
1010
const SCHEMA_VERSION: u8 = 2;
1111

1212
#[test]
13-
fn test_schema_v1() -> Result<(), Box<dyn Error>> {
13+
fn test_schema_v2() -> Result<(), Box<dyn Error>> {
1414
test_schema_version(SCHEMA_VERSION)
1515
}
1616

@@ -57,3 +57,40 @@ fn test_v2_semver() -> Result<(), Box<dyn Error>> {
5757

5858
Ok(())
5959
}
60+
61+
#[test]
62+
fn test_v2_path() -> Result<(), Box<dyn Error>> {
63+
// Load the schemas and compile the semver schema.
64+
let mut compiler = new_compiler("schema/v2")?;
65+
let mut schemas = Schemas::new();
66+
let id = id_for(SCHEMA_VERSION, "path");
67+
let idx = compiler.compile(&id, &mut schemas)?;
68+
69+
// Test valid relative paths.
70+
for valid_path in [
71+
json!("README.txt"),
72+
json!(".git"),
73+
json!("src/pair.c"),
74+
json!(".github/workflows/"),
75+
] {
76+
if let Err(e) = schemas.validate(&valid_path, idx) {
77+
panic!("path {} failed: {e}", valid_path);
78+
}
79+
}
80+
81+
// Test invalid paths.
82+
for invalid_path in [
83+
json!("\\foo.md"),
84+
json!("this\\and\\that.txt"),
85+
json!("/absolute/path"),
86+
// ECMA supports look-ahead, but the regex crate does not.
87+
// https://github.com/santhosh-tekuri/boon/issues/19
88+
// json!("../outside/path"),
89+
// json!("thing/../other"),
90+
] {
91+
if schemas.validate(&invalid_path, idx).is_ok() {
92+
panic!("{} unexpectedly passed!", invalid_path)
93+
}
94+
}
95+
Ok(())
96+
}

0 commit comments

Comments
 (0)