Skip to content

Commit

Permalink
Add badge schema
Browse files Browse the repository at this point in the history
  • Loading branch information
theory committed Jul 23, 2024
1 parent 5a3f838 commit 43ee53d
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
29 changes: 29 additions & 0 deletions schema/v2/badge.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://pgxn.org/meta/v2/badge.schema.json",
"title": "Badge",
"description": "*Badge* represents a link to a [Shields](https://github.com/badges/shields/blob/master/spec/SPECIFICATION.md \"Shields badge specification\")-conformant badge.",
"type": "object",
"properties": {
"src": {
"type": "string",
"format": "uri",
"description": "The URI for the badge."
},
"alt": {
"type": "string",
"minLength": 4,
"maxLength": 4048,
"description": "Alternate text for accessability."
}
},
"required": ["src", "alt"],
"patternProperties": { "^[xX]_.": { "description": "Custom key" } },
"additionalProperties": false,
"examples": [
{
"alt": "Test Status",
"src": "https://test.packages.postgresql.org/github.com/example/pair.svg"
}
]
}
69 changes: 68 additions & 1 deletion tests/v2_schema_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1748,7 +1748,7 @@ fn test_v2_pipeline() -> Result<(), Box<dyn Error>> {
for invalid in [
json!("vroom"),
json!("🎃🎃"),
json!("pgsx"),
json!("pgx"),
json!(""),
json!(true),
json!(false),
Expand Down Expand Up @@ -2147,3 +2147,70 @@ fn test_v2_variations() -> Result<(), Box<dyn Error>> {

Ok(())
}

#[test]
fn test_v2_badge() -> Result<(), Box<dyn Error>> {
// Load the schemas and compile the maintainer schema.
let mut compiler = new_compiler("schema/v2")?;
let mut schemas = Schemas::new();
let id = id_for(SCHEMA_VERSION, "badge");
let idx = compiler.compile(&id, &mut schemas)?;

for valid in [
("short", json!({"src": "x:y", "alt": "food"})),
(
"long",
json!({
"src": "https://github.com/theory/kv-pair/workflows/CI/badge.svg",
"alt": "CI/CD Test Status",
}),
),
("custom x_", json!({"src": "x:y", "alt": "food", "x_y": 1})),
(
"custom X_",
json!({"src": "x:y", "alt": "food", "X_z": true}),
),
] {
if let Err(e) = schemas.validate(&valid.1, idx) {
panic!("extension {} failed: {e}", valid.0);
}
}

for invalid in [
("array", json!([])),
("string", json!("web")),
("empty string", json!("")),
("true", json!(true)),
("false", json!(false)),
("null", json!(null)),
("empty object", json!({})),
("only x_", json!({"x_y": 0})),
("only X_", json!({"X_y": 0})),
("bare x_", json!({"src": "x:y", "alt": "food", "x_": 0})),
("bare X_", json!({"src": "x:y", "alt": "food", "x_": 0})),
("unknown", json!({"src": "x:y", "alt": "food", "foo": 0})),
// src
("src array", json!({"src": []})),
("src object", json!({"src": {}})),
("src empty", json!({"src": ""})),
("src bool", json!({"src": true})),
("src number", json!({"src": 42})),
("src null", json!({"src": null})),
("src invalid", json!({"src": "xyz"})),
("src invalid", json!({"src": "not a uri"})),
// alt
("alt array", json!({"alt": []})),
("alt object", json!({"alt": {}})),
("alt empty", json!({"alt": ""})),
("alt bool", json!({"alt": true})),
("alt number", json!({"alt": 42})),
("alt null", json!({"alt": null})),
("alt too short", json!({"alt": ["xyz"]})),
] {
if schemas.validate(&invalid.1, idx).is_ok() {
panic!("{} unexpectedly passed!", invalid.0)
}
}

Ok(())
}

0 comments on commit 43ee53d

Please sign in to comment.