yaml-edit 0.3.5

A lossless parser and editor for YAML files
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
//! YAML Schema validation support
//!
//! This module provides schema validation for YAML documents, supporting
//! the standard YAML schemas: Failsafe, JSON, and Core.

use crate::scalar::{ScalarType, ScalarValue};
use crate::yaml::{Document, TaggedNode};
use rowan::ast::AstNode;

/// Specific type of validation error that occurred
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ValidationErrorKind {
    /// A scalar type is not allowed in the current schema
    TypeNotAllowed {
        /// The scalar type that was found
        found_type: ScalarType,
        /// The types that are allowed in this schema
        allowed_types: Vec<ScalarType>,
    },
    /// Custom validation constraint failed
    CustomConstraintFailed {
        /// The constraint that failed
        constraint_name: String,
        /// The actual value that failed validation
        actual_value: String,
    },
    /// Type coercion failed
    CoercionFailed {
        /// The type that was found
        from_type: ScalarType,
        /// The types that coercion was attempted to
        to_types: Vec<ScalarType>,
    },
}

/// Error that occurs during schema validation
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ValidationError {
    /// The specific kind of validation error
    pub kind: ValidationErrorKind,
    /// Path to the node that failed validation (e.g., "root.items\[0\].name")
    pub path: String,
    /// Name of the schema that was being validated against
    pub schema_name: String,
}

impl ValidationError {
    /// Create a new type not allowed error
    pub fn type_not_allowed(
        path: impl Into<String>,
        schema_name: impl Into<String>,
        found_type: ScalarType,
        allowed_types: Vec<ScalarType>,
    ) -> Self {
        Self {
            kind: ValidationErrorKind::TypeNotAllowed {
                found_type,
                allowed_types,
            },
            path: path.into(),
            schema_name: schema_name.into(),
        }
    }

    /// Create a new custom constraint failed error
    pub fn custom_constraint_failed(
        path: impl Into<String>,
        schema_name: impl Into<String>,
        constraint_name: impl Into<String>,
        actual_value: impl Into<String>,
    ) -> Self {
        Self {
            kind: ValidationErrorKind::CustomConstraintFailed {
                constraint_name: constraint_name.into(),
                actual_value: actual_value.into(),
            },
            path: path.into(),
            schema_name: schema_name.into(),
        }
    }

    /// Create a new coercion failed error
    pub fn coercion_failed(
        path: impl Into<String>,
        schema_name: impl Into<String>,
        from_type: ScalarType,
        to_types: Vec<ScalarType>,
    ) -> Self {
        Self {
            kind: ValidationErrorKind::CoercionFailed {
                from_type,
                to_types,
            },
            path: path.into(),
            schema_name: schema_name.into(),
        }
    }

    /// Get a human-readable error message
    pub fn message(&self) -> String {
        match &self.kind {
            ValidationErrorKind::TypeNotAllowed {
                found_type,
                allowed_types,
            } => {
                format!(
                    "type {:?} not allowed in {} schema, expected one of {:?}",
                    found_type, self.schema_name, allowed_types
                )
            }
            ValidationErrorKind::CustomConstraintFailed {
                constraint_name,
                actual_value,
            } => {
                format!("custom constraint '{constraint_name}' failed for value '{actual_value}'")
            }
            ValidationErrorKind::CoercionFailed {
                from_type,
                to_types,
            } => {
                format!(
                    "cannot coerce {:?} to any of {:?} in {} schema",
                    from_type, to_types, self.schema_name
                )
            }
        }
    }
}

impl std::fmt::Display for ValidationError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Validation error at {}: {}", self.path, self.message())
    }
}

impl std::error::Error for ValidationError {}

/// Result type for schema validation operations
pub type ValidationResult<T> = Result<T, Vec<ValidationError>>;

/// Result of a custom validation function
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CustomValidationResult {
    /// Validation passed
    Valid,
    /// Validation failed with a specific constraint name and reason
    Invalid {
        /// Name of the constraint that failed (e.g., "email_format", "port_range")
        constraint: String,
        /// Human-readable reason for failure
        reason: String,
    },
}

impl CustomValidationResult {
    /// Create a validation failure
    pub fn invalid(constraint: impl Into<String>, reason: impl Into<String>) -> Self {
        Self::Invalid {
            constraint: constraint.into(),
            reason: reason.into(),
        }
    }

    /// Check if validation passed
    pub fn is_valid(&self) -> bool {
        matches!(self, Self::Valid)
    }

    /// Check if validation failed
    pub fn is_invalid(&self) -> bool {
        matches!(self, Self::Invalid { .. })
    }
}

/// Custom validation function for scalar values
///
/// Takes (value, path) and returns CustomValidationResult
pub type CustomValidator = Box<dyn Fn(&str, &str) -> CustomValidationResult + Send + Sync>;

/// Custom schema definition with user-defined validation rules
pub struct CustomSchema {
    /// Schema name for error messages
    pub name: String,
    /// Allowed scalar types in this schema
    pub allowed_types: Vec<ScalarType>,
    /// Custom validation functions by type
    pub custom_validators: std::collections::HashMap<ScalarType, CustomValidator>,
    /// Whether to allow type coercion
    pub allow_coercion: bool,
}

impl std::fmt::Debug for CustomSchema {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CustomSchema")
            .field("name", &self.name)
            .field("allowed_types", &self.allowed_types)
            .field("allow_coercion", &self.allow_coercion)
            .field(
                "validators",
                &format!("<{} validators>", self.custom_validators.len()),
            )
            .finish()
    }
}

impl CustomSchema {
    /// Create a new custom schema
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            allowed_types: Vec::new(),
            custom_validators: std::collections::HashMap::new(),
            allow_coercion: true,
        }
    }

    /// Allow a specific scalar type
    pub fn allow_type(mut self, scalar_type: ScalarType) -> Self {
        if !self.allowed_types.contains(&scalar_type) {
            self.allowed_types.push(scalar_type);
        }
        self
    }

    /// Allow multiple scalar types
    pub fn allow_types(mut self, types: &[ScalarType]) -> Self {
        for &scalar_type in types {
            if !self.allowed_types.contains(&scalar_type) {
                self.allowed_types.push(scalar_type);
            }
        }
        self
    }

    /// Add a custom validator for a specific type
    pub fn with_validator<F>(mut self, scalar_type: ScalarType, validator: F) -> Self
    where
        F: Fn(&str, &str) -> CustomValidationResult + Send + Sync + 'static,
    {
        self.custom_validators
            .insert(scalar_type, Box::new(validator));
        self
    }

    /// Disable type coercion for strict validation
    pub fn strict(mut self) -> Self {
        self.allow_coercion = false;
        self
    }

    /// Check if a scalar type is allowed
    pub fn allows_type(&self, scalar_type: ScalarType) -> bool {
        self.allowed_types.contains(&scalar_type)
    }

    /// Validate a scalar value with custom rules
    pub fn validate_scalar(&self, content: &str, path: &str) -> Result<(), ValidationError> {
        self.validate_scalar_value(&ScalarValue::parse(content.trim()), path)
    }

    fn validate_scalar_value(
        &self,
        scalar_value: &ScalarValue,
        path: &str,
    ) -> Result<(), ValidationError> {
        let content = scalar_value.value();
        let scalar_type = scalar_value.scalar_type();

        check_type(
            scalar_value,
            &self.allowed_types,
            self.allow_coercion,
            path,
            &self.name,
        )?;

        if let Some(validator) = self.custom_validators.get(&scalar_type) {
            let result = validator(content, path);
            if let CustomValidationResult::Invalid { constraint, reason } = result {
                return Err(ValidationError::custom_constraint_failed(
                    path,
                    &self.name,
                    format!("{constraint}: {reason}"),
                    content,
                ));
            }
        }

        Ok(())
    }
}

/// Check that `value` has one of the `allowed` types or, if `coerce` is
/// set, can be coerced to one of them.
fn check_type(
    value: &ScalarValue,
    allowed: &[ScalarType],
    coerce: bool,
    path: &str,
    schema_name: &str,
) -> Result<(), ValidationError> {
    let scalar_type = value.scalar_type();
    if allowed.contains(&scalar_type) {
        return Ok(());
    }
    if !coerce {
        return Err(ValidationError::type_not_allowed(
            path,
            schema_name,
            scalar_type,
            allowed.to_vec(),
        ));
    }
    if allowed.iter().any(|&t| value.coerce_to_type(t).is_some()) {
        Ok(())
    } else {
        Err(ValidationError::coercion_failed(
            path,
            schema_name,
            scalar_type,
            allowed.to_vec(),
        ))
    }
}

/// YAML Schema types as defined in YAML 1.2 specification
#[derive(Debug)]
pub enum Schema {
    /// Failsafe schema - only strings, sequences, and mappings
    Failsafe,
    /// JSON schema - JSON-compatible types only
    Json,
    /// Core schema - full YAML 1.2 type system
    Core,
    /// User-defined custom schema
    Custom(CustomSchema),
}

impl PartialEq for Schema {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Schema::Failsafe, Schema::Failsafe)
            | (Schema::Json, Schema::Json)
            | (Schema::Core, Schema::Core) => true,
            (Schema::Custom(a), Schema::Custom(b)) => a.name == b.name,
            _ => false,
        }
    }
}

impl Schema {
    /// Get the name of this schema
    pub fn name(&self) -> &str {
        match self {
            Schema::Failsafe => "failsafe",
            Schema::Json => "json",
            Schema::Core => "core",
            Schema::Custom(custom) => &custom.name,
        }
    }

    /// Check if a scalar type is allowed in this schema
    pub fn allows_scalar_type(&self, scalar_type: ScalarType) -> bool {
        self.allowed_scalar_types().contains(&scalar_type)
    }

    /// Get the allowed scalar types for this schema
    pub fn allowed_scalar_types(&self) -> Vec<ScalarType> {
        match self {
            Schema::Failsafe => vec![ScalarType::String],
            Schema::Json => vec![
                ScalarType::String,
                ScalarType::Integer,
                ScalarType::Float,
                ScalarType::Boolean,
                ScalarType::Null,
            ],
            Schema::Core => vec![
                ScalarType::String,
                ScalarType::Integer,
                ScalarType::Float,
                ScalarType::Boolean,
                ScalarType::Null,
                #[cfg(feature = "base64")]
                ScalarType::Binary,
                ScalarType::Timestamp,
                ScalarType::Regex,
            ],
            Schema::Custom(custom) => custom.allowed_types.clone(),
        }
    }
}

/// Schema validator for YAML documents
#[derive(Debug)]
pub struct SchemaValidator {
    schema: Schema,
    strict: bool,
}

impl SchemaValidator {
    /// Create a new schema validator
    pub fn new(schema: Schema) -> Self {
        Self {
            schema,
            strict: false,
        }
    }

    /// Create a failsafe schema validator
    pub fn failsafe() -> Self {
        Self::new(Schema::Failsafe)
    }

    /// Create a JSON schema validator  
    pub fn json() -> Self {
        Self::new(Schema::Json)
    }

    /// Create a core schema validator
    pub fn core() -> Self {
        Self::new(Schema::Core)
    }

    /// Create a validator for a custom schema
    pub fn custom(schema: CustomSchema) -> Self {
        Self::new(Schema::Custom(schema))
    }

    /// Enable strict mode - disallow type coercion
    pub fn strict(mut self) -> Self {
        self.strict = true;
        self
    }

    /// Get the schema type
    pub fn schema(&self) -> &Schema {
        &self.schema
    }

    /// Validate a YAML document against the schema
    pub fn validate(&self, document: &Document) -> ValidationResult<()> {
        self.check_document(document, false)
    }

    /// Check if a document can be coerced to match the schema
    ///
    /// This does not run the validators of a custom schema.
    pub fn can_coerce(&self, document: &Document) -> ValidationResult<()> {
        self.check_document(document, !self.strict)
    }

    fn check_document(&self, document: &Document, coerce_only: bool) -> ValidationResult<()> {
        let mut errors = Vec::new();
        if let Some(root) = document.root_node() {
            self.check_node(&root, "root", coerce_only, &mut errors);
        }
        if errors.is_empty() {
            Ok(())
        } else {
            Err(errors)
        }
    }

    fn check_node(
        &self,
        node: &rowan::SyntaxNode<crate::yaml::Lang>,
        path: &str,
        coerce_only: bool,
        errors: &mut Vec<ValidationError>,
    ) {
        use crate::yaml::{extract, Mapping, Scalar, Sequence};

        if let Some(scalar) = extract::<Scalar>(node) {
            let value = ScalarValue::from_scalar(&scalar);
            self.check_scalar(&value, false, path, coerce_only, errors);
        } else if let Some(tagged_node) = extract::<TaggedNode>(node) {
            if let Some(content) = tagged_node
                .as_sequence()
                .map(|s| s.syntax().clone())
                .or_else(|| tagged_node.as_mapping().map(|m| m.syntax().clone()))
            {
                self.check_node(&content, path, coerce_only, errors);
            } else if let Some(value) = ScalarValue::from_tagged(&tagged_node) {
                // A core schema tag names the type even where the content is
                // not a valid spelling of it.
                let value = match tagged_node
                    .resolved_tag()
                    .as_deref()
                    .and_then(ScalarValue::core_tag_scalar_type)
                {
                    Some(scalar_type) => value.with_type(scalar_type),
                    None => value,
                };
                self.check_scalar(&value, true, path, coerce_only, errors);
            }
        } else if let Some(sequence) = extract::<Sequence>(node) {
            for (i, item) in sequence.items().enumerate() {
                self.check_node(&item, &format!("{path}[{i}]"), coerce_only, errors);
            }
        } else if let Some(mapping) = extract::<Mapping>(node) {
            // The schema applies to the values, not the keys
            for (key_node, value_node) in mapping.pairs() {
                let key_name = key_node.text().to_string().trim().to_string();
                self.check_node(
                    &value_node,
                    &format!("{path}.{key_name}"),
                    coerce_only,
                    errors,
                );
            }
        }
    }

    fn check_scalar(
        &self,
        value: &ScalarValue,
        tagged: bool,
        path: &str,
        coerce_only: bool,
        errors: &mut Vec<ValidationError>,
    ) {
        let result = match &self.schema {
            Schema::Custom(custom_schema) if !coerce_only => {
                custom_schema.validate_scalar_value(value, path)
            }
            // A tagged scalar has its type written out, so it is not coerced
            _ => check_type(
                value,
                &self.schema.allowed_scalar_types(),
                !tagged && !self.strict,
                path,
                self.schema.name(),
            ),
        };
        errors.extend(result.err());
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_validator_creation() {
        let failsafe = SchemaValidator::failsafe();
        assert_eq!(*failsafe.schema(), Schema::Failsafe);
        assert!(!failsafe.strict);

        let json = SchemaValidator::json();
        assert_eq!(*json.schema(), Schema::Json);

        let core = SchemaValidator::core();
        assert_eq!(*core.schema(), Schema::Core);

        let strict_validator = SchemaValidator::json().strict();
        assert!(strict_validator.strict);
    }
}