Aschema-ruby/test/inputs/schema/top-level-enum.schema/default/TopLevel.rb+34 −0
| @@ -0,0 +1,34 @@ | ||
| 1 | +# This code may look unusually verbose for Ruby (and it is), but | |
| 2 | +# it performs some subtle and complex validation of JSON data. | |
| 3 | +# | |
| 4 | +# To parse this JSON, add 'dry-struct' and 'dry-types' gems, then do: | |
| 5 | +# | |
| 6 | +# top_level = TopLevel.from_json! "…" | |
| 7 | +# puts top_level == TopLevel::One | |
| 8 | +# | |
| 9 | +# If from_json! succeeds, the value returned matches the schema. | |
| 10 | + | |
| 11 | +require 'json' | |
| 12 | +require 'dry-types' | |
| 13 | +require 'dry-struct' | |
| 14 | + | |
| 15 | +module Types | |
| 16 | + include Dry.Types(default: :nominal) | |
| 17 | + | |
| 18 | + String = Strict::String | |
| 19 | + TopLevel = Strict::String.enum("one", "three", "two") | |
| 20 | +end | |
| 21 | + | |
| 22 | +module TopLevel | |
| 23 | + One = "one" | |
| 24 | + Three = "three" | |
| 25 | + Two = "two" | |
| 26 | + | |
| 27 | + def self.from_dynamic!(d) | |
| 28 | + Types::TopLevel[d] | |
| 29 | + end | |
| 30 | + | |
| 31 | + def self.from_json!(json) | |
| 32 | + from_dynamic!(JSON.parse(json)) | |
| 33 | + end | |
| 34 | +end |
No generated files match these filters.