-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support maxProperties and minProperties (#137)
https://swagger.io/docs/specification/data-models/data-types/ > The minProperties and maxProperties keywords let you restrict the number of properties allowed in an object. This can be useful when using additionalProperties or free-form objects.
- Loading branch information
Showing
5 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
class OpenAPIParser::SchemaValidator | ||
module PropertiesNumber | ||
# check minProperties and manProperties value by schema | ||
# @param [Object] value | ||
# @param [OpenAPIParser::Schemas::Schema] schema | ||
def check_properties_number(value, schema) | ||
include_properties_num = schema.minProperties || schema.maxProperties | ||
return [value, nil] unless include_properties_num | ||
|
||
validate(value, schema) | ||
[value, nil] | ||
rescue OpenAPIParser::OpenAPIError => e | ||
return [nil, e] | ||
end | ||
|
||
private | ||
|
||
def validate(value, schema) | ||
reference = schema.object_reference | ||
|
||
if schema.minProperties && (value.size < schema.minProperties) | ||
raise OpenAPIParser::LessThanMinProperties.new(value, reference) | ||
end | ||
|
||
if schema.maxProperties && (value.size > schema.maxProperties) | ||
raise OpenAPIParser::MoreThanMaxProperties.new(value, reference) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters