schemacop 3.0.0.rc2 → 3.0.1

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: b87ad53205f0d50ca9f4fcc2916375616a1123b379bd621a896275e05b94279d
4
+ data.tar.gz: 89e105928ed6ae0078f251795d53298222393fcf5af750acd8d3930ea0ab0461
5
5
  SHA512:
6
- metadata.gz: c04434b491dac3c09018969b13293c029347011a58386234ea5dcca6b3aa7ed8ebe3f291b8d819869d79b3c8c80d5a998bbbca0a95d928988367e04efcad4ec2
7
- data.tar.gz: 75564dd05800d295f3eb7ebb606f97ea67f2c19512a62193d49651cc77828836cfa95977465692ac85a5738a8407c7a2275a7cbe34db110c8400e70aa5057e95
6
+ metadata.gz: 8d957a52a69e9f7174f850eb1a86c972fc44a4d5575f128db8152568cd317bc6c511093d137af4a13626eef12de029379b289f2bc29d4a2083e701adee247034
7
+ data.tar.gz: 7b9b7e640e70743d5ce7c5ae8dc0f6626c10d5ed0d627de174cbca622904e26a9e469f453099e1295f905436af8626e9874c9e0659949959779d3eca1ac815af
data/.releaser_config CHANGED
@@ -1,4 +1,3 @@
1
1
  version_file: VERSION
2
2
  always_from_master: true
3
- yard_path: doc
4
3
  gem_style: github
data/CHANGELOG.md CHANGED
@@ -10,13 +10,43 @@
10
10
  ### Changes
11
11
  -->
12
12
 
13
- ## 3.0.0.r2
13
+ ## 3.0.1 (2021-02-11)
14
+
15
+ * Add format `symbol` to strings
16
+
17
+ ## 3.0.0 (2021-02-08)
18
+
19
+ * Setup Zeitwerk ignores for Rails applications
20
+
21
+ * Read previous `3.0.0.rcX` entries for all changes included
22
+ in this stable release
23
+
24
+ ## 3.0.0.rc5 (2021-02-05)
25
+
26
+ * Use `ruby2_keywords` for compatibility with ruby `2.6.2`
27
+
28
+ ## 3.0.0.rc4 (2021-02-02)
29
+
30
+ * Fix some minor bugs
31
+
32
+ * Improve documentation
33
+
34
+ * `used_external_schemas` for the `ReferenceNode` is now applied
35
+ recursively
36
+
37
+ ## 3.0.0.rc3 (2021-01-28)
38
+
39
+ * Add minor improvements to the documentation
40
+
41
+ * Internal restructuring, no changes in API
42
+
43
+ ## 3.0.0.rc2 (2021-01-28)
14
44
 
15
45
  * Represent node names as strings internally
16
46
 
17
47
  * Update documentation
18
48
 
19
- ## 3.0.0.rc1
49
+ ## 3.0.0.rc1 (2021-01-22)
20
50
 
21
51
  * Add support for ruby `3.0.0`
22
52
 
@@ -24,7 +54,7 @@
24
54
 
25
55
  * Document all `v3` nodes
26
56
 
27
- ## 3.0.0.rc0
57
+ ## 3.0.0.rc0 (2021-01-14)
28
58
 
29
59
  * Add `Schemacop::Schema3`
30
60
 
data/README.md CHANGED
@@ -118,4 +118,4 @@ To run tests:
118
118
 
119
119
  ## Copyright
120
120
 
121
- Copyright (c) 2020 Sitrox. See `LICENSE` for further details.
121
+ Copyright © 2016 - 2021 Sitrox. See `LICENSE` for further details.
data/README_V3.md CHANGED
@@ -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,13 +83,13 @@ 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
92
+ ```ruby
93
93
  Schemacop::Schema3.new :hash do
94
94
  int!
95
95
  end
@@ -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
@@ -242,6 +241,9 @@ transformed into various types.
242
241
  * `number`
243
242
  The string must be a number and will be casted to a ruby `Float` object.
244
243
 
244
+ * `symbol`
245
+ The string can be anything and will be casted to a ruby `Symbol` object.
246
+
245
247
  #### Examples
246
248
 
247
249
  ```ruby
@@ -381,7 +383,7 @@ schema.validate!(1234) # => Schemacop::Exceptions::ValidationError: /: Invali
381
383
  ### Array
382
384
 
383
385
  Type: `:array`\
384
- DSL: `arr`
386
+ DSL: `ary`
385
387
 
386
388
  The array type represents a ruby `Array`.
387
389
  It consists of one or multiple values, which can be validated using arbitrary nodes.
@@ -403,11 +405,11 @@ It consists of one or multiple values, which can be validated using arbitrary no
403
405
 
404
406
  #### Contains
405
407
 
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.
408
+ The `array` node features the *contains* node, which you can use with the DSL
409
+ method `cont`. With that DSL method, you can specify a schema which at least one
410
+ item in the array needs to validate against.
409
411
 
410
- One usecase for example could be that you want an array of integers, from which
412
+ One use case for example could be that you want an array of integers, from which
411
413
  at least one must be 5 or larger:
412
414
 
413
415
  ```ruby
@@ -447,9 +449,10 @@ how you specify your array contents.
447
449
 
448
450
  List validation validates a sequence of arbitrary length where each item matches
449
451
  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`).
452
+ empty array will also suffice. To specify a list validation, use the `list` DSL
453
+ method, and specify the type you want to validate against. Here, you need to
454
+ specify the type of the element using the long `type` name (e.g. `integer` and
455
+ not `int`).
453
456
 
454
457
  For example, you can specify that you want an array with only integers between 1 and 5:
455
458
 
@@ -542,8 +545,8 @@ schema.validate!([1, 'foo', 'bar']) # => Schemacop::Exceptions::ValidationError:
542
545
  schema.validate!([1, 'foo', 2, 3]) # => [1, "foo", 2, 3]
543
546
  ```
544
547
 
545
- Please note, that you cannot use multiple `add` in the same array schema, this will result in
546
- an exception:
548
+ Please note that you cannot use multiple `add` in the same array schema, this
549
+ will result in an exception:
547
550
 
548
551
  ```ruby
549
552
  schema = Schemacop::Schema3.new :array do
@@ -555,9 +558,10 @@ end
555
558
  # => Schemacop::Exceptions::InvalidSchemaError: You can only use "add" once to specify additional items.
556
559
  ```
557
560
 
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:
561
+ If you want to specify that your schema accept multiple additional types, use
562
+ the `one_of` type (see below for more infos). The correct way to specify that
563
+ you want to allow additional items, which may be an integer or a string is as
564
+ follows:
561
565
 
562
566
  ```ruby
563
567
  schema = Schemacop::Schema3.new :array do
@@ -586,13 +590,14 @@ It consists of key-value-pairs that can be validated using arbitrary nodes.
586
590
 
587
591
  * `additional_properties`
588
592
  This option specifies whether additional, unspecified properties are allowed
589
- (`true`) or not (`false`). By default, this is `true` if no properties are
590
- specified and `false` if you have specified at least one property.
593
+ (`true`) or not (`false`). By default, this is `false`, i.e. you need to
594
+ explicitly set it to `true` if you want to allow arbitrary additional properties,
595
+ or use the `add` DSL method (see below) to specify additional properties.
591
596
 
592
597
  * `property_names`
593
598
  This option allows to specify a regexp pattern (as string) which validates the
594
599
  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.
600
+ makes sense if `additional_properties` is enabled. See below for more information.
596
601
 
597
602
  * `min_properties`
598
603
  Specifies the (inclusive) minimum number of properties a hash must contain.
@@ -645,8 +650,8 @@ schema.validate!('foo' => 42) # => {"foo"=>42}
645
650
 
646
651
  The result in both cases will be a
647
652
  [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:
653
+ which means that you can access the data in the hash with the symbol as well as
654
+ the string representation:
650
655
 
651
656
  ```ruby
652
657
  schema = Schemacop::Schema3.new :hash do
@@ -660,9 +665,9 @@ result[:foo] # => 42
660
665
  result['foo'] # 42
661
666
  ```
662
667
 
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:
668
+ Please note that if you specify the value twice in the data you want to
669
+ validate, once with the key being a symbol and once being a string, Schemacop
670
+ will raise an error:
666
671
 
667
672
  ```ruby
668
673
  schema = Schemacop::Schema3.new :hash do
@@ -672,6 +677,61 @@ end
672
677
  schema.validate!(foo: 42, 'foo' => 43) # => Schemacop::Exceptions::ValidationError: /: Has 1 ambiguous properties: [:foo].
673
678
  ```
674
679
 
680
+ In addition to the normal node options (which vary from type to type, check
681
+ the respective nodes for details), properties also support the `as` option.
682
+
683
+ With this, you can "rename" properties in the output:
684
+
685
+ ```ruby
686
+ schema = Schemacop::Schema3.new :hash do
687
+ int! :foo, as: :bar
688
+ end
689
+
690
+ schema.validate!({foo: 42}) # => {"bar"=>42}
691
+ ```
692
+
693
+ Please note that if you specify a node with the same property name multiple
694
+ times, or use the `as` option to rename a node to the same name of another
695
+ node, the last specified node will be used:
696
+
697
+ ```ruby
698
+ schema = Schemacop::Schema3.new :hash do
699
+ int? :foo
700
+ str? :foo
701
+ end
702
+
703
+ schema.validate!({foo: 1}) # => Schemacop::Exceptions::ValidationError: /foo: Invalid type, expected "string".
704
+ schema.validate!({foo: 'bar'}) # => {"foo"=>"bar"}
705
+ ```
706
+
707
+ As well as:
708
+
709
+ ```ruby
710
+ schema = Schemacop::Schema3.new :hash do
711
+ int? :foo
712
+ int? :bar, as: :foo
713
+ end
714
+
715
+ schema.validate!({foo: 1}) # => {"foo"=>1}
716
+ schema.validate!({foo: 1, bar: 2}) # => {"foo"=>2}
717
+ schema.validate!({bar: 2}) # => {"foo"=>2}
718
+ ```
719
+
720
+ If you want to specify a node which may be one of multiple types, use the `one_of`
721
+ node (see further down for more details):
722
+
723
+ ```ruby
724
+ schema = Schemacop::Schema3.new :hash do
725
+ one_of! :foo do
726
+ int
727
+ str
728
+ end
729
+ end
730
+
731
+ schema.validate!({foo: 1}) # => {"foo"=>1}
732
+ schema.validate!({foo: 'bar'}) # => {"foo"=>"bar"}
733
+ ```
734
+
675
735
  ##### Pattern properties
676
736
 
677
737
  In addition to symbols, property keys can also be a regular expression. Here,
@@ -798,10 +858,11 @@ schema.validate!({
798
858
  Type: `:object`\
799
859
  DSL: `obj`
800
860
 
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.
861
+ The object type represents a Ruby `Object`. Please note that the `as_json`
862
+ method on nodes of this type will just return `{}` (an empty JSON object), as
863
+ there isn't a useful way to represent a Ruby object without conflicting with the
864
+ `Hash` type. If you want to represent a JSON object, you should use the `Hash`
865
+ node.
805
866
 
806
867
  In the most basic form, this node will accept anything:
807
868
 
@@ -882,7 +943,7 @@ schema.validate!('foooo') # => Schemacop::Exceptions::ValidationError: /: Does n
882
943
  Type: `:any_of`\
883
944
  DSL: `any_of`
884
945
 
885
- Similar to the AllOf node, you can specify multiple schemas, for which the
946
+ Similar to the `all_of` node, you can specify multiple schemas, for which the
886
947
  given value needs to validate against at least one of the schemas.
887
948
 
888
949
  For example, your value needs to be either a string which is at least 2
@@ -899,7 +960,7 @@ schema.validate!('foo') # => "foo"
899
960
  schema.validate!(42) # => 42
900
961
  ```
901
962
 
902
- Please note that you need to specify at least one item in the AllOf node:
963
+ Please note that you need to specify at least one item in the `any_of` node:
903
964
 
904
965
  ```ruby
905
966
  Schemacop::Schema3.new :any_of # => Schemacop::Exceptions::InvalidSchemaError: Node "any_of" makes only sense with at least 1 item.
@@ -910,9 +971,9 @@ Schemacop::Schema3.new :any_of # => Schemacop::Exceptions::InvalidSchemaError: N
910
971
  Type: `:one_of`\
911
972
  DSL: `one_of`
912
973
 
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.
974
+ Similar to the `all_of` node, you can specify multiple schemas, for which the
975
+ given value needs to validate against exaclty one of the schemas. If the given
976
+ value validates against multiple schemas, the value is invalid.
916
977
 
917
978
  For example, if you want an integer which is either a multiple of 2 or 3,
918
979
  but not both (i.e. no multiple of 6), you could do it as follows:
@@ -951,7 +1012,7 @@ schema.validate!(6) # => Schemacop::Exceptions::ValidationError: /: Matches 2 de
951
1012
  Type: `:is_not`\
952
1013
  DSL: `is_not`
953
1014
 
954
- With the IsNot node, you can specify a schema which the given value must not
1015
+ With the `is_not` node, you can specify a schema which the given value must not
955
1016
  validate against, i.e. every value which matches the schema will make this node
956
1017
  invalid.
957
1018
 
@@ -969,7 +1030,7 @@ schema.validate!(3) # => Schemacop::Exceptions::ValidationError: /: Must not
969
1030
  schema.validate!('foo') # => "foo"
970
1031
  ```
971
1032
 
972
- Note that a IsNot node needs exactly one item:
1033
+ Note that a `is_not` node needs exactly one item:
973
1034
 
974
1035
  ```ruby
975
1036
  schema = Schemacop::Schema3.new :is_not # => Schemacop::Exceptions::InvalidSchemaError: Node "is_not" only allows exactly one item.
@@ -984,7 +1045,7 @@ Type: `reference`
984
1045
  **Definition**
985
1046
  DSL: `scm`
986
1047
 
987
- Finally, with the Reference node, you can define schemas and then later reference
1048
+ Finally, with the *Reference* node, you can define schemas and then later reference
988
1049
  them for usage, e.g. when you have a rather long schema which you need at multiple
989
1050
  places.
990
1051
 
@@ -1052,7 +1113,7 @@ schema.validate!([id: 42, first_name: 'Joe']) # => Schemacop::Except
1052
1113
 
1053
1114
  ## Context
1054
1115
 
1055
- Schemacop als features the concept of a `Context`. You can define schemas in a
1116
+ Schemacop also features the concept of a `Context`. You can define schemas in a
1056
1117
  context, and then reference them in other schemas in that context. This is e.g.
1057
1118
  useful if you need a part of the schema to be different depending on the
1058
1119
  business action.
@@ -1076,7 +1137,7 @@ context.schema :PersonInfo do
1076
1137
  end
1077
1138
 
1078
1139
  # 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
1140
+ # Note that at this point, we don't know what's in the :Person schema.
1080
1141
  schema = Schemacop::Schema3.new :reference, path: :Person
1081
1142
 
1082
1143
  # Validate the data in the context we defined before, where we need the first_name
@@ -1097,7 +1158,7 @@ other_context.schema :Person do
1097
1158
  end
1098
1159
 
1099
1160
  # 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
1161
+ # birth date of the person, instead only the nickname is allowed.
1101
1162
  Schemacop.with_context other_context do
1102
1163
  schema.validate!({first_name: 'Joe', last_name: 'Doe', info: { born_at: '1980-01-01'} })
1103
1164
  # => Schemacop::Exceptions::ValidationError: /nickname: Value must be given.
@@ -1115,11 +1176,11 @@ to use other data in the second context than in the first.
1115
1176
 
1116
1177
  ## External schemas
1117
1178
 
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.
1179
+ Finally, Schemacop features the possibility to specify schemas in seperate
1180
+ files. This is especially useful is you have schemas in your application which
1181
+ are used multiple times throughout the application.
1121
1182
 
1122
- For each schema, you define the schema in a single file, and after loading the
1183
+ For each schema, you define the schema in a separate file, and after loading the
1123
1184
  schemas, you can reference them in other schemas.
1124
1185
 
1125
1186
  The default load path is `'app/schemas'`, but this can be configured by setting
@@ -1133,14 +1194,17 @@ local schemas > context schemas > global schemas
1133
1194
 
1134
1195
  Where:
1135
1196
 
1136
- * local schemas: Defined by using the DSL method? `scm`
1197
+ * local schemas: Defined by using the DSL method `scm`
1137
1198
  * context schemas: Defined in the current context using `context.schema`
1138
1199
  * global schemas: Defined in a ruby file in the load path
1139
1200
 
1140
1201
  ### Rails applications
1141
1202
 
1142
- In Rails applications, your schemas are automatically eager-laoded from the load
1143
- path `'app/schemas'` when your application is started.
1203
+ In Rails applications, your schemas are automatically eager-loaded from the load
1204
+ path `'app/schemas'` when your application is started, unless your application
1205
+ is running in the `DEVELOPMENT` environment. In the `DEVELOPMENT` environment,
1206
+ schemas are loaded each time when they are used, and as such you can make changes
1207
+ to your external schemas without having to restart the server each time.
1144
1208
 
1145
1209
  After starting your application, you can reference them like normally defined
1146
1210
  reference schemas, with the name being relative to the load path.
@@ -1188,8 +1252,12 @@ schema.validate!({usr: {first_name: 'Joe', last_name: 'Doe', groups: [{name: 'fo
1188
1252
  ### Non-Rails applications
1189
1253
 
1190
1254
  Usage in non-Rails applications is the same as with usage in Rails applications,
1191
- however you need to eager load the schemas yourself:
1255
+ however you might need to eager load the schemas yourself:
1192
1256
 
1193
1257
  ```ruby
1194
1258
  Schemacop::V3::GlobalContext.eager_load!
1195
- ```
1259
+ ```
1260
+
1261
+ As mentioned before, you can also use the external schemas without having to
1262
+ eager-load them, but if you use the schemas multiple times, it might be better
1263
+ to eager-load them on start of your application / script.