/
file.cr
59 lines (52 loc) · 1.6 KB
/
file.cr
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
module Marten
abstract class Schema
module Field
# Represents a file schema field.
class File < Base
# Returns the maximum name size allowed.
getter max_name_size
def initialize(
@id : ::String,
@required : ::Bool = true,
@max_name_size : ::Int32? = nil,
@allow_empty_files : ::Bool = false
)
end
# Returns `true` if empty files are allowed.
def allow_empty_files?
@allow_empty_files
end
def deserialize(value) : HTTP::UploadedFile?
case value
when HTTP::UploadedFile
value
when Nil
value
when ::String
value.empty? ? nil : raise_unexpected_field_value(value)
when ::JSON::Any
deserialize(value.raw)
else
raise_unexpected_field_value(value)
end
end
def serialize(value) : ::String?
# Files cannot really be serialized as strings, so simply return nil.
nil
end
def validate(schema, value)
return if !value.is_a?(HTTP::UploadedFile)
if max_name_size && value.filename && value.filename.not_nil!.size > max_name_size.not_nil!
schema.errors.add(
id,
I18n.t("marten.schema.field.file.errors.file_name_too_long", max_name_size: max_name_size)
)
end
if !allow_empty_files? && value.size == 0
schema.errors.add(id, I18n.t("marten.schema.field.file.errors.empty"))
end
end
end
end
end
end