cratestack-parser 0.10.0

Rust-native schema-first framework for typed HTTP APIs, generated clients, and backend services.
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
#![cfg(test)]

use super::parse_schema;

#[test]
fn accepts_readonly_and_server_only_field_attributes() {
    let schema = parse_schema(
        r#"
model Account {
  id Int @id
  balance Decimal @readonly
  internalScore Int @server_only
}
"#,
    )
    .expect("schema with field-policy attributes should parse");

    let fields = &schema.models[0].fields;
    assert!(
        fields[1].attributes.iter().any(|a| a.raw == "@readonly"),
        "expected @readonly on balance",
    );
    assert!(
        fields[2].attributes.iter().any(|a| a.raw == "@server_only"),
        "expected @server_only on internalScore",
    );
}

#[test]
fn rejects_readonly_on_primary_key() {
    let error = parse_schema(
        r#"
model Account {
  id Int @id @readonly
}
"#,
    )
    .expect_err("@readonly on @id should fail");

    assert!(
        error
            .to_string()
            .contains("primary key and must not declare @readonly"),
        "error: {error}",
    );
}

#[test]
fn rejects_server_only_on_primary_key() {
    let error = parse_schema(
        r#"
model Account {
  id Int @id @server_only
}
"#,
    )
    .expect_err("@server_only on @id should fail");

    assert!(
        error
            .to_string()
            .contains("primary key and must not declare @server_only"),
        "error: {error}",
    );
}

#[test]
fn rejects_readonly_and_server_only_together() {
    let error = parse_schema(
        r#"
model Account {
  id Int @id
  balance Decimal @readonly @server_only
}
"#,
    )
    .expect_err("combining @readonly + @server_only should fail");

    assert!(
        error
            .to_string()
            .contains("declares both @readonly and @server_only"),
        "error: {error}",
    );
}

#[test]
fn accepts_bare_dbgenerated_default() {
    let schema = parse_schema(
        r#"
model Article {
  id String @id @default(dbgenerated())
  createdAt DateTime @default(dbgenerated())
}
"#,
    )
    .expect("bare @default(dbgenerated()) should parse");

    let fields = &schema.models[0].fields;
    assert!(
        fields[0]
            .attributes
            .iter()
            .any(|a| a.raw == "@default(dbgenerated())"),
    );
}

#[test]
fn rejects_dbgenerated_with_argument() {
    let error = parse_schema(
        r#"
model Article {
  id String @id @default(dbgenerated("gen_random_uuid()"))
}
"#,
    )
    .expect_err("dbgenerated() with an argument should fail");

    assert!(
        error.to_string().contains("takes no argument"),
        "error: {error}",
    );
}

#[test]
fn accepts_pii_and_sensitive_field_attributes() {
    let schema = parse_schema(
        r#"
model Customer {
  id Int @id
  email String @pii
  riskScore Int @sensitive
}
"#,
    )
    .expect("schema with @pii and @sensitive should parse");

    let fields = &schema.models[0].fields;
    assert!(fields[1].attributes.iter().any(|a| a.raw == "@pii"));
    assert!(fields[2].attributes.iter().any(|a| a.raw == "@sensitive"));
}

/// gRPC/protobuf support was removed (a breaking change that shipped in
/// 0.8.5), and with it the `@pb(N)` shape validator (duplicate check,
/// non-negative-integer check, reserved-range check).
///
/// Deleting that validator alone would have made `@pb(N)` *inert* rather
/// than invalid: `.cstack` attributes parse generically into opaque
/// `Attribute { raw, span }` (see `crate::parse::fields`) and there is no
/// blanket "reject unknown attribute" pass, so an unrecognised name just
/// falls through (see `validate_validator_attributes`'s `_ => {}` arm).
/// That is the right default for an attribute that never existed and the
/// wrong one for an attribute that did — a pre-0.8.5 schema full of `@pb`
/// pins would keep parsing while silently meaning nothing.
///
/// So `@pb` is rejected by name in `validate::removed_attributes`. This
/// pins the user-visible half of that: the attribute is a hard error, and
/// the message says what happened rather than just "unknown".
#[test]
fn pb_field_attribute_is_rejected_as_removed() {
    let err = parse_schema(
        r#"
model User {
  id Int @id
  email String @pb(3)
}
"#,
    )
    .expect_err("`@pb` was removed in 0.8.5 and must not parse as a silent no-op");

    assert!(err.to_string().contains("@pb"), "error: {err}");
    assert!(err.to_string().contains("removed in 0.8.5"), "error: {err}");
}

/// The rejection is wired at *every* field-bearing declaration, not just on
/// models — a `@pb` pin was equally writable on a mixin, a type, a view, or
/// the auth block before 0.8.5, and must fail equally loudly on all of them
/// now.
///
/// This covers all five call sites deliberately. An earlier revision wired
/// only three (model/mixin/type) and left `view` and `auth` silently
/// accepting `@pb`, which is precisely the no-op behaviour
/// `validate::removed_attributes` exists to prevent.
#[test]
fn pb_field_attribute_is_rejected_on_every_field_bearing_declaration() {
    for (kind, source) in [
        (
            "mixin",
            r#"
mixin Timestamps {
  created_at DateTime @pb(1)
}
"#,
        ),
        (
            "type",
            r#"
type Address {
  city String @pb(1)
}
"#,
        ),
        (
            "view",
            r#"
model Widget {
  id Int @id
  name String
}

view WidgetSummary from Widget {
  id Int @id
  name String @pb(4)
  @@sql("SELECT id, name FROM widget")
}
"#,
        ),
        (
            "auth block",
            r#"
auth User {
  id String @id @pb(9)
}
"#,
        ),
    ] {
        let err =
            parse_schema(source).expect_err(&format!("`@pb` must be rejected on {kind} fields"));
        assert!(err.to_string().contains("@pb"), "{kind}: {err}");
        assert!(
            err.to_string().contains("removed in 0.8.5"),
            "{kind} must get the same removal guidance models get, not a bare \
             unknown-attribute error: {err}",
        );
    }
}

/// The match is on `@pb` exactly and `@pb(...)`, not on a `@pb` *prefix* —
/// an unrelated attribute that merely starts with those characters must
/// still fall through the generic unrecognised-attribute path untouched.
#[test]
fn removed_attribute_matching_does_not_swallow_longer_names() {
    parse_schema(
        r#"
model User {
  id Int @id
  secret String @pbkdf2_rounds(10)
}
"#,
    )
    .expect("only `@pb` itself and `@pb(...)` were removed");
}

/// cratestack#679 (half 1 of 2 — see the module doc on
/// `validate::removed_attributes` for why the typo-class half, e.g.
/// `@raedonly` silently dropping `@readonly`, is deliberately out of scope
/// here): field-level `@allow(...)` parses, is retained in the IR, and is
/// never read by any codegen — a silent no-op that looks like access
/// control and enforces nothing. It must be a hard parse error instead,
/// naming the field and pointing at the real alternatives.
#[test]
fn allow_field_attribute_is_rejected() {
    let err = parse_schema(
        r#"
model Asset {
  id Int @id
  bucket String @allow(auth().role == "system")
}
"#,
    )
    .expect_err("field-level `@allow` must not parse as a silent no-op");

    assert!(err.to_string().contains("@allow"), "error: {err}");
    assert!(err.to_string().contains("bucket"), "error: {err}");
    assert!(
        err.to_string().contains("@@allow"),
        "error should point at the model-level alternative: {err}",
    );
}

/// Same defect, `@deny` half.
#[test]
fn deny_field_attribute_is_rejected() {
    let err = parse_schema(
        r#"
model Asset {
  id Int @id
  bucket String @deny(auth().role != "system")
}
"#,
    )
    .expect_err("field-level `@deny` must not parse as a silent no-op");

    assert!(err.to_string().contains("@deny"), "error: {err}");
    assert!(
        err.to_string().contains("@@deny"),
        "error should point at the model-level alternative: {err}",
    );
}

/// Mirrors `pb_field_attribute_is_rejected_on_every_field_bearing_declaration`:
/// field-level `@allow`/`@deny` must be rejected on all five field-bearing
/// declaration kinds, not just `model`. A missed call site in
/// `validate::removed_attributes` fails *silently* (the attribute goes back
/// to being an inert no-op), so this is the guard against that.
#[test]
fn allow_and_deny_field_attributes_are_rejected_on_every_field_bearing_declaration() {
    for attribute in ["@allow(auth() != null)", "@deny(auth() == null)"] {
        for (kind, source) in [
            (
                "model",
                format!(
                    r#"
model Asset {{
  id Int @id
  bucket String {attribute}
}}
"#
                ),
            ),
            (
                "mixin",
                format!(
                    r#"
mixin Timestamps {{
  created_at DateTime {attribute}
}}
"#
                ),
            ),
            (
                "type",
                format!(
                    r#"
type Address {{
  city String {attribute}
}}
"#
                ),
            ),
            (
                "view",
                format!(
                    r#"
model Widget {{
  id Int @id
  name String
}}

view WidgetSummary from Widget {{
  id Int @id
  name String {attribute}
  @@sql("SELECT id, name FROM widget")
}}
"#
                ),
            ),
            (
                "auth block",
                format!(
                    r#"
auth User {{
  id String @id {attribute}
}}
"#
                ),
            ),
        ] {
            let err = parse_schema(&source)
                .expect_err(&format!("`{attribute}` must be rejected on {kind} fields"));
            let name = if attribute.starts_with("@allow") {
                "@allow"
            } else {
                "@deny"
            };
            assert!(err.to_string().contains(name), "{kind}: {err}");
            assert!(
                err.to_string().contains("not supported at field position"),
                "{kind} must get field-policy guidance, not a bare unknown-attribute error: {err}",
            );
        }
    }
}

/// Regression guard for cratestack#679's scope boundary: procedure-level
/// `@allow`/`@deny` is real, supported policy
/// (`cratestack-macros/src/policy/procedure.rs`) on a *procedure's*
/// attribute list, not a field's — the field-position rejection added for
/// #679 must not touch it.
#[test]
fn procedure_level_allow_still_parses() {
    parse_schema(
        r#"
auth UserAuth {
  id Int
  role String
}

type PublishPostInput {
  postId Int
}

mutation procedure publishPost(args: PublishPostInput): PublishPostInput
  @allow(auth().role == "admin")
"#,
    )
    .expect("procedure-level `@allow` must keep parsing — it is real, supported policy");
}

/// Regression guard for cratestack#679's scope boundary: model/view-level
/// `@@allow`/`@@deny` (double-`@`) is real, supported policy
/// (`cratestack-macros/src/policy/model.rs`) on the model/view's own
/// attribute list, not a field's — the field-position rejection must match
/// the bare single-`@` name precisely and not swallow the double-`@` form.
#[test]
fn model_level_double_at_allow_and_deny_still_parse() {
    parse_schema(
        r#"
auth UserAuth {
  id Int
  role String
}

model User {
  id Int @id
  email String @unique
  role String

  @@allow("read", auth() != null)
  @@deny("read", auth().role == "banned")
}
"#,
    )
    .expect("model-level `@@allow`/`@@deny` must keep parsing — they are real, supported policy");
}

/// cratestack#679's typo half. Mirrors
/// `pb_field_attribute_is_rejected_on_every_field_bearing_declaration`:
/// a missed call site in `validate::misspelled_attributes` fails
/// *silently* — the typo goes back to being an inert no-op, which is the
/// exact bug — so this covers all five field-bearing declarations.
#[test]
fn a_misspelled_field_attribute_is_rejected_on_every_field_bearing_declaration() {
    for (kind, source) in [
        (
            "model",
            r#"
model Asset {
  id Int @id
  bucket String @raedonly
}
"#,
        ),
        (
            "mixin",
            r#"
mixin Timestamps {
  created_at DateTime @raedonly
}
"#,
        ),
        (
            "type",
            r#"
type Address {
  city String @raedonly
}
"#,
        ),
        (
            "view",
            r#"
model Widget {
  id Int @id
  name String
}

view WidgetSummary from Widget {
  id Int @id
  name String @raedonly
  @@sql("SELECT id, name FROM widget")
}
"#,
        ),
        (
            "auth block",
            r#"
auth User {
  id String @id
  label String @raedonly
}
"#,
        ),
    ] {
        let err = parse_schema(source)
            .expect_err(&format!("`@raedonly` must be rejected on {kind} fields"));
        let message = err.to_string();
        assert!(
            message.contains("@raedonly"),
            "{kind} must name the offending attribute: {message}"
        );
        assert!(
            message.contains("@readonly"),
            "{kind} must suggest the attribute the author meant: {message}"
        );
    }
}

/// The other half of cratestack#679's option (b), and the reason it was
/// chosen over a closed attribute set: an attribute that resembles nothing
/// is left inert rather than becoming a parse error. This is the ticket's
/// own `@totallyBogusAttribute` example.
///
/// Without this, "reject every unrecognised attribute" would satisfy the
/// test above while being a different — and much larger — language change
/// than the one that was decided.
#[test]
fn an_unknown_attribute_that_resembles_nothing_still_parses() {
    parse_schema(
        r#"
model Asset {
  id Int @id
  bucket String @totallyBogusAttribute(whatever == 1)
}
"#,
    )
    .expect("an attribute that is not a near-miss of a known name stays inert (option (b))");
}

/// Guards against the near-miss check over-rejecting real, supported
/// attributes — the failure mode that made option (a) too risky to take.
/// Every one of these must keep parsing exactly as before.
#[test]
fn supported_field_attributes_are_unaffected_by_the_near_miss_check() {
    parse_schema(
        r#"
model Widget {
  id Int @id
  code String @unique @length(min: 1, max: 10)
  email String @email
  price Decimal @range(min: 0)
  slug String @regex("^[a-z]+$")
  currency String @iso4217
  secret String @server_only
  balance Decimal @readonly
  note String @sensitive
  owner String @pii
  rev Int @version
}
"#,
    )
    .expect("every supported field attribute must survive the near-miss check");
}