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
use crate::datamodel_connector::ConnectorCapability;
use crate::{diagnostics::DatamodelError, validate::validation_pipeline::context::Context};
use bigdecimal::BigDecimal;
use parser_database::ScalarType;
use schema_ast::ast::{self, Expression};

/// Function `auto()` works for now only with MongoDB.
pub(super) fn validate_auto_param(default_value: Option<&ast::Expression>, ctx: &mut Context<'_>) {
    if ctx.connector.has_capability(ConnectorCapability::DefaultValueAuto) {
        return;
    }

    let expression = match default_value {
        Some(default_value) => default_value,
        None => return,
    };

    match expression {
        Expression::Function(name, _, span) if name == "auto" => {
            let message = "The current connector does not support the `auto()` function.";

            ctx.push_error(DatamodelError::new_attribute_validation_error(
                message, "@default", *span,
            ));
        }
        _ => (),
    }
}

/// Validates the @default attribute of a scalar field
pub(super) fn validate_default_value(
    default_value: Option<&ast::Expression>,
    scalar_type: Option<ScalarType>,
    ctx: &mut Context<'_>,
) {
    use chrono::{DateTime, FixedOffset};

    let scalar_type = match scalar_type {
        Some(scalar_type) => scalar_type,
        None => return,
    };

    let expression = match default_value {
        Some(default_value) => default_value,
        None => return,
    };

    // For array expressions, validate each element in the array.
    if let ast::Expression::Array(items, _) = expression {
        for item in items {
            validate_default_value(Some(item), Some(scalar_type), ctx);
        }

        return;
    }

    // Scalar type specific validations.
    match (scalar_type, expression) {
        (ScalarType::Json, ast::Expression::StringValue(value, span)) => {
            let details = match serde_json::from_str::<serde_json::Value>(value) {
                Ok(_) => return,
                Err(details) => details,
            };

            let message = format!("Parse error: \"{value}\" is not a valid JSON string. ({details})",);

            ctx.push_error(DatamodelError::new_attribute_validation_error(
                &message, "@default", *span,
            ));
        }
        (ScalarType::Bytes, ast::Expression::StringValue(value, span)) => {
            let details = match prisma_value::decode_bytes(value) {
                Ok(_) => return,
                Err(details) => details,
            };

            let message = format!("Parse error: \"{value}\" is not a valid base64 string. ({details})",);

            ctx.push_error(DatamodelError::new_attribute_validation_error(
                &message, "@default", *span,
            ));
        }
        (ScalarType::DateTime, ast::Expression::StringValue(value, span)) => {
            let details = match DateTime::<FixedOffset>::parse_from_rfc3339(value) {
                Ok(_) => return,
                Err(details) => details,
            };

            let message = format!("Parse error: \"{value}\" is not a valid rfc3339 datetime string. ({details})");

            ctx.push_error(DatamodelError::new_attribute_validation_error(
                &message, "@default", *span,
            ));
        }
        (ScalarType::BigInt | ScalarType::Int, ast::Expression::NumericValue(value, span)) => {
            let details = match value.parse::<i64>() {
                Ok(_) => return,
                Err(details) => details,
            };

            let message = format!("Parse error: \"{value}\" is not a valid integer. ({details})");

            ctx.push_error(DatamodelError::new_attribute_validation_error(
                &message, "@default", *span,
            ));
        }
        (ScalarType::Decimal, ast::Expression::StringValue(value, span)) => {
            let details = match value.parse::<BigDecimal>() {
                Ok(_) => return,
                Err(details) => details,
            };

            let message = format!("Parse error: \"{value}\" is not a valid decimal. ({details})");

            ctx.push_error(DatamodelError::new_attribute_validation_error(
                &message, "default", *span,
            ));
        }
        _ => (),
    }
}