From 1fbd10f0e8090ab79bde834f60df9d89987c689d Mon Sep 17 00:00:00 2001 From: Jo <10510431+j178@users.noreply.github.com> Date: Mon, 20 Apr 2026 18:26:04 +0800 Subject: [PATCH] Disallow rev for non-remote repos in schema --- crates/prek/src/config.rs | 10 --- crates/prek/src/schema.rs | 149 ++++++++++++++++++++++++++++---------- prek.schema.json | 9 ++- 3 files changed, 117 insertions(+), 51 deletions(-) diff --git a/crates/prek/src/config.rs b/crates/prek/src/config.rs index eff0647a9..b4200e37f 100644 --- a/crates/prek/src/config.rs +++ b/crates/prek/src/config.rs @@ -16,8 +16,6 @@ use strum::EnumCount; use crate::fs::Simplified; use crate::install_source::InstallSource; -#[cfg(feature = "schemars")] -use crate::schema::{schema_repo_builtin, schema_repo_local, schema_repo_meta, schema_repo_remote}; use crate::version; use crate::warn_user; use crate::warn_user_once; @@ -845,9 +843,7 @@ impl TryFrom for BuiltinHook { } #[derive(Debug, Clone, Serialize, Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] pub(crate) struct RemoteRepo { - #[cfg_attr(feature = "schemars", schemars(schema_with = "schema_repo_remote"))] pub repo: String, pub rev: String, #[serde(skip_serializing)] @@ -890,9 +886,7 @@ impl Display for RemoteRepo { } #[derive(Debug, Clone, Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] pub(crate) struct LocalRepo { - #[cfg_attr(feature = "schemars", schemars(schema_with = "schema_repo_local"))] pub repo: String, pub hooks: Vec, @@ -907,9 +901,7 @@ impl Display for LocalRepo { } #[derive(Debug, Clone, Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] pub(crate) struct MetaRepo { - #[cfg_attr(feature = "schemars", schemars(schema_with = "schema_repo_meta"))] pub repo: String, pub hooks: Vec, @@ -924,9 +916,7 @@ impl Display for MetaRepo { } #[derive(Debug, Clone, Deserialize)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] pub(crate) struct BuiltinRepo { - #[cfg_attr(feature = "schemars", schemars(schema_with = "schema_repo_builtin"))] pub repo: String, pub hooks: Vec, diff --git a/crates/prek/src/schema.rs b/crates/prek/src/schema.rs index b3654a453..df96d84f1 100644 --- a/crates/prek/src/schema.rs +++ b/crates/prek/src/schema.rs @@ -1,5 +1,5 @@ use crate::config::{ - BuiltinHook, BuiltinRepo, FilePattern, LocalRepo, MetaHook, MetaRepo, PassFilenames, + BuiltinHook, BuiltinRepo, FilePattern, LocalHook, LocalRepo, MetaHook, MetaRepo, PassFilenames, RemoteHook, RemoteRepo, Repo, Stage, Stages, }; use std::borrow::Cow; @@ -235,6 +235,65 @@ fn predefined_hook_schema( schema } +impl schemars::JsonSchema for RemoteRepo { + fn schema_name() -> Cow<'static, str> { + Cow::Borrowed("RemoteRepo") + } + + fn json_schema(schema_gen: &mut schemars::generate::SchemaGenerator) -> schemars::Schema { + let hook_schema = schema_gen.subschema_for::(); + schemars::json_schema!({ + "type": "object", + "properties": { + "repo": { + "type": "string", + "not": { + "enum": ["local", "meta", "builtin"], + }, + "description": "Remote repository location. Must not be `local`, `meta`, or `builtin`.", + }, + "rev": { + "type": "string", + }, + "hooks": { + "type": "array", + "items": hook_schema, + "writeOnly": true, + }, + }, + "required": ["repo", "rev", "hooks"], + "additionalProperties": true, + }) + } +} + +impl schemars::JsonSchema for LocalRepo { + fn schema_name() -> Cow<'static, str> { + Cow::Borrowed("LocalRepo") + } + + fn json_schema(schema_gen: &mut schemars::generate::SchemaGenerator) -> schemars::Schema { + let hook_schema = schema_gen.subschema_for::(); + schemars::json_schema!({ + "type": "object", + "properties": { + "repo": { + "type": "string", + "const": "local", + "description": "Must be `local`.", + }, + "hooks": { + "type": "array", + "items": hook_schema, + }, + "rev": false, + }, + "required": ["repo", "hooks"], + "additionalProperties": true, + }) + } +} + impl schemars::JsonSchema for MetaHook { fn schema_name() -> Cow<'static, str> { Cow::Borrowed("MetaHook") @@ -248,6 +307,33 @@ impl schemars::JsonSchema for MetaHook { } } +impl schemars::JsonSchema for MetaRepo { + fn schema_name() -> Cow<'static, str> { + Cow::Borrowed("MetaRepo") + } + + fn json_schema(schema_gen: &mut schemars::generate::SchemaGenerator) -> schemars::Schema { + let hook_schema = schema_gen.subschema_for::(); + schemars::json_schema!({ + "type": "object", + "properties": { + "repo": { + "type": "string", + "const": "meta", + "description": "Must be `meta`.", + }, + "hooks": { + "type": "array", + "items": hook_schema, + }, + "rev": false, + }, + "required": ["repo", "hooks"], + "additionalProperties": true, + }) + } +} + impl schemars::JsonSchema for BuiltinHook { fn schema_name() -> Cow<'static, str> { Cow::Borrowed("BuiltinHook") @@ -261,44 +347,31 @@ impl schemars::JsonSchema for BuiltinHook { } } -pub(crate) fn schema_repo_local( - _gen: &mut schemars::generate::SchemaGenerator, -) -> schemars::Schema { - schemars::json_schema!({ - "type": "string", - "const": "local", - "description": "Must be `local`.", - }) -} - -pub(crate) fn schema_repo_meta(_gen: &mut schemars::generate::SchemaGenerator) -> schemars::Schema { - schemars::json_schema!({ - "type": "string", - "const": "meta", - "description": "Must be `meta`.", - }) -} - -pub(crate) fn schema_repo_builtin( - _gen: &mut schemars::generate::SchemaGenerator, -) -> schemars::Schema { - schemars::json_schema!({ - "type": "string", - "const": "builtin", - "description": "Must be `builtin`.", - }) -} +impl schemars::JsonSchema for BuiltinRepo { + fn schema_name() -> Cow<'static, str> { + Cow::Borrowed("BuiltinRepo") + } -pub(crate) fn schema_repo_remote( - _gen: &mut schemars::generate::SchemaGenerator, -) -> schemars::Schema { - schemars::json_schema!({ - "type": "string", - "not": { - "enum": ["local", "meta", "builtin"], - }, - "description": "Remote repository location. Must not be `local`, `meta`, or `builtin`.", - }) + fn json_schema(schema_gen: &mut schemars::generate::SchemaGenerator) -> schemars::Schema { + let hook_schema = schema_gen.subschema_for::(); + schemars::json_schema!({ + "type": "object", + "properties": { + "repo": { + "type": "string", + "const": "builtin", + "description": "Must be `builtin`.", + }, + "hooks": { + "type": "array", + "items": hook_schema, + }, + "rev": false, + }, + "required": ["repo", "hooks"], + "additionalProperties": true, + }) + } } impl schemars::JsonSchema for Repo { diff --git a/prek.schema.json b/prek.schema.json index a358e1fb7..35d080d16 100644 --- a/prek.schema.json +++ b/prek.schema.json @@ -413,7 +413,8 @@ "items": { "$ref": "#/definitions/LocalHook" } - } + }, + "rev": false }, "required": [ "repo", @@ -569,7 +570,8 @@ "items": { "$ref": "#/definitions/MetaHook" } - } + }, + "rev": false }, "required": [ "repo", @@ -732,7 +734,8 @@ "items": { "$ref": "#/definitions/BuiltinHook" } - } + }, + "rev": false }, "required": [ "repo",