Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ls/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,7 @@ pub struct MetadataValidationRule {
pub ty: Option<String>,
/// Format of the metadata entry, if type is "date".
pub format: Option<String>,
/// Regex pattern to validate the metadata entry, if type is "string".
#[serde(default)]
pub regex: Option<String>,
}
46 changes: 33 additions & 13 deletions ls/src/features/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ use async_lsp::lsp_types::{
use dashmap::mapref::one::Ref;
use serde::{Deserialize, Serialize};

use crate::configuration::MetadataValidationRule;
use chrono::NaiveDate;
use regex::Regex;

use crate::configuration::MetadataValidationRule;
#[cfg(feature = "full-compiler")]
use crate::documents::document::Document;
use crate::documents::storage::DocumentStorage;
Expand Down Expand Up @@ -95,18 +96,37 @@ pub fn compiler_diagnostics(
if let Some(ty) = &validation_rule.ty {
match ty.as_str() {
"string" => {
compiler.add_linter(linter.validator(
|meta| {
matches!(
meta.value,
yara_x_parser::ast::MetaValue::String(_)
)
},
format!(
"`{}` must be a `string`",
validation_rule.identifier
),
));
if let Some(pattern) = &validation_rule.regex {
compiler.add_linter(linter.validator(
|meta| {
if let yara_x_parser::ast::MetaValue::String(
value,
) = &meta.value
{
Regex::new(pattern).unwrap().is_match(value.0)
} else {
false
}
},
format!(
"`{}` must be a string and match the pattern `{}`",
validation_rule.identifier, pattern
),
));
} else {
compiler.add_linter(linter.validator(
|meta| {
matches!(
meta.value,
yara_x_parser::ast::MetaValue::String(_)
)
},
format!(
"`{}` must be a `string`",
validation_rule.identifier
),
));
}
}
"integer" => {
compiler.add_linter(linter.validator(
Expand Down
17 changes: 17 additions & 0 deletions ls/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,23 @@ impl YARALanguageServer {
),
});
}

for rule in &config.metadata_validation {
if let Some(pattern) = &rule.regex
&& let Err(err) = regex::Regex::new(pattern)
{
let _ = client.show_message(ShowMessageParams {
typ: MessageType::ERROR,
message: format!(
"YARA: invalid regex for metadata '{}': {} ({})",
rule.identifier,
pattern,
err
),
});
}
}

let _ = client.emit(UpdateConfig(config));
}
None => {
Expand Down
Loading