Module: Jimmy::Declaration
- Defined in:
- lib/jimmy/declaration.rb,
lib/jimmy/declaration/types.rb,
lib/jimmy/declaration/number.rb,
lib/jimmy/declaration/object.rb,
lib/jimmy/declaration/string.rb,
lib/jimmy/declaration/casting.rb,
lib/jimmy/declaration/assertion.rb,
lib/jimmy/declaration/composites.rb,
lib/jimmy/declaration/conditions.rb
Overview
Contains methods for declaring or modifying schemas.
Constant Summary collapse
- SIMPLE_TYPES =
Acceptable values for
#type
. Set.new(%w[array boolean integer null number object string]).freeze
- FORMATS =
Set.new( %w[ date-time date time email idn-email hostname idn-hostname ipv4 ipv6 uri uri-reference iri iri-reference uri-template json-pointer relative-json-pointer regex ] ).freeze
- BOOLEANS =
Set.new([true, false]).freeze
Instance Method Summary collapse
-
#all_of(*schemas) ⇒ self
Set the
allOf
value for the schema. -
#any_of(*schemas) ⇒ self
Set the
anyOf
value for the schema. -
#array ⇒ Schema
Make the schema allow type “array”.
-
#boolean ⇒ Schema
Make the schema allow type “boolean”.
-
#const(constant_value) ⇒ self
Set a constant value that will be expected to match exactly.
-
#date ⇒ Schema
Validate a string with format “date”.
-
#date_time ⇒ Schema
Validate a string with format “date-time”.
-
#default(default) ⇒ self
Set the default value for the schema.
-
#define(name, schema = Schema.new) {|schema| ... } ⇒ self
Add a schema to this schema’s
definitions
property. -
#definitions(definitions) {|name, schema| ... } ⇒ self
Add definitions to the schema’s
definitions
property. -
#description(description) ⇒ self
Set the description of the schema.
-
#email ⇒ Schema
Validate a string with format “email”.
-
#enum(allowed_values) ⇒ self
Set an enum value for the schema.
-
#examples(*examples) ⇒ self
(also: #example)
Add examples to the schema.
-
#hostname ⇒ Schema
Validate a string with format “hostname”.
-
#idn_email ⇒ Schema
Validate a string with format “idn-email”.
-
#idn_hostname ⇒ Schema
Validate a string with format “idn-hostname”.
-
#if(schema, then_schema = nil, else_schema = nil) ⇒ self
Define the schema that determines whether the
then
orelse
schemas must be valid. -
#integer ⇒ Schema
Make the schema allow type “integer”.
-
#ipv4 ⇒ Schema
Validate a string with format “ipv4”.
-
#ipv6 ⇒ Schema
Validate a string with format “ipv6”.
-
#iri ⇒ Schema
Validate a string with format “iri”.
-
#iri_reference ⇒ Schema
Validate a string with format “iri-reference”.
-
#json_pointer ⇒ Schema
Validate a string with format “json-pointer”.
-
#multiple_of(number) ⇒ self
Set the number of which the value should be a multiple.
-
#not(schema) ⇒ self
Define the schema that this schema must not match.
-
#null ⇒ Schema
(also: #nullable)
Make the schema allow type “null”.
-
#number ⇒ Schema
Make the schema allow type “number”.
-
#object ⇒ Schema
Make the schema allow type “object”.
-
#one_of(*schemas) ⇒ self
Set the
oneOf
value for the schema. -
#pattern(expression) ⇒ self
Set the pattern for a string value.
-
#range(range) ⇒ self
Set minimum and maximum by providing a range.
-
#read_only(is_read_only = true) ⇒ self
Set whether the schema is read-only.
-
#regex ⇒ Schema
Validate a string with format “regex”.
-
#relative_json_pointer ⇒ Schema
Validate a string with format “relative-json-pointer”.
-
#string ⇒ Schema
Make the schema allow type “string”.
-
#struct ⇒ Jimmy::Schema
Shortcut for object.additional_properties(false).
-
#time ⇒ Schema
Validate a string with format “time”.
-
#title(title) ⇒ self
Set the title of the schema.
-
#type(*types) ⇒ self
(also: #types)
Set the type(s) of the schema.
-
#uri ⇒ Schema
Validate a string with format “uri”.
-
#uri_reference ⇒ Schema
Validate a string with format “uri-reference”.
-
#uri_template ⇒ Schema
Validate a string with format “uri-template”.
-
#write_only(is_write_only = true) ⇒ self
Set whether the schema is write-only.
Instance Method Details
#all_of(*schemas) ⇒ self
Set the allOf
value for the schema.
17 18 19 |
# File 'lib/jimmy/declaration/composites.rb', line 17 def all_of(*schemas) set_composite 'allOf', schemas.flatten end |
#any_of(*schemas) ⇒ self
Set the anyOf
value for the schema.
9 10 11 |
# File 'lib/jimmy/declaration/composites.rb', line 9 def any_of(*schemas) set_composite 'anyOf', schemas.flatten end |
#array ⇒ Schema
Make the schema allow type “array”.
|
# File 'lib/jimmy/declaration/types.rb', line 33
|
#boolean ⇒ Schema
Make the schema allow type “boolean”.
|
# File 'lib/jimmy/declaration/types.rb', line 33
|
#const(constant_value) ⇒ self
Set a constant value that will be expected to match exactly.
59 60 61 |
# File 'lib/jimmy/declaration.rb', line 59 def const(constant_value) set const: constant_value end |
#date ⇒ Schema
Validate a string with format “date”.
|
# File 'lib/jimmy/declaration/string.rb', line 44
|
#date_time ⇒ Schema
Validate a string with format “date-time”.
|
# File 'lib/jimmy/declaration/string.rb', line 44
|
#default(default) ⇒ self
Set the default value for the schema.
35 36 37 |
# File 'lib/jimmy/declaration.rb', line 35 def default(default) set default: default end |
#define(name, schema = Schema.new) {|schema| ... } ⇒ self
Add a schema to this schema’s definitions
property.
87 88 89 90 91 |
# File 'lib/jimmy/declaration.rb', line 87 def define(name, schema = Schema.new, &block) return definitions name, &block if name.is_a? Hash assign_to_schema_hash 'definitions', name, schema, &block end |
#definitions(definitions) {|name, schema| ... } ⇒ self
Add definitions to the schema’s definitions
property.
101 102 103 |
# File 'lib/jimmy/declaration.rb', line 101 def definitions(definitions, &block) batch_assign_to_schema_hash 'definitions', definitions, &block end |
#description(description) ⇒ self
Set the description of the schema.
27 28 29 30 |
# File 'lib/jimmy/declaration.rb', line 27 def description(description) assert_string description set description: description end |
#email ⇒ Schema
Validate a string with format “email”.
|
# File 'lib/jimmy/declaration/string.rb', line 44
|
#enum(allowed_values) ⇒ self
Set an enum value for the schema.
66 67 68 69 70 |
# File 'lib/jimmy/declaration.rb', line 66 def enum(allowed_values) allowed_values = allowed_values.to_a if allowed_values.is_a? Set assert_array allowed_values, minimum: 1, unique: true set enum: allowed_values end |
#examples(*examples) ⇒ self Also known as: example
Add examples to the schema
75 76 77 78 |
# File 'lib/jimmy/declaration.rb', line 75 def examples(*examples) getset('examples') { [] }.concat examples self end |
#hostname ⇒ Schema
Validate a string with format “hostname”.
|
# File 'lib/jimmy/declaration/string.rb', line 44
|
#idn_email ⇒ Schema
Validate a string with format “idn-email”.
|
# File 'lib/jimmy/declaration/string.rb', line 44
|
#idn_hostname ⇒ Schema
Validate a string with format “idn-hostname”.
|
# File 'lib/jimmy/declaration/string.rb', line 44
|
#if(schema, then_schema = nil, else_schema = nil) ⇒ self
Define the schema that determines whether the then
or else
schemas must be valid.
11 12 13 14 15 16 |
# File 'lib/jimmy/declaration/conditions.rb', line 11 def if(schema, then_schema = nil, else_schema = nil) set(if: cast_schema(schema)).tap do |s| s.then then_schema unless then_schema.nil? s.else else_schema unless else_schema.nil? end end |
#integer ⇒ Schema
Make the schema allow type “integer”.
|
# File 'lib/jimmy/declaration/types.rb', line 33
|
#ipv4 ⇒ Schema
Validate a string with format “ipv4”.
|
# File 'lib/jimmy/declaration/string.rb', line 44
|
#ipv6 ⇒ Schema
Validate a string with format “ipv6”.
|
# File 'lib/jimmy/declaration/string.rb', line 44
|
#iri ⇒ Schema
Validate a string with format “iri”.
|
# File 'lib/jimmy/declaration/string.rb', line 44
|
#iri_reference ⇒ Schema
Validate a string with format “iri-reference”.
|
# File 'lib/jimmy/declaration/string.rb', line 44
|
#json_pointer ⇒ Schema
Validate a string with format “json-pointer”.
|
# File 'lib/jimmy/declaration/string.rb', line 44
|
#multiple_of(number) ⇒ self
Set the number of which the value should be a multiple.
8 9 10 11 12 13 |
# File 'lib/jimmy/declaration/number.rb', line 8 def multiple_of(number) valid_for 'number', 'integer' assert_numeric number assert(number.positive?) { "Expected #{number} to be positive" } set multipleOf: number end |
#not(schema) ⇒ self
Define the schema that this schema must not match.
108 109 110 111 |
# File 'lib/jimmy/declaration.rb', line 108 def not(schema) # TODO: combine more nots into an anyOf set not: cast_schema(schema) end |
#null ⇒ Schema Also known as: nullable
Make the schema allow type “null”.
|
# File 'lib/jimmy/declaration/types.rb', line 33
|
#number ⇒ Schema
Make the schema allow type “number”.
|
# File 'lib/jimmy/declaration/types.rb', line 33
|
#object ⇒ Schema
Make the schema allow type “object”.
|
# File 'lib/jimmy/declaration/types.rb', line 33
|
#one_of(*schemas) ⇒ self
Set the oneOf
value for the schema.
25 26 27 |
# File 'lib/jimmy/declaration/composites.rb', line 25 def one_of(*schemas) set_composite 'oneOf', schemas.flatten end |
#pattern(expression) ⇒ self
Set the pattern for a string value.
31 32 33 34 |
# File 'lib/jimmy/declaration/string.rb', line 31 def pattern(expression) assert_regexp expression set(pattern: expression.source).string end |
#range(range) ⇒ self
Set minimum and maximum by providing a range.
18 19 20 21 22 23 24 25 |
# File 'lib/jimmy/declaration/number.rb', line 18 def range(range) assert_range range schema do |s| s.type range.begin.is_a?(Integer) ? 'integer' : 'number' s.minimum range.begin s.maximum range.end, exclusive: range.exclude_end? unless range.end.nil? end end |
#read_only(is_read_only = true) ⇒ self
Set whether the schema is read-only.
42 43 44 45 |
# File 'lib/jimmy/declaration.rb', line 42 def read_only(is_read_only = true) assert_boolean is_read_only set readOnly: is_read_only end |
#regex ⇒ Schema
Validate a string with format “regex”.
|
# File 'lib/jimmy/declaration/string.rb', line 44
|
#relative_json_pointer ⇒ Schema
Validate a string with format “relative-json-pointer”.
|
# File 'lib/jimmy/declaration/string.rb', line 44
|
#string ⇒ Schema
Make the schema allow type “string”.
|
# File 'lib/jimmy/declaration/types.rb', line 33
|
#struct ⇒ Jimmy::Schema
Shortcut for object.additional_properties(false).
7 8 9 |
# File 'lib/jimmy/declaration/object.rb', line 7 def struct object.additional_properties false end |
#time ⇒ Schema
Validate a string with format “time”.
|
# File 'lib/jimmy/declaration/string.rb', line 44
|
#title(title) ⇒ self
Set the title of the schema.
19 20 21 22 |
# File 'lib/jimmy/declaration.rb', line 19 def title(title) assert_string title set title: title end |
#type(*types) ⇒ self Also known as: types
Set the type(s) of the schema.
12 13 14 15 16 17 18 19 |
# File 'lib/jimmy/declaration/types.rb', line 12 def type(*types) types = types.flatten types.each &method(:assert_simple_type) assert_array types, unique: true, minimum: 1 types = Array(get('type') { [] }) | types.flatten types = types.first if types.one? set type: types end |
#uri ⇒ Schema
Validate a string with format “uri”.
|
# File 'lib/jimmy/declaration/string.rb', line 44
|
#uri_reference ⇒ Schema
Validate a string with format “uri-reference”.
|
# File 'lib/jimmy/declaration/string.rb', line 44
|
#uri_template ⇒ Schema
Validate a string with format “uri-template”.
|
# File 'lib/jimmy/declaration/string.rb', line 44
|
#write_only(is_write_only = true) ⇒ self
Set whether the schema is write-only.
50 51 52 53 |
# File 'lib/jimmy/declaration.rb', line 50 def write_only(is_write_only = true) assert_boolean is_write_only set writeOnly: is_write_only end |