Skip to content

Instantly share code, notes, and snippets.

@j1n3l0
Last active October 19, 2021 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j1n3l0/72a286885ed2a1570ba4c1559c6f27b3 to your computer and use it in GitHub Desktop.
Save j1n3l0/72a286885ed2a1570ba4c1559c6f27b3 to your computer and use it in GitHub Desktop.
Experiments with building Type::Tiny types from JSON schemas
BEGIN {
package MyTypes;
use Type::Library -extends => ['Types::Standard'], -declare => qw< Transaction >;
use Type::Utils -all;
use JSON::Schema::AsType;
my $schema_uri
= 'https://gist.githubusercontent.com/j1n3l0/43676471d8121bfbce2835119201b3ed/'
. 'raw/a155fc1247488cac9104e944901c7274c0496434/demo-schema.json';
# NOTE
# Not sure why but without the surrounding `as( ... )` I get the following error:
# Can't locate object method "new" via package "1" (perhaps you forgot to load "1"?)
declare Transaction, as( JSON::Schema::AsType->new( uri => $schema_uri ) );
__PACKAGE__->meta->make_immutable();
}
# Tests
use Test2::V0;
use Test::TypeTiny;
use MyTypes -types;
should_fail {}, Transaction;
should_fail { client_id => 'test', source_id => 'test', foo => 'bar' }, Transaction;
should_pass { client_id => 'test', source_id => 'test' }, Transaction;
done_testing();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment