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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/// Schema for `fail`-branch requirements. Non-member requirements accept
/// null alongside the demanded type: fail tests routinely sit behind
/// `default`-chained locals, where a null input takes the fallback and
/// renders. Member requirements stay exact (a null member value really
/// aborts).
#[expect(
clippy::too_many_lines,
reason = "keeping this semantic lowering operation together makes its state transitions easier to audit"
)]
pub(crate) fn fail_requirement_schema<'a>(
implications: impl IntoIterator<Item = &'a helm_schema_core::ContractRequirementImplication>,
) -> (Value, usize) {
let mut parts = Vec::new();
let mut insertion_abstentions = 0;
for implication in implications {
let requirement = fail_value_requirement_schema(
&implication.requirements,
!matches!(
&implication.target,
helm_schema_core::ContractRequirementTarget::Value
),
);
if is_empty_schema(&requirement) {
continue;
}
match &implication.target {
helm_schema_core::ContractRequirementTarget::Value => parts.push(requirement),
helm_schema_core::ContractRequirementTarget::Members { allow_integer } => {
let mut arms = vec![
serde_json::json!({ "type": "array", "items": requirement }),
serde_json::json!({
"type": "object",
"additionalProperties": requirement,
}),
];
if *allow_integer {
let integer =
if requirements_allow_runtime_kind(&implication.requirements, "integer") {
serde_json::json!({ "type": "integer" })
} else {
// Nonpositive integer ranges execute no iterations,
// so no member reaches the body requirement.
serde_json::json!({ "type": "integer", "maximum": 0 })
};
arms.push(integer);
}
arms.push(serde_json::json!({ "type": "null" }));
parts.push(serde_json::json!({ "anyOf": arms }));
}
helm_schema_core::ContractRequirementTarget::MembersExceptKeys {
keys,
allow_integer,
} => {
let properties = keys
.iter()
.map(|key| (key.clone(), serde_json::json!({})))
.collect::<serde_json::Map<_, _>>();
let mut arms = vec![
serde_json::json!({ "type": "array", "items": requirement }),
serde_json::json!({
"type": "object",
"properties": properties,
"additionalProperties": requirement,
}),
];
if *allow_integer {
let integer =
if requirements_allow_runtime_kind(&implication.requirements, "integer") {
serde_json::json!({ "type": "integer" })
} else {
serde_json::json!({ "type": "integer", "maximum": 0 })
};
arms.push(integer);
}
arms.push(serde_json::json!({ "type": "null" }));
parts.push(serde_json::json!({ "anyOf": arms }));
}
helm_schema_core::ContractRequirementTarget::MembersMatchingPrefix { prefix } => {
let pattern = format!("^{}", helm_schema_core::escape_regex_literal(prefix));
parts.push(serde_json::json!({
"anyOf": [
{
"type": "object",
"patternProperties": { (pattern): requirement },
},
{ "type": "array", "maxItems": 0 },
{ "type": "null" },
]
}));
}
helm_schema_core::ContractRequirementTarget::MembersAt {
target_path,
allow_integer,
}
| helm_schema_core::ContractRequirementTarget::MembersAtWhereTruthy {
target_path,
allow_integer,
..
} => {
// Nil-tolerant requirements (comparison operands, and
// truthy-scoped types whose absent leaf is falsy and escapes
// the consumer) hold only when the leaf is present, so the
// wrapper must not demand the field itself. A
// `NotSchemaType` requirement is likewise satisfied by an
// absent leaf: the failing test it negates fired only on
// values OF that type (a quoted-token splice constrains
// strings; members without the field never render it).
let tolerant_leaf = implication.requirements.iter().all(|requirement| {
matches!(
requirement,
helm_schema_core::FailValueRequirement::ComparableKind(_)
| helm_schema_core::FailValueRequirement::TruthyImpliesSchemaType(_)
| helm_schema_core::FailValueRequirement::NotEquals(_)
)
}) || implication.requirements.iter().any(|requirement| {
matches!(
requirement,
helm_schema_core::FailValueRequirement::NotSchemaType(_)
)
});
let member = match &implication.target {
// A per-member selector binds only the members the
// chart's own gate routes to the consumer; every other
// member — and every non-object one, which cannot hold
// the selector field — passes the arm unconstrained. The
// gated leaf stays OPTIONAL: the selector says which
// members reach the consumer, not that the consumer
// aborts when its operand is missing, which is the
// separate absence claim's fact (an empty target path
// carries exactly that claim, as its own required
// member).
helm_schema_core::ContractRequirementTarget::MembersAtWhereTruthy {
guard_path,
..
} => serde_json::json!({
"if": required_object_path_schema(
guard_path,
crate::condition_encoding::helm_truthy_condition_schema().into_value(),
),
"then": optional_leaf_object_path_schema(target_path, requirement),
}),
_ if tolerant_leaf => {
optional_leaf_object_path_schema(target_path, requirement)
}
_ => required_object_path_schema(target_path, requirement),
};
let mut arms = vec![
serde_json::json!({ "type": "array", "items": member }),
serde_json::json!({
"type": "object",
"additionalProperties": member,
}),
];
if *allow_integer {
// Integer iteration yields int members, which can never
// host the required field; only zero iterations pass.
arms.push(serde_json::json!({ "type": "integer", "maximum": 0 }));
}
arms.push(serde_json::json!({ "type": "null" }));
parts.push(serde_json::json!({ "anyOf": arms }));
}
helm_schema_core::ContractRequirementTarget::MembersWhereEquals {
guard_path,
value,
target_path,
} => {
let Some(value) = serde_json::to_value(value).ok() else {
continue;
};
let guard =
required_object_path_schema(guard_path, serde_json::json!({ "const": value }));
let (target, abstentions) = crate::schema_tree::insert_path_schema_value(
empty_schema(),
target_path,
requirement,
);
insertion_abstentions += abstentions;
let member = serde_json::json!({ "if": guard, "then": target });
parts.push(serde_json::json!({
"anyOf": [
{ "type": "array", "items": member },
{ "type": "object", "additionalProperties": member },
{ "type": "null" },
]
}));
}
helm_schema_core::ContractRequirementTarget::Keys => {
let mut object =
if requirements_allow_runtime_kind(&implication.requirements, "string") {
serde_json::json!({ "type": "object" })
} else {
serde_json::json!({ "type": "object", "maxProperties": 0 })
};
// Pattern requirements constrain each KEY's spelling
// (traefik's uppercase gate); string keys are structural in
// YAML maps, so only the pattern itself needs encoding.
let mut key_schemas = Vec::new();
for requirement in &implication.requirements {
match requirement {
helm_schema_core::FailValueRequirement::MatchesPattern {
pattern,
templated: false,
} => {
if let Some(pattern) = ecma_compatible_pattern(pattern) {
key_schemas.push(serde_json::json!({ "pattern": pattern }));
}
}
helm_schema_core::FailValueRequirement::NotMatchesPattern { pattern } => {
if let Some(pattern) = ecma_compatible_pattern(pattern) {
key_schemas
.push(serde_json::json!({ "not": { "pattern": pattern } }));
}
}
helm_schema_core::FailValueRequirement::StringLengthBounds { min, max } => {
let mut bounds = serde_json::Map::new();
if let Some(min) = min {
bounds.insert("minLength".to_string(), serde_json::json!(min));
}
if let Some(max) = max {
bounds.insert("maxLength".to_string(), serde_json::json!(max));
}
key_schemas.push(Value::Object(bounds));
}
// Keys are always strings, so the exclusions apply
// directly rather than through the non-string escape.
helm_schema_core::FailValueRequirement::PlainScalarSafe {
token_initial,
..
} => {
key_schemas.push(serde_json::json!({
"allOf": crate::resolve_policy::plain_scalar_structural_exclusions(
*token_initial,
)
}));
}
_ => {}
}
}
if let Some(object) = object.as_object_mut() {
match key_schemas.len() {
0 => {}
1 => {
object.insert(
"propertyNames".to_string(),
key_schemas.pop().unwrap_or_else(|| serde_json::json!({})),
);
}
_ => {
object.insert(
"propertyNames".to_string(),
serde_json::json!({ "allOf": key_schemas }),
);
}
}
}
let array = if requirements_allow_runtime_kind(&implication.requirements, "integer")
{
serde_json::json!({ "type": "array" })
} else {
// An empty array never evaluates the range body, so no
// integer key reaches the strict consumer.
serde_json::json!({ "type": "array", "maxItems": 0 })
};
parts.push(serde_json::json!({
"anyOf": [object, array, { "type": "null" }]
}));
}
}
}
(merge_schema_list(parts), insertion_abstentions)
}
/// Like [`required_object_path_schema`], but the LEAF member stays
/// optional: nil-tolerant requirements (comparison operands) constrain the
/// field only when it is present. Intermediate segments stay required
/// because field access through an absent parent aborts rendering with a
/// nil-pointer error before the tolerant leaf comparison runs.
fn requirements_allow_runtime_kind(
requirements: &[helm_schema_core::FailValueRequirement],
schema_type: &str,
) -> bool {
use helm_schema_core::FailValueRequirement;
requirements.iter().all(|requirement| match requirement {
FailValueRequirement::SchemaType(required)
// Null is asserted away before Sprig's missing-key handling runs.
| FailValueRequirement::SchemaTypeEvenNull(required) => required == schema_type,
// Every runtime kind has a Helm-falsy spelling that escapes the
// consumer, so the truthy-scoped requirement excludes no kind.
FailValueRequirement::TruthyImpliesSchemaType(_)
// Every runtime kind has a Helm-falsy spelling.
| FailValueRequirement::HelmFalsy
| FailValueRequirement::NotEquals(_)
// Applies only to present fields on objects; every other kind
// passes vacuously (a missing field differs from every literal).
| FailValueRequirement::FieldNotEquals { .. }
// The requirement constrains rendered CONTENT, not the value's kind
// (non-strings format as safe plain tokens, and every string kind
// has token-safe inhabitants).
| FailValueRequirement::QuotedSerializationSafe { .. }
| FailValueRequirement::PlainScalarSafe { .. }
// The field constraint applies only to objects carrying the field;
// every other kind passes vacuously.
| FailValueRequirement::FieldHelmFalsy { .. } => true,
// Every runtime kind except null has truthy inhabitants.
FailValueRequirement::HelmTruthy => schema_type != "null",
FailValueRequirement::ComparableKind(required) => {
required == schema_type || schema_type == "null"
}
FailValueRequirement::PrintfStringOperand => {
matches!(schema_type, "object" | "string")
}
FailValueRequirement::NotSchemaType(rejected) => rejected != schema_type,
FailValueRequirement::MatchesPattern { .. }
| FailValueRequirement::NotMatchesPattern { .. }
| FailValueRequirement::StringLengthBounds { .. } => schema_type == "string",
FailValueRequirement::Iterable { allow_integer } => {
matches!(schema_type, "array" | "object" | "null")
|| schema_type == "integer" && *allow_integer
}
FailValueRequirement::HasMember(_)
| FailValueRequirement::HasMemberEvenDefaulted(_)
| FailValueRequirement::FieldEquals { .. }
// Presence of a (truthy or non-null) field needs an object host.
| FailValueRequirement::FieldPresentNotNull { .. }
| FailValueRequirement::FieldHelmTruthy { .. } => schema_type == "object",
FailValueRequirement::MemberHost { handled_kinds, .. } => {
schema_type == "object" || handled_kinds.iter().any(|kind| kind == schema_type)
}
FailValueRequirement::IndexableAt(_) => matches!(schema_type, "array" | "string"),
FailValueRequirement::SplitSegmentsAtLeast {
allow_non_string, ..
} => schema_type == "string" || *allow_non_string,
FailValueRequirement::AnyOf(alternatives) => alternatives
.iter()
.any(|alternative| requirements_allow_runtime_kind(alternative, schema_type)),
})
}
#[expect(
clippy::too_many_lines,
reason = "keeping this semantic lowering operation together makes its state transitions easier to audit"
)]
fn fail_value_requirement_schema(
requirements: &[helm_schema_core::FailValueRequirement],
per_member: bool,
) -> Value {
use helm_schema_core::FailValueRequirement;
let mut parts = Vec::new();
let mut required_members: Vec<&str> = Vec::new();
for requirement in requirements {
match requirement {
FailValueRequirement::SchemaType(schema_type) => {
if per_member {
parts.push(type_schema(schema_type));
} else {
parts.push(crate::schema_model::type_union_schema([
schema_type.as_str(),
"null",
]));
}
}
// No null tolerance: the consumer type-asserts before its nil
// handling, so an explicit null aborts (absence stays open
// through the arm's properties anchoring).
FailValueRequirement::SchemaTypeEvenNull(schema_type) => {
parts.push(type_schema(schema_type));
}
// Only truthy values reach the consumer; every Helm-falsy
// spelling escapes through the selection and stays accepted.
FailValueRequirement::TruthyImpliesSchemaType(schema_type) => {
parts.push(
SchemaNode::any_of(vec![
SchemaNode::from_value(type_schema(schema_type)),
SchemaNode::not(
crate::condition_encoding::helm_truthy_condition_schema(),
),
])
.into_value(),
);
}
FailValueRequirement::HelmTruthy => {
parts.push(
crate::condition_encoding::helm_truthy_condition_schema().into_value(),
);
}
FailValueRequirement::HelmFalsy => {
parts.push(
SchemaNode::not(crate::condition_encoding::helm_truthy_condition_schema())
.into_value(),
);
}
// `properties` constrains only PRESENT keys on objects — an
// absent or null field differs from every literal, so no
// `required` rides along.
FailValueRequirement::FieldNotEquals { path, value } => {
let Some(value) = guard_value_to_json(value) else {
continue;
};
let mut node = serde_json::json!({ "not": { "const": value } });
for segment in path.iter().rev() {
node = serde_json::json!({ "properties": { segment: node } });
}
parts.push(node);
}
// `properties` constrains only PRESENT keys on objects, which
// is exactly the tolerance the negated truthiness test needs:
// an absent or falsy field renders, a truthy one aborts.
FailValueRequirement::FieldHelmFalsy { path } => {
let mut node = SchemaNode::not(
crate::condition_encoding::helm_truthy_condition_schema(),
)
.into_value();
for segment in path.iter().rev() {
node = serde_json::json!({ "properties": { segment: node } });
}
parts.push(node);
}
FailValueRequirement::NotEquals(value) => {
let Some(value) = guard_value_to_json(value) else {
continue;
};
parts.push(serde_json::json!({ "not": { "const": value } }));
}
// Nil compares, so a null member is as valid as an absent one.
FailValueRequirement::ComparableKind(schema_type) => {
parts.push(crate::schema_model::type_union_schema([
schema_type.as_str(),
"null",
]));
}
FailValueRequirement::NotSchemaType(schema_type) => {
parts.push(serde_json::json!({ "not": type_schema(schema_type) }));
}
FailValueRequirement::HasMember(member)
| FailValueRequirement::HasMemberEvenDefaulted(member) => {
required_members.push(member);
}
FailValueRequirement::MatchesPattern { pattern, templated } => {
// JSON Schema patterns are ECMA 262; abstaining on an
// untranslatable Go/RE2 pattern only widens the arm back
// to its other requirements.
if let Some(pattern) = ecma_compatible_pattern(pattern) {
let matches = serde_json::json!({ "type": "string", "pattern": pattern });
if *templated {
// The pattern constrains `tpl`'s OUTPUT: a raw value
// carrying a template action renders to something
// that may match, so admit it alongside the
// action-free strings the pattern already accepts.
parts.push(serde_json::json!({
"anyOf": [
matches,
{ "type": "string", "pattern": "\\{\\{" },
]
}));
} else {
parts.push(matches);
}
}
}
FailValueRequirement::NotMatchesPattern { pattern } => {
// Abstaining on an untranslatable pattern only widens the
// arm back to its other requirements, as for MatchesPattern.
if let Some(pattern) = ecma_compatible_pattern(pattern) {
parts.push(serde_json::json!({
"type": "string",
"not": { "pattern": pattern },
}));
}
}
FailValueRequirement::StringLengthBounds { min, max } => {
let mut bounds = serde_json::Map::new();
bounds.insert("type".to_string(), serde_json::json!("string"));
if let Some(min) = min {
bounds.insert("minLength".to_string(), serde_json::json!(min));
}
if let Some(max) = max {
bounds.insert("maxLength".to_string(), serde_json::json!(max));
}
parts.push(Value::Object(bounds));
}
FailValueRequirement::MemberHost { handled_kinds, .. } => {
let types = std::iter::once("object").chain(
handled_kinds
.iter()
.filter(|kind| kind.as_str() != "object")
.map(String::as_str),
);
parts.push(crate::schema_model::type_union_schema(types));
}
FailValueRequirement::Iterable { allow_integer } => {
parts.push(crate::runtime_iterable_schema(*allow_integer));
}
FailValueRequirement::IndexableAt(index) => {
parts.push(serde_json::json!({
"anyOf": [
{ "type": "array", "minItems": index + 1 },
{ "type": "string" },
]
}));
}
FailValueRequirement::SplitSegmentsAtLeast {
separator,
segments,
allow_non_string,
} => {
let occurrences = segments.saturating_sub(1);
let pattern = format!(
"^(?:[\\s\\S]*{}){{{occurrences}}}",
helm_schema_core::escape_regex_literal(separator)
);
let string = serde_json::json!({ "type": "string", "pattern": pattern });
if *allow_non_string {
parts.push(serde_json::json!({
"anyOf": [string, { "not": { "type": "string" } }]
}));
} else {
parts.push(string);
}
}
FailValueRequirement::QuotedSerializationSafe { style, templated } => {
parts.push(crate::quoted_serialization::reference_schema(
*style, *templated,
));
}
FailValueRequirement::PrintfStringOperand => {
parts.push(serde_json::json!({
"anyOf": [
crate::resolve_policy::printf_string_formattable_string_schema(),
crate::resolve_policy::printf_string_formattable_mapping_schema(),
]
}));
}
FailValueRequirement::PlainScalarSafe {
token_initial,
templated,
} => {
parts.push(plain_scalar_safe_schema(*token_initial, *templated));
}
// Presence rides the equality (Go's `eq` aborts on nil), so
// every wrapping level requires its segment and the host must
// be an object.
FailValueRequirement::FieldEquals { path, value } => {
let Some(value) = guard_value_to_json(value) else {
continue;
};
let mut node = serde_json::json!({ "const": value });
for segment in path.iter().rev() {
node = serde_json::json!({
"type": "object",
"required": [segment],
"properties": { segment: node },
});
}
parts.push(node);
}
// Presence rides along both field forms: an absent field is
// null-rendered (not-null fails) and falsy (truthy fails), so
// every wrapping level requires its segment.
FailValueRequirement::FieldPresentNotNull { path } => {
let mut node = serde_json::json!({ "not": { "type": "null" } });
for segment in path.iter().rev() {
node = serde_json::json!({
"type": "object",
"required": [segment],
"properties": { segment: node },
});
}
parts.push(node);
}
FailValueRequirement::FieldHelmTruthy { path } => {
let mut node =
crate::condition_encoding::helm_truthy_condition_schema().into_value();
for segment in path.iter().rev() {
node = serde_json::json!({
"type": "object",
"required": [segment],
"properties": { segment: node },
});
}
parts.push(node);
}
FailValueRequirement::AnyOf(alternatives) => {
let arms: Vec<Value> = alternatives
.iter()
.map(|alternative| fail_value_requirement_schema(alternative, per_member))
.collect();
// Field-based alternatives all READ a member field, and a
// field read aborts on non-object members, so the
// alternation nests inside one object schema. Descendant
// property insertions then merge as siblings of the inner
// `anyOf` (a conjunction); emitting a bare `anyOf` would
// let the union combiner treat the carrier as one more
// arm and make the alternation vacuous.
let field_based = alternatives.iter().flatten().all(|requirement| {
matches!(
requirement,
FailValueRequirement::HasMember(_)
| FailValueRequirement::FieldEquals { .. }
| FailValueRequirement::FieldNotEquals { .. }
| FailValueRequirement::FieldHelmFalsy { .. }
| FailValueRequirement::FieldPresentNotNull { .. }
| FailValueRequirement::FieldHelmTruthy { .. }
)
});
if field_based {
parts.push(serde_json::json!({ "type": "object", "anyOf": arms }));
} else {
parts.push(serde_json::json!({ "anyOf": arms }));
}
}
}
}
if !required_members.is_empty() {
required_members.sort_unstable();
required_members.dedup();
parts.push(serde_json::json!({
"type": "object",
"required": required_members,
}));
}
// Requirements are CONJUNCTIVE: they must all hold for the tested
// value. `merge_schema_list` is the evidence-union combiner whose
// fallback is `anyOf`, which would silently weaken a multi-requirement
// implication (two not-equals arms union into a tautology).
let mut conjuncts: Vec<Value> = Vec::new();
for part in parts {
if !conjuncts.contains(&part) {
conjuncts.push(part);
}
}
match conjuncts.len() {
0 => empty_schema(),
1 => conjuncts.remove(0),
_ => serde_json::json!({ "allOf": conjuncts }),
}
}