Skip to content

Commit 2b10895

Browse files
authored
update validations (#2)
update validations, and add tests for them
1 parent c96eb16 commit 2b10895

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Following validations are incorporated into models:
5454
- minimum length: must be a string value of length greater than or equal to a specified value
5555
- maximum item count: must be a list value with number of items less than or equal to a specified value
5656
- minimum item count: must be a list value with number of items greater than or equal to a specified value
57+
- pattern: must match the specified regex pattern
5758
- enum: value must be from a list of allowed values
5859

5960
Validations are imposed in the constructor and `setproperty!` methods of models.

src/val.jl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ function val_unique_items(val::Vector, is_unique)
1919
is_unique || return true
2020
return length(Set(val)) == length(val)
2121
end
22+
function val_pattern(val::AbstractString, pattern::Regex)
23+
return !isnothing(match(pattern, val))
24+
end
2225

2326
const MSG_INVALID_API_PARAM = Dict{Symbol,Function}([
2427
:maximum => (val,excl)->string("must be a value less than ", excl ? "or equal to " : "", val),
@@ -28,7 +31,10 @@ const MSG_INVALID_API_PARAM = Dict{Symbol,Function}([
2831
:maxItems => (val)->string("number of items must be less than or equal to ", val),
2932
:minItems => (val)->string("number of items must be greater than or equal to ", val),
3033
:uniqueItems => (val)->string("items must be unique"),
31-
:enum => (lst)->string("value is not from the allowed values", lst),
34+
:maxProperties => (val)->string("number of properties must be less than or equal to ", val),
35+
:minProperties => (val)->string("number of properties must be greater than or equal to ", val),
36+
:enum => (lst)->string("value is not from the allowed values ", lst),
37+
:pattern => (val)->string("value does not match required pattern"),
3238
])
3339

3440
const VAL_API_PARAM = Dict{Symbol,Function}([
@@ -39,8 +45,9 @@ const VAL_API_PARAM = Dict{Symbol,Function}([
3945
:maxItems => val_max_length,
4046
:minItems => val_min_length,
4147
:uniqueItems => val_unique_items,
42-
:maxProterties => val_max_length,
43-
:minProterties => val_min_length,
48+
:maxProperties => val_max_length,
49+
:minProperties => val_min_length,
50+
:pattern => val_pattern,
4451
:enum => val_enum,
4552
])
4653

test/client/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ function runtests()
1313
@testset "Utils" begin
1414
test_longpoll_exception_check()
1515
end
16+
@testset "Validations" begin
17+
test_validations()
18+
end
1619
@testset "Petstore" begin
1720
if get(ENV, "RUNNER_OS", "") == "Linux"
1821
@testset "V3" begin

test/client/utilstests.jl

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,52 @@ function test_longpoll_exception_check()
3030
@test OpenAPI.Clients.is_longpoll_timeout(CompositeException([openapiex1, as_taskfailedexception(openapiex2)]))
3131
@test OpenAPI.Clients.is_longpoll_timeout(CompositeException([openapiex1, as_taskfailedexception(openapiex1)])) == false
3232
end
33+
34+
function test_validations()
35+
# maximum
36+
@test_throws OpenAPI.ValidationException OpenAPI.validate_param("test_param", "test_model", :maximum, 11, 10, true)
37+
@test_throws OpenAPI.ValidationException OpenAPI.validate_param("test_param", "test_model", :maximum, 11, 10, false)
38+
@test_throws OpenAPI.ValidationException OpenAPI.validate_param("test_param", "test_model", :maximum, 10, 10, true)
39+
@test OpenAPI.validate_param("test_param", "test_model", :maximum, 10, 10, false) === nothing
40+
@test OpenAPI.validate_param("test_param", "test_model", :maximum, 1, 10, false) === nothing
41+
42+
# minimum
43+
@test_throws OpenAPI.ValidationException OpenAPI.validate_param("test_param", "test_model", :minimum, 10, 11, true)
44+
@test_throws OpenAPI.ValidationException OpenAPI.validate_param("test_param", "test_model", :minimum, 10, 11, false)
45+
@test_throws OpenAPI.ValidationException OpenAPI.validate_param("test_param", "test_model", :minimum, 10, 10, true)
46+
@test OpenAPI.validate_param("test_param", "test_model", :minimum, 10, 10, false) === nothing
47+
@test OpenAPI.validate_param("test_param", "test_model", :minimum, 10, 1, false) === nothing
48+
49+
# maxLength, maxItems, maxProperties
50+
for test in (:maxLength, :maxItems, :maxProperties)
51+
for items in (1:10, Dict(zip(1:10, 1:10)), [1:10...])
52+
@test OpenAPI.validate_param("test_param", "test_model", test, items, 10) === nothing
53+
end
54+
for items in (1:2, Dict(zip(1:2, 1:2)), [1:2...])
55+
@test OpenAPI.validate_param("test_param", "test_model", test, items, 10) === nothing
56+
end
57+
end
58+
59+
# minLength, minItems, minProperties
60+
for test in (:minLength, :minItems, :minProperties)
61+
for items in (1:10, Dict(zip(1:10, 1:10)), [1:10...])
62+
@test OpenAPI.validate_param("test_param", "test_model", test, items, 10) === nothing
63+
@test OpenAPI.validate_param("test_param", "test_model", test, items, 1) === nothing
64+
end
65+
end
66+
67+
# unique
68+
@test OpenAPI.validate_param("test_param", "test_model", :uniqueItems, [1, 2, 3], true) === nothing
69+
@test OpenAPI.validate_param("test_param", "test_model", :uniqueItems, [1, 2, 2], false) === nothing
70+
@test_throws OpenAPI.ValidationException OpenAPI.validate_param("test_param", "test_model", :uniqueItems, [1, 2, 2], true)
71+
72+
# pattern
73+
@test OpenAPI.validate_param("test_param", "test_model", :pattern, "test", r"[a-z]+") === nothing
74+
@test_throws OpenAPI.ValidationException OpenAPI.validate_param("test_param", "test_model", :pattern, "test", r"[0-9]+")
75+
76+
# enum
77+
@test OpenAPI.validate_param("test_param", "test_model", :enum, [:a, :b, :b], [:a, :b, :c]) === nothing
78+
@test_throws OpenAPI.ValidationException OpenAPI.validate_param("test_param", "test_model", :enum, [:a, :b, :c, :d], [:a, :b, :c])
79+
80+
return nothing
81+
end

0 commit comments

Comments
 (0)