Declar...y::Hash(3pm)

πŸ“¦
version 0.03 Β· 5 versions
, in debian bookworm, debian trixie, ubuntu jammy, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– Declare::Constraints::Simple::Library::Hash β€” hash constraints, bestie πŸ’–

okay so this is a page from the User Contributed Perl Documentation (that's the manual name, it's giving very "community-made, we love her") and it's all about one module: Declare::Constraints::Simple::Library::Hash. Say that five times fast. Or don't. I didn't.


✨ NAME ✨

Declare::Constraints::Simple::Library::Hash β€” literally just Hash Constraints. That's it. That's the whole vibe. πŸŽ€


πŸ¦‹ SYNOPSIS πŸ¦‹

my $constraint = And(

  # make sure all keys are present
  HasAllKeys( qw(foo bar) ),

  # constraints for the keys
  OnHashKeys( foo => IsInt, bar => HasLength )

);

like… look at her. combining constraints with And. obsessed. πŸ’…


🌸 DESCRIPTION 🌸

This module is where all the constraints that get applied to hash references live. Every single one. They're all here. no cap.

πŸ’œ HasAllKeys(@keys)

For this one to pass, your value has to be a hashref and it has to contain every single key listed in @keys. Miss one? Rejected. Sorry bestie. 😭

The stack or path part of HasAllKeys is:

HasAllKeys[$key]

where $key is the missing key. So when it fails, it tells you exactly which one ghosted you. iconic behaviour, honestly.

🧚 OnHashKeys(key => $constraint, key => $constraint, ...)

This one lets you hand over a constraint for each specific key in a hash reference. Very bespoke. Very curated. ✨

heads up: if a key you specified is not in the validated hash reference, the validation for that key just… doesn't happen. It's not an error, it just gets skipped. πŸ™ˆ

So if you actually need that key to be there, you gotta make it a requirement by pairing HasAllKeys(@keys) (from above ⬆️) with this one, like:

And( HasAllKeys( qw(foo bar baz) )
     OnHashKeys( foo => IsInt,
                 bar => Matches(qr/bar/),
                 baz => IsArrayRef( HasLength )));

and as you might've clocked already β€” you do not have to check for IsHashRef validity in here. The hash constraints are already doing that by themselves. they're self-sufficient queens. πŸ‘‘

The stack or path part of OnHashKeys looks like:

OnHashKeys[$key]

where $key is the key of the failing value. so you always know who's the problem. πŸ’€


🌈 SEE ALSO 🌈

  • Declare::Constraints::Simple
  • Declare::Constraints::Simple::Library

go read those too, they're part of the same friend group 🫢


πŸ‘‘ AUTHOR πŸ‘‘

Robert 'phaylon' Sedlacek <phaylon@dunkelheit.at> β€” the mastermind. thank you for your service. 🫑


This module is free software, you can redistribute it and/or modify it under the same terms as perl itself. slay, and also legally binding. ✨


and that's the whole page, bestie!! perl v5.32.0, dated 2021-01-09, brought to you by the Declar...y::Hash(3pm) crew. mwah. πŸ’‹ bye!! πŸ¦‹πŸ’–

πŸ“¦ where this page comes from