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
use super::*;
use crate::schema::*;

pub const MAX_NUMBER_OF_FIELDS: usize = 1024;

pub fn validate_type_kind<'a, S: CustomSchema>(
    context: &SchemaContext,
    type_kind: &SchemaTypeKind<S>,
) -> Result<(), SchemaValidationError> {
    match type_kind {
        TypeKind::Any
        | TypeKind::Bool
        | TypeKind::I8
        | TypeKind::I16
        | TypeKind::I32
        | TypeKind::I64
        | TypeKind::I128
        | TypeKind::U8
        | TypeKind::U16
        | TypeKind::U32
        | TypeKind::U64
        | TypeKind::U128
        | TypeKind::String => {
            // Nothing to check
        }
        TypeKind::Array { element_type } => {
            validate_index::<S>(context, element_type)?;
        }
        TypeKind::Tuple { field_types } => {
            if field_types.len() > MAX_NUMBER_OF_FIELDS {
                return Err(SchemaValidationError::TypeKindTupleTooLong {
                    max_size: MAX_NUMBER_OF_FIELDS,
                });
            }
            for field_type in field_types.iter() {
                validate_index::<S>(context, field_type)?;
            }
        }
        TypeKind::Enum { variants } => {
            for (_, field_types) in variants.iter() {
                if field_types.len() > MAX_NUMBER_OF_FIELDS {
                    return Err(SchemaValidationError::TypeKindEnumVariantTooLong {
                        max_size: MAX_NUMBER_OF_FIELDS,
                    });
                }
                for field_type in field_types.iter() {
                    validate_index::<S>(context, field_type)?;
                }
            }
        }
        TypeKind::Map {
            key_type,
            value_type,
        } => {
            validate_index::<S>(context, key_type)?;
            validate_index::<S>(context, value_type)?;
        }
        TypeKind::Custom(custom_type_kind) => {
            S::validate_custom_type_kind(context, custom_type_kind)?;
        }
    }

    Ok(())
}

pub fn validate_index<S: CustomSchema>(
    context: &SchemaContext,
    type_id: &LocalTypeId,
) -> Result<(), SchemaValidationError> {
    match type_id {
        LocalTypeId::WellKnown(well_known_index) => {
            if S::resolve_well_known_type(*well_known_index).is_none() {
                return Err(SchemaValidationError::TypeKindInvalidWellKnownIndex);
            }
        }
        LocalTypeId::SchemaLocalIndex(schema_local_index) => {
            if *schema_local_index >= context.local_types_len {
                return Err(SchemaValidationError::TypeKindInvalidSchemaLocalIndex);
            }
        }
    }
    Ok(())
}