Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Type::Tiny and anyOf, oneOf

by tobyink (Canon)
on Feb 02, 2021 at 23:46 UTC ( [id://11127840]=note: print w/replies, xml ) Need Help??


in reply to Type::Tiny and anyOf, oneOf

Not exactly sure what you want to accomplish but "any of" sounds like a union:

use Types::Standard qw( ArrayRef HashRef ScalarRef ); my ( @thing1, %thing2, $thing3 ); my $union = ArrayRef | HashRef; $union->check( \@thing1 ); # true $union->check( \%thing1 ); # true $union->check( \$thing1 ); # false ( ArrayRef | HashRef | ScalarRef )->check( \$thing1 ); # true

Replies are listed 'Best First'.
Re^2: Type::Tiny and anyOf, oneOf
by 1nickt (Canon) on Feb 03, 2021 at 00:04 UTC

    Thanks Toby. It probably can't be accomplished with a type. With a JSON Schema you can say about an object (Dict) that it must contain one of a list of (named) keys each with their own type. You could require either a phone number or an email address, for example. So something like:

    my $check = compile(Dict[ foo => Str, oneOf => [ baz => Int, qux => Str, ], ]);


    The way forward always starts with a minimal test.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11127840]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-20 03:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found