Add JSON Schema support (#3046)

## Summary

This PR adds JSON Schema support. The setup mirrors Ruff's own.
This commit is contained in:
Charlie Marsh 2024-04-17 13:24:41 -04:00 committed by GitHub
parent 7c5b13c412
commit 7fb2bf816f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
31 changed files with 818 additions and 26 deletions

View file

@ -22,6 +22,7 @@ anyhow = { workspace = true }
clap = { workspace = true, features = ["derive"], optional = true }
itertools = { workspace = true }
rustc-hash = { workspace = true }
schemars = { workspace = true, optional = true }
serde = { workspace = true, optional = true }
serde_json = { workspace = true, optional = true }

View file

@ -8,6 +8,7 @@ use uv_auth::{self, KeyringProvider};
feature = "serde",
serde(deny_unknown_fields, rename_all = "kebab-case")
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub enum KeyringProviderType {
/// Do not use keyring for credential lookup.
#[default]

View file

@ -203,6 +203,7 @@ impl NoBuild {
feature = "serde",
serde(deny_unknown_fields, rename_all = "kebab-case")
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub enum IndexStrategy {
/// Only use results from the first index that returns a match for a given package name.
///

View file

@ -28,6 +28,7 @@ impl FromStr for ConfigSettingEntry {
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
enum ConfigSettingValue {
/// The value consists of a single string.
String(String),
@ -82,6 +83,7 @@ impl<'de> serde::Deserialize<'de> for ConfigSettingValue {
///
/// See: <https://peps.python.org/pep-0517/#config-settings>
#[derive(Debug, Default, Clone)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(not(feature = "serde"), allow(dead_code))]
pub struct ConfigSettings(BTreeMap<String, ConfigSettingValue>);

View file

@ -59,6 +59,29 @@ impl<'de> serde::Deserialize<'de> for PackageNameSpecifier {
}
}
#[cfg(feature = "schemars")]
impl schemars::JsonSchema for PackageNameSpecifier {
fn schema_name() -> String {
"PackageNameSpecifier".to_string()
}
fn json_schema(_gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
schemars::schema::SchemaObject {
instance_type: Some(schemars::schema::InstanceType::String.into()),
string: Some(Box::new(schemars::schema::StringValidation {
// See: https://packaging.python.org/en/latest/specifications/name-normalization/#name-format
pattern: Some(
r"^(:none:|:all:|([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9._-]*[a-zA-Z0-9]))$"
.to_string(),
),
..schemars::schema::StringValidation::default()
})),
..schemars::schema::SchemaObject::default()
}
.into()
}
}
/// Package name specification.
///
/// Consumes both package names and selection directives for compatibility with pip flags