schemacop 3.0.0.rc2 → 3.0.0.rc3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 73279c1805de5438933af200c883a7f6a813878d583c92bf7e597580f392df95
4
- data.tar.gz: 2ac5ce2925de12c5df8a4f82086d967c68d31cd9f8c598cff0da9231d221f5ff
3
+ metadata.gz: b93149de6cee935353aba3381d9e1ee76b51bc4b19763d8634566d375208fcac
4
+ data.tar.gz: cb8e1a8b82104f6482d5017d90e3d5c2094d564ac9ef1001d2336bac137886cb
5
5
  SHA512:
6
- metadata.gz: c04434b491dac3c09018969b13293c029347011a58386234ea5dcca6b3aa7ed8ebe3f291b8d819869d79b3c8c80d5a998bbbca0a95d928988367e04efcad4ec2
7
- data.tar.gz: 75564dd05800d295f3eb7ebb606f97ea67f2c19512a62193d49651cc77828836cfa95977465692ac85a5738a8407c7a2275a7cbe34db110c8400e70aa5057e95
6
+ metadata.gz: 0df1ba4b0c314901d015ca969996ffa9df862c79df0a52129bc3b749add7ff2e60ff52a9fb8a874f82b57017fef821c1e8d5d703093e0fd3bade224e8c84d259
7
+ data.tar.gz: 1f501b7fdee6cc8347fe1acaf522f268ef5882e595f0c136c68c773b98615b37a05aeeaf1b472c3152546cdd3ae0859dd216eae9850f9f2992c2e970c7111157
@@ -1,4 +1,3 @@
1
1
  version_file: VERSION
2
2
  always_from_master: true
3
- yard_path: doc
4
3
  gem_style: github
@@ -10,13 +10,19 @@
10
10
  ### Changes
11
11
  -->
12
12
 
13
- ## 3.0.0.r2
13
+ ## 3.0.0.rc3 (2020-01-28)
14
+
15
+ * Add minor improvements to the documentation
16
+
17
+ * Internal restructuring, no changes in API
18
+
19
+ ## 3.0.0.rc2 (2020-01-28)
14
20
 
15
21
  * Represent node names as strings internally
16
22
 
17
23
  * Update documentation
18
24
 
19
- ## 3.0.0.rc1
25
+ ## 3.0.0.rc1 (2020-01-22)
20
26
 
21
27
  * Add support for ruby `3.0.0`
22
28
 
@@ -24,7 +30,7 @@
24
30
 
25
31
  * Document all `v3` nodes
26
32
 
27
- ## 3.0.0.rc0
33
+ ## 3.0.0.rc0 (2020-01-14)
28
34
 
29
35
  * Add `Schemacop::Schema3`
30
36
 
@@ -24,7 +24,7 @@
24
24
 
25
25
  ## Validation
26
26
 
27
- Using schemacop, you can either choose to validate the data either using the
27
+ Using Schemacop, you can either choose to validate your data either using the
28
28
  graceful `validate` method, or the bang variant, `validate!`.
29
29
 
30
30
  The `validate` method on a schema with some supplied data will return a
@@ -68,14 +68,14 @@ schema.validate!('Foo') # => Schemacop::Exceptions::ValidationError: /:
68
68
 
69
69
  Schemacop can raise the following exceptions:
70
70
 
71
- * `Schemacop::Exceptions::ValidationError`: This exception is raised when the `validate!`
72
- method is used, and the data that was passed in is invalid. The exception message contains
73
- additional informations why the validation failed.
71
+ * `Schemacop::Exceptions::ValidationError`: This exception is raised when the
72
+ `validate!` method is used, and the data that was passed in is invalid. The
73
+ exception message contains additional information why the validation failed.
74
74
 
75
75
  Example:
76
76
 
77
77
  ```ruby
78
- schema = Schemacop::Schema3.new :hash do
78
+ schema = Schemacop::Schema3.new do
79
79
  int! :foo
80
80
  end
81
81
 
@@ -83,14 +83,14 @@ Schemacop can raise the following exceptions:
83
83
  # => Schemacop::Exceptions::ValidationError: /foo: Invalid type, expected "integer".
84
84
  ```
85
85
 
86
- * `Schemacop::Exceptions::InvalidSchemaError`: This exception is raised when the schema
87
- itself is not valid. The exception message contains additional informations why the
88
- validation failed.
86
+ * `Schemacop::Exceptions::InvalidSchemaError`: This exception is raised when the
87
+ schema itself is not valid. The exception message contains additional
88
+ information why the validation failed.
89
89
 
90
90
  Example:
91
91
 
92
- ```ruby
93
- Schemacop::Schema3.new :hash do
92
+ ```ruby
93
+ Schemacop::Schema3.new do
94
94
  int!
95
95
  end
96
96
 
@@ -109,7 +109,6 @@ The nodes in Schemacop v3 also support generic keywords, similar to JSON schema:
109
109
  * `default`: You may provide a default value for items that will be set if the
110
110
  value is not given
111
111
 
112
-
113
112
  The three keywords `title`, `description` and `examples` aren't used for validation,
114
113
  but can be used to document the schema. They will be included in the JSON output
115
114
  when you use the `as_json` method:
@@ -134,7 +133,7 @@ schema.validate!('bar') # => "bar"
134
133
  schema.validate!('baz') # => Schemacop::Exceptions::ValidationError: /: Value not included in enum ["foo", "bar"].
135
134
  ```
136
135
 
137
- Please note, that you can also specify values in the enum that are not valid for
136
+ Please note that you can also specify values in the enum that are not valid for
138
137
  the schema. This means that the validation will still fail:
139
138
 
140
139
  ```ruby
@@ -196,9 +195,9 @@ transformed into various types.
196
195
  #### Options
197
196
 
198
197
  * `min_length`
199
- Defines the minimum required string length
198
+ Defines the (inclusive) minimum required string length
200
199
  * `max_length`
201
- Defines the maximum required string length
200
+ Defines the (inclusive) maximum required string length
202
201
  * `pattern`
203
202
  Defines a (ruby) regex pattern the value will be matched against. Must be a
204
203
  string and should generally start with `^` and end with `$` so as to evaluate
@@ -230,7 +229,7 @@ transformed into various types.
230
229
 
231
230
  * `boolean`
232
231
  The string must be either `true` or `false`. This value will be casted to
233
- ruby's `TrueClass` or `FalseClass`.
232
+ Ruby's `TrueClass` or `FalseClass`.
234
233
 
235
234
  * `binary`
236
235
  The string is expected to contain binary contents. No casting or additional
@@ -403,11 +402,11 @@ It consists of one or multiple values, which can be validated using arbitrary no
403
402
 
404
403
  #### Contains
405
404
 
406
- The `array` node features the contains node, which you can use with the DSL
407
- method `cont`. With that DSL method, you can specify a schema which at least
408
- one item in the array needs to validate against.
405
+ The `array` node features the *contains* node, which you can use with the DSL
406
+ method `cont`. With that DSL method, you can specify a schema which at least one
407
+ item in the array needs to validate against.
409
408
 
410
- One usecase for example could be that you want an array of integers, from which
409
+ One use case for example could be that you want an array of integers, from which
411
410
  at least one must be 5 or larger:
412
411
 
413
412
  ```ruby
@@ -447,9 +446,10 @@ how you specify your array contents.
447
446
 
448
447
  List validation validates a sequence of arbitrary length where each item matches
449
448
  the same schema. Unless you specify a `min_items` count on the array node, an
450
- empty array will also validate. To specify a list validation, use the `list`
451
- DSL method, and specify the type you want to validate against. Here, you need
452
- to specify the type of the element using the long `type` name (e.g. `integer` and not `int`).
449
+ empty array will also suffice. To specify a list validation, use the `list` DSL
450
+ method, and specify the type you want to validate against. Here, you need to
451
+ specify the type of the element using the long `type` name (e.g. `integer` and
452
+ not `int`).
453
453
 
454
454
  For example, you can specify that you want an array with only integers between 1 and 5:
455
455
 
@@ -542,8 +542,8 @@ schema.validate!([1, 'foo', 'bar']) # => Schemacop::Exceptions::ValidationError:
542
542
  schema.validate!([1, 'foo', 2, 3]) # => [1, "foo", 2, 3]
543
543
  ```
544
544
 
545
- Please note, that you cannot use multiple `add` in the same array schema, this will result in
546
- an exception:
545
+ Please note that you cannot use multiple `add` in the same array schema, this
546
+ will result in an exception:
547
547
 
548
548
  ```ruby
549
549
  schema = Schemacop::Schema3.new :array do
@@ -555,9 +555,10 @@ end
555
555
  # => Schemacop::Exceptions::InvalidSchemaError: You can only use "add" once to specify additional items.
556
556
  ```
557
557
 
558
- If you want to specify that your schema accept multiple additional types, use the `one_of`
559
- type (see below for more infos). The correct way to specify that you want to allow additional
560
- items, which may be an integer or a string is as follows:
558
+ If you want to specify that your schema accept multiple additional types, use
559
+ the `one_of` type (see below for more infos). The correct way to specify that
560
+ you want to allow additional items, which may be an integer or a string is as
561
+ follows:
561
562
 
562
563
  ```ruby
563
564
  schema = Schemacop::Schema3.new :array do
@@ -592,7 +593,7 @@ It consists of key-value-pairs that can be validated using arbitrary nodes.
592
593
  * `property_names`
593
594
  This option allows to specify a regexp pattern (as string) which validates the
594
595
  keys of any properties that are not specified in the hash. This option only
595
- makes sense if `additional_properties` is enabled. See below for more informations.
596
+ makes sense if `additional_properties` is enabled. See below for more information.
596
597
 
597
598
  * `min_properties`
598
599
  Specifies the (inclusive) minimum number of properties a hash must contain.
@@ -645,8 +646,8 @@ schema.validate!('foo' => 42) # => {"foo"=>42}
645
646
 
646
647
  The result in both cases will be a
647
648
  [HashWithIndifferentAccess](https://api.rubyonrails.org/classes/ActiveSupport/HashWithIndifferentAccess.html),
648
- which means that you can access the data in the hash with the symbol as well
649
- as the string representation:
649
+ which means that you can access the data in the hash with the symbol as well as
650
+ the string representation:
650
651
 
651
652
  ```ruby
652
653
  schema = Schemacop::Schema3.new :hash do
@@ -660,9 +661,9 @@ result[:foo] # => 42
660
661
  result['foo'] # 42
661
662
  ```
662
663
 
663
- Please note, that if you specify the value twice in the data you want to validate,
664
- once with the key being a symbol and once being a string, Schemacop will raise an
665
- error:
664
+ Please note that if you specify the value twice in the data you want to
665
+ validate, once with the key being a symbol and once being a string, Schemacop
666
+ will raise an error:
666
667
 
667
668
  ```ruby
668
669
  schema = Schemacop::Schema3.new :hash do
@@ -798,10 +799,11 @@ schema.validate!({
798
799
  Type: `:object`\
799
800
  DSL: `obj`
800
801
 
801
- The object type represents a ruby `Object`. Please note that the `as_json` method
802
- on nodes of this type will just return `{}` (an empty JSON object), as there isn't
803
- a useful way to represent a ruby object without conflicting with the `Hash` type.
804
- If you want to represent an JSON object, you should use the `Hash` node.
802
+ The object type represents a Ruby `Object`. Please note that the `as_json`
803
+ method on nodes of this type will just return `{}` (an empty JSON object), as
804
+ there isn't a useful way to represent a Ruby object without conflicting with the
805
+ `Hash` type. If you want to represent a JSON object, you should use the `Hash`
806
+ node.
805
807
 
806
808
  In the most basic form, this node will accept anything:
807
809
 
@@ -882,7 +884,7 @@ schema.validate!('foooo') # => Schemacop::Exceptions::ValidationError: /: Does n
882
884
  Type: `:any_of`\
883
885
  DSL: `any_of`
884
886
 
885
- Similar to the AllOf node, you can specify multiple schemas, for which the
887
+ Similar to the `all_of` node, you can specify multiple schemas, for which the
886
888
  given value needs to validate against at least one of the schemas.
887
889
 
888
890
  For example, your value needs to be either a string which is at least 2
@@ -899,7 +901,7 @@ schema.validate!('foo') # => "foo"
899
901
  schema.validate!(42) # => 42
900
902
  ```
901
903
 
902
- Please note that you need to specify at least one item in the AllOf node:
904
+ Please note that you need to specify at least one item in the `any_of` node:
903
905
 
904
906
  ```ruby
905
907
  Schemacop::Schema3.new :any_of # => Schemacop::Exceptions::InvalidSchemaError: Node "any_of" makes only sense with at least 1 item.
@@ -910,9 +912,9 @@ Schemacop::Schema3.new :any_of # => Schemacop::Exceptions::InvalidSchemaError: N
910
912
  Type: `:one_of`\
911
913
  DSL: `one_of`
912
914
 
913
- Similar to the AllOf node, you can specify multiple schemas, for which the
914
- given value needs to validate against at exaclty one of the schemas. If the
915
- given value validates against multiple schemas, the value is invalid.
915
+ Similar to the `all_of` node, you can specify multiple schemas, for which the
916
+ given value needs to validate against exaclty one of the schemas. If the given
917
+ value validates against multiple schemas, the value is invalid.
916
918
 
917
919
  For example, if you want an integer which is either a multiple of 2 or 3,
918
920
  but not both (i.e. no multiple of 6), you could do it as follows:
@@ -951,7 +953,7 @@ schema.validate!(6) # => Schemacop::Exceptions::ValidationError: /: Matches 2 de
951
953
  Type: `:is_not`\
952
954
  DSL: `is_not`
953
955
 
954
- With the IsNot node, you can specify a schema which the given value must not
956
+ With the `is_not` node, you can specify a schema which the given value must not
955
957
  validate against, i.e. every value which matches the schema will make this node
956
958
  invalid.
957
959
 
@@ -969,7 +971,7 @@ schema.validate!(3) # => Schemacop::Exceptions::ValidationError: /: Must not
969
971
  schema.validate!('foo') # => "foo"
970
972
  ```
971
973
 
972
- Note that a IsNot node needs exactly one item:
974
+ Note that a `is_not` node needs exactly one item:
973
975
 
974
976
  ```ruby
975
977
  schema = Schemacop::Schema3.new :is_not # => Schemacop::Exceptions::InvalidSchemaError: Node "is_not" only allows exactly one item.
@@ -984,7 +986,7 @@ Type: `reference`
984
986
  **Definition**
985
987
  DSL: `scm`
986
988
 
987
- Finally, with the Reference node, you can define schemas and then later reference
989
+ Finally, with the *Reference* node, you can define schemas and then later reference
988
990
  them for usage, e.g. when you have a rather long schema which you need at multiple
989
991
  places.
990
992
 
@@ -1052,7 +1054,7 @@ schema.validate!([id: 42, first_name: 'Joe']) # => Schemacop::Except
1052
1054
 
1053
1055
  ## Context
1054
1056
 
1055
- Schemacop als features the concept of a `Context`. You can define schemas in a
1057
+ Schemacop also features the concept of a `Context`. You can define schemas in a
1056
1058
  context, and then reference them in other schemas in that context. This is e.g.
1057
1059
  useful if you need a part of the schema to be different depending on the
1058
1060
  business action.
@@ -1076,7 +1078,7 @@ context.schema :PersonInfo do
1076
1078
  end
1077
1079
 
1078
1080
  # Now we can define our general schema, where we reference the :Person schema.
1079
- # Note that at this point, we don't know what's in the :Person sche,a
1081
+ # Note that at this point, we don't know what's in the :Person schema.
1080
1082
  schema = Schemacop::Schema3.new :reference, path: :Person
1081
1083
 
1082
1084
  # Validate the data in the context we defined before, where we need the first_name
@@ -1097,7 +1099,7 @@ other_context.schema :Person do
1097
1099
  end
1098
1100
 
1099
1101
  # Finally, validate the data in the new context. We do not want the real name or
1100
- # birth date of the person, instead only the nickname is allowed
1102
+ # birth date of the person, instead only the nickname is allowed.
1101
1103
  Schemacop.with_context other_context do
1102
1104
  schema.validate!({first_name: 'Joe', last_name: 'Doe', info: { born_at: '1980-01-01'} })
1103
1105
  # => Schemacop::Exceptions::ValidationError: /nickname: Value must be given.
@@ -1115,11 +1117,11 @@ to use other data in the second context than in the first.
1115
1117
 
1116
1118
  ## External schemas
1117
1119
 
1118
- Finally, schemacop features the possibilit to specify schemas in seperate files.
1119
- This is especially useful is you have schemas in your application which are used
1120
- multiple times through the application.
1120
+ Finally, Schemacop features the possibility to specify schemas in seperate
1121
+ files. This is especially useful is you have schemas in your application which
1122
+ are used multiple times throughout the application.
1121
1123
 
1122
- For each schema, you define the schema in a single file, and after loading the
1124
+ For each schema, you define the schema in a separate file, and after loading the
1123
1125
  schemas, you can reference them in other schemas.
1124
1126
 
1125
1127
  The default load path is `'app/schemas'`, but this can be configured by setting
@@ -1192,4 +1194,4 @@ however you need to eager load the schemas yourself:
1192
1194
 
1193
1195
  ```ruby
1194
1196
  Schemacop::V3::GlobalContext.eager_load!
1195
- ```
1197
+ ```
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.0.0.rc2
1
+ 3.0.0.rc3
@@ -10,7 +10,6 @@ require 'schemacop/v2/node'
10
10
  require 'schemacop/v2/node_with_block'
11
11
  require 'schemacop/v2/node_supporting_type'
12
12
  require 'schemacop/v2/field_node'
13
- require 'schemacop/v2/root_node'
14
13
  require 'schemacop/v2/node_supporting_field'
15
14
  require 'schemacop/v2/caster'
16
15
  require 'schemacop/v2/dupper'
@@ -1,6 +0,0 @@
1
- module Schemacop
2
- module V2
3
- class RootNode < NodeSupportingType
4
- end
5
- end
6
- end
@@ -11,6 +11,8 @@ module Schemacop
11
11
 
12
12
  supports_children(name: true)
13
13
 
14
+ attr_reader :properties
15
+
14
16
  def self.allowed_options
15
17
  super + ATTRIBUTES - %i[dependencies] + %i[additional_properties]
16
18
  end
@@ -19,6 +21,14 @@ module Schemacop
19
21
  super + NodeRegistry.dsl_methods(true) + %i[dsl_dep dsl_add]
20
22
  end
21
23
 
24
+ def self.sanitize_exp(exp)
25
+ exp = exp.to_s
26
+ if exp.start_with?('(?-mix:')
27
+ exp = exp.to_s.gsub(/^\(\?-mix:/, '').gsub(/\)$/, '')
28
+ end
29
+ return exp
30
+ end
31
+
22
32
  def add_child(node)
23
33
  unless node.name
24
34
  fail Exceptions::InvalidSchemaError, 'Child nodes must have a name.'
@@ -54,7 +64,7 @@ module Schemacop
54
64
 
55
65
  json = {}
56
66
  json[:properties] = Hash[properties.values.map { |p| [p.name, p.as_json] }] if properties.any?
57
- json[:patternProperties] = Hash[pattern_properties.values.map { |p| [sanitize_exp(p.name), p.as_json] }] if pattern_properties.any?
67
+ json[:patternProperties] = Hash[pattern_properties.values.map { |p| [self.class.sanitize_exp(p.name), p.as_json] }] if pattern_properties.any?
58
68
 
59
69
  # In schemacop, by default, additional properties are not allowed,
60
70
  # the users explicitly need to enable additional properties
@@ -75,14 +85,6 @@ module Schemacop
75
85
  return process_json(ATTRIBUTES, json)
76
86
  end
77
87
 
78
- def sanitize_exp(exp)
79
- exp = exp.to_s
80
- if exp.start_with?('(?-mix:')
81
- exp = exp.to_s.gsub(/^\(\?-mix:/, '').gsub(/\)$/, '')
82
- end
83
- return exp
84
- end
85
-
86
88
  def allowed_types
87
89
  { Hash => :object }
88
90
  end
@@ -191,10 +193,10 @@ module Schemacop
191
193
  next
192
194
  end
193
195
 
194
- result[prop.name] = prop.cast(data_hash[prop.name])
196
+ result[prop.as || prop.name] = prop.cast(data_hash[prop.name])
195
197
 
196
- if result[prop.name].nil? && !data_hash.include?(prop.name)
197
- result.delete(prop.name)
198
+ if result[prop.as || prop.name].nil? && !data_hash.include?(prop.name)
199
+ result.delete(prop.as || prop.name)
198
200
  end
199
201
  end
200
202
 
@@ -2,6 +2,7 @@ module Schemacop
2
2
  module V3
3
3
  class Node
4
4
  attr_reader :name
5
+ attr_reader :as
5
6
  attr_reader :default
6
7
  attr_reader :title
7
8
  attr_reader :description
@@ -32,8 +33,10 @@ module Schemacop
32
33
  if options.delete(:cast_str)
33
34
  format = NodeRegistry.name(klass)
34
35
  one_of_options = {
35
- required: options.delete(:required),
36
- name: options.delete(:name)
36
+ required: options.delete(:required),
37
+ name: options.delete(:name),
38
+ as: options.delete(:as),
39
+ description: options.delete(:description)
37
40
  }
38
41
  node = create(:one_of, **one_of_options) do
39
42
  self.node node
@@ -45,7 +48,7 @@ module Schemacop
45
48
  end
46
49
 
47
50
  def self.allowed_options
48
- %i[name required default description examples enum parent options cast_str title]
51
+ %i[name required default description examples enum parent options cast_str title as]
49
52
  end
50
53
 
51
54
  def self.dsl_methods
@@ -75,6 +78,7 @@ module Schemacop
75
78
  # Assign attributes #
76
79
  @name = options.delete(:name)
77
80
  @name = @name.to_s unless @name.nil? || @name.is_a?(Regexp)
81
+ @as = options.delete(:as)
78
82
  @required = !!options.delete(:required)
79
83
  @default = options.delete(:default)
80
84
  @title = options.delete(:title)
@@ -1,9 +1,9 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: schemacop 3.0.0.rc2 ruby lib
2
+ # stub: schemacop 3.0.0.rc3 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "schemacop".freeze
6
- s.version = "3.0.0.rc2"
6
+ s.version = "3.0.0.rc3"
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new("> 1.3.1".freeze) if s.respond_to? :required_rubygems_version=
9
9
  s.require_paths = ["lib".freeze]
metadata CHANGED
@@ -1,11 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schemacop
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0.rc2
4
+ version: 3.0.0.rc3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sitrox
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2021-01-28 00:00:00.000000000 Z
@@ -150,8 +150,8 @@ dependencies:
150
150
  - - '='
151
151
  - !ruby/object:Gem::Version
152
152
  version: 0.21.2
153
- description:
154
- email:
153
+ description:
154
+ email:
155
155
  executables: []
156
156
  extensions: []
157
157
  extra_rdoc_files: []
@@ -264,7 +264,7 @@ homepage: https://github.com/sitrox/schemacop
264
264
  licenses:
265
265
  - MIT
266
266
  metadata: {}
267
- post_install_message:
267
+ post_install_message:
268
268
  rdoc_options: []
269
269
  require_paths:
270
270
  - lib
@@ -279,8 +279,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
279
279
  - !ruby/object:Gem::Version
280
280
  version: 1.3.1
281
281
  requirements: []
282
- rubygems_version: 3.0.3
283
- signing_key:
282
+ rubygems_version: 3.1.4
283
+ signing_key:
284
284
  specification_version: 4
285
285
  summary: Schemacop validates ruby structures consisting of nested hashes and arrays
286
286
  against simple schema definitions.