{"id":26095420,"url":"https://github.com/nigelhorne/params-validate-strict","last_synced_at":"2026-05-29T12:02:09.504Z","repository":{"id":279633641,"uuid":"939446464","full_name":"nigelhorne/Params-Validate-Strict","owner":"nigelhorne","description":"Validates a set of parameters against a schema","archived":false,"fork":false,"pushed_at":"2026-05-25T01:52:42.000Z","size":2718,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-05-25T03:25:21.744Z","etag":null,"topics":["cpan","cpan-module","perl","perl5","perl5-module"],"latest_commit_sha":null,"homepage":"https://metacpan.org/pod/Params::Validate::Strict","language":"Perl","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nigelhorne.png","metadata":{"files":{"readme":"README.md","changelog":"Changes","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":"nigelhore","patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"lfx_crowdfunding":null,"polar":null,"buy_me_a_coffee":null,"thanks_dev":null,"custom":["https://www.paypal.com/paypalme/bandsman"]}},"created_at":"2025-02-26T14:56:40.000Z","updated_at":"2026-05-25T01:52:42.000Z","dependencies_parsed_at":"2025-07-29T14:22:35.084Z","dependency_job_id":"3094287a-2a10-4423-b21f-b33c0e4c788e","html_url":"https://github.com/nigelhorne/Params-Validate-Strict","commit_stats":null,"previous_names":["nigelhorne/params-validate-strict"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/nigelhorne/Params-Validate-Strict","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nigelhorne%2FParams-Validate-Strict","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nigelhorne%2FParams-Validate-Strict/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nigelhorne%2FParams-Validate-Strict/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nigelhorne%2FParams-Validate-Strict/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nigelhorne","download_url":"https://codeload.github.com/nigelhorne/Params-Validate-Strict/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nigelhorne%2FParams-Validate-Strict/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33650712,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-05-29T02:00:06.066Z","response_time":107,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpan","cpan-module","perl","perl5","perl5-module"],"created_at":"2025-03-09T13:16:20.149Z","updated_at":"2026-05-29T12:02:09.496Z","avatar_url":"https://github.com/nigelhorne.png","language":"Perl","funding_links":["https://github.com/sponsors/nigelhore","https://www.paypal.com/paypalme/bandsman"],"categories":[],"sub_categories":[],"readme":"# NAME\n\nParams::Validate::Strict - Validates a set of parameters against a schema\n\n# VERSION\n\nVersion 0.33\n\n# SYNOPSIS\n\n    my $schema = {\n        username =\u003e { type =\u003e 'string', min =\u003e 3, max =\u003e 50 },\n        age =\u003e { type =\u003e 'integer', min =\u003e 0, max =\u003e 150 },\n    };\n\n    my $input = {\n         username =\u003e 'john_doe',\n         age =\u003e '30',   # Will be coerced to integer\n    };\n\n    my $validated_input = validate_strict(schema =\u003e $schema, input =\u003e $input);\n\n    if(defined($validated_input)) {\n        print \"Example 1: Validation successful!\\n\";\n        print 'Username: ', $validated_input-\u003e{username}, \"\\n\";\n        print 'Age: ', $validated_input-\u003e{age}, \"\\n\";   # It's an integer now\n    } else {\n        print \"Example 1: Validation failed: $@\\n\";\n    }\n\nUpon first reading this may seem overly complex and full of scope creep in a sledgehammer to crack a nut sort of way,\nhowever two use cases make use of the extensive logic that comes with this code\nand I have a couple of other reasons for writing it.\n\n- Black Box Testing\n\n    The schema can be plumbed into [App::Test::Generator](https://metacpan.org/pod/App%3A%3ATest%3A%3AGenerator) to automatically create a set of black-box test cases.\n\n- WAF\n\n    The schema can be plumbed into a WAF to protect from random user input.\n\n- Improved API Documentation\n\n    Even if you don't use this module,\n    the specification syntax can help with documentation.\n\n- I like it\n\n    I find it fun to write this,\n    even if nobody else finds it useful,\n    though I hope you will.\n\n# METHODS\n\n## validate\\_strict\n\nValidates a set of parameters against a schema.\n\nThis function takes two mandatory arguments:\n\n- `schema` || `members`\n\n    A reference to a hash that defines the validation rules for each parameter.\n    The keys of the hash are the parameter names, and the values are either a string representing the parameter type or a reference to a hash containing more detailed rules.\n\n    As an alternative the schema may be supplied as an **arrayref of parameter\n    hashrefs**, where every element describes one parameter and carries a mandatory\n    `name` key:\n\n        $schema = [\n          { name =\u003e 'username', type =\u003e 'string', min =\u003e 3, max =\u003e 50 },\n          { name =\u003e 'age',      type =\u003e 'integer', min =\u003e 0, max =\u003e 150 },\n          { name =\u003e 'role',     type =\u003e 'string', optional =\u003e 1, default =\u003e 'user' },\n        ];\n\n    The arrayref form is normalised to the standard hashref form before any further\n    processing.  It is particularly useful when declaration order matters (e.g.\n    for positional or mixed calling conventions used by some CPAN modules).  The\n    `name` key is consumed during normalisation and does not appear as a\n    validation rule.\n\n    For some sort of compatibility with [Data::Processor](https://metacpan.org/pod/Data%3A%3AProcessor),\n    it is possible to wrap the schema within a hash like this:\n\n        $schema = {\n          description =\u003e 'Describe what this schema does',\n          error_msg =\u003e 'An error message',\n          schema =\u003e {\n            # ... schema goes here\n          }\n        }\n\n- `args` || `input`\n\n    A reference to a hash containing the parameters to be validated.\n    The keys of the hash are the parameter names, and the values are the parameter values.\n\nIt takes optional arguments:\n\n- `description`\n\n    What the schema does,\n    used in error messages.\n\n- `error_msg`\n\n    Overrides the default message when something doesn't validate.\n\n- `unknown_parameter_handler`\n\n    This parameter describes what to do when a parameter is given that is not in the schema of valid parameters.\n    It must be one of `die`, `warn`, or `ignore`.\n\n    It defaults to `die` unless `carp_on_warn` is given, in which case it defaults to `warn`.\n\n- `logger`\n\n    A logging object that understands messages such as `error` and `warn`.\n\n- `custom_types`\n\n    A reference to a hash that defines reusable custom types.\n    Custom types allow you to define validation rules once and reuse them throughout your schema,\n    making your validation logic more maintainable and readable.\n\n    Each custom type is defined as a hash reference containing the same validation rules available for regular parameters\n    (`type`, `min`, `max`, `matches`, `memberof`, `values`, `enum`, `notmemberof`, `callback`, etc.).\n\n        my $custom_types = {\n          email =\u003e {\n            type =\u003e 'string',\n            matches =\u003e qr/^[\\w\\.\\-]+@[\\w\\.\\-]+\\.\\w+$/,\n            error_msg =\u003e 'Invalid email address format'\n          }, phone =\u003e {\n            type =\u003e 'string',\n            matches =\u003e qr/^\\+?[1-9]\\d{1,14}$/,\n            min =\u003e 10,\n            max =\u003e 15\n          }, percentage =\u003e {\n            type =\u003e 'number',\n            min =\u003e 0,\n            max =\u003e 100\n          }, status =\u003e {\n            type =\u003e 'string',\n            memberof =\u003e ['draft', 'published', 'archived']\n          }\n        };\n\n        my $schema = {\n          user_email =\u003e { type =\u003e 'email' },\n          contact_number =\u003e { type =\u003e 'phone', optional =\u003e 1 },\n          completion =\u003e { type =\u003e 'percentage' },\n          post_status =\u003e { type =\u003e 'status' }\n        };\n\n        my $validated = validate_strict(\n          schema =\u003e $schema,\n          input =\u003e $input,\n          custom_types =\u003e $custom_types\n        );\n\n    Custom types can be extended or overridden in the schema by specifying additional constraints:\n\n        my $schema = {\n          admin_username =\u003e {\n            type =\u003e 'username',  # Uses custom type definition\n            min =\u003e 5,            # Overrides custom type's min value\n            max =\u003e 15            # Overrides custom type's max value\n          }\n        };\n\n    Custom types work seamlessly with nested schema, optional parameters, and all other validation features.\n\nThe schema can define the following rules for each parameter:\n\n- `type`\n\n    The data type of the parameter.\n    Valid types are `string`, `integer`, `number`, `float` `boolean`, `scalar`, `scalarref`, `stringref`, `hashref`, `arrayref`, `object` and `coderef`.\n    `scalar` accepts any plain scalar value (string, number, boolean, etc.) but rejects references (arrayrefs, hashrefs, coderefs, objects).\n    `scalarref` accepts a reference to a scalar value (e.g. `\\$var`) but rejects plain scalars, arrayrefs, hashrefs, coderefs, and objects.\n    `stringref` accepts a reference to a scalar that contains a plain string (e.g. `\\$str`) and rejects plain scalars, references-to-references, arrayrefs, hashrefs, coderefs, and objects.\n    The `min`/`max` constraints apply to the **length** (in characters) of the referenced string.\n    All other string rules (`matches`, `nomatch`, `memberof`, etc.) operate on the dereferenced string value.\n    The validated return value is the dereferenced plain string.\n\n    A type can be an arrayref when a parameter could have different types (e.g. a string or an object).\n\n        $schema = {\n          username =\u003e [\n            { type =\u003e 'string', min =\u003e 3, max =\u003e 50 },        # Name\n            { type =\u003e 'integer', 'min' =\u003e 1 },        # UID that isn't root\n          ]\n        };\n\n    As a shorthand, `type` itself may be an arrayref of type name strings (a _union type_)\n    when all other constraints are shared between the alternatives:\n\n        $schema = {\n          data =\u003e { type =\u003e ['string', 'arrayref'] },\n          id   =\u003e { type =\u003e ['string', 'integer'], optional =\u003e 1 },\n        };\n\n    This is equivalent to the full array-of-rules form but more concise.\n    Every other key in the rule hash (`optional`, `min`, `max`, `matches`, etc.)\n    is inherited by each candidate type and validated independently against it.\n    Type names are tried left-to-right; the first match wins and its coercion\n    (e.g. numeric types) is propagated back to the caller.\n    If the value fails all candidate types, validation croaks with a message\n    listing the union members.\n\n- `can`\n\n    The parameter must be an object that understands the method `can`.\n    `can` can be a simple scalar string of a method name,\n    or an arrayref of a list of method names, all of which must be supported by the object.\n\n        $schema = {\n          gedcom =\u003e { type =\u003e object, can =\u003e 'get_individual' }\n        }\n\n- `isa`\n\n    The parameter must be an object of type `isa`.\n\n- `memberof`\n\n    The parameter must be a member of the given arrayref.\n\n        status =\u003e {\n          type =\u003e 'string',\n          memberof =\u003e ['draft', 'published', 'archived']\n        }\n\n        priority =\u003e {\n          type =\u003e 'integer',\n          memberof =\u003e [1, 2, 3, 4, 5]\n        }\n\n    For string types, the comparison is case-sensitive by default. Use the `case_sensitive`\n    flag to control this behavior:\n\n        # Case-sensitive (default) - must be exact match\n        code =\u003e {\n          type =\u003e 'string',\n          memberof =\u003e ['ABC', 'DEF', 'GHI']\n          # 'abc' will fail\n        }\n\n        # Case-insensitive - any case accepted\n        code =\u003e {\n          type =\u003e 'string',\n          memberof =\u003e ['ABC', 'DEF', 'GHI'],\n          case_sensitive =\u003e 0\n          # 'abc', 'Abc', 'ABC' all pass, original case preserved\n        }\n\n    For numeric types (`integer`, `number`, `float`), the comparison uses numeric\n    equality (`==` operator):\n\n        rating =\u003e {\n          type =\u003e 'number',\n          memberof =\u003e [0.5, 1.0, 1.5, 2.0]\n        }\n\n    Note that `memberof` cannot be combined with `min` or `max` constraints as they\n    serve conflicting purposes - `memberof` defines an explicit whitelist while `min`/`max`\n    define ranges.\n\n- `enum`\n\n    Same as `memberof`.\n\n- `values`\n\n    Same as `memberof`.\n\n- `notmemberof`\n\n    The parameter must not be a member of the given arrayref (blacklist).\n    This is the inverse of `memberof`.\n\n        username =\u003e {\n          type =\u003e 'string',\n          notmemberof =\u003e ['admin', 'root', 'system', 'administrator']\n        }\n\n        port =\u003e {\n          type =\u003e 'integer',\n          notmemberof =\u003e [22, 23, 25, 80, 443]  # Reserved ports\n        }\n\n    Like `memberof`, string comparisons are case-sensitive by default but can be controlled\n    with the `case_sensitive` flag:\n\n        # Case-sensitive (default)\n        username =\u003e {\n          type =\u003e 'string',\n          notmemberof =\u003e ['Admin', 'Root']\n          # 'admin' would pass, 'Admin' would fail\n        }\n\n        # Case-insensitive\n        username =\u003e {\n          type =\u003e 'string',\n          notmemberof =\u003e ['Admin', 'Root'],\n          case_sensitive =\u003e 0\n          # 'admin', 'ADMIN', 'Admin' all fail\n        }\n\n    The blacklist is checked after any `transform` rules are applied, allowing you to\n    normalize input before checking:\n\n        username =\u003e {\n          type =\u003e 'string',\n          transform =\u003e sub { lc($_[0]) },  # Normalize to lowercase\n          notmemberof =\u003e ['admin', 'root', 'system']\n        }\n\n    `notmemberof` can be combined with other validation rules:\n\n        username =\u003e {\n          type =\u003e 'string',\n          notmemberof =\u003e ['admin', 'root', 'system'],\n          min =\u003e 3,\n          max =\u003e 20,\n          matches =\u003e qr/^[a-z0-9_]+$/\n        }\n\n- `case_sensitive`\n\n    A boolean value indicating whether string comparisons should be case-sensitive.\n    This flag affects the `memberof` and `notmemberof` validation rules.\n    The default value is `1` (case-sensitive).\n\n    When set to `0`, string comparisons are performed case-insensitively, allowing values\n    with different casing to match. The original case of the input value is preserved in\n    the validated output.\n\n        # Case-sensitive (default)\n        status =\u003e {\n          type =\u003e 'string',\n          memberof =\u003e ['Draft', 'Published', 'Archived'] # Input 'draft' will fail - must match exact case\n        }\n\n        # Case-insensitive\n        status =\u003e {\n          type =\u003e 'string',\n          memberof =\u003e ['Draft', 'Published', 'Archived'],\n          case_sensitive =\u003e 0 # Input 'draft', 'DRAFT', or 'DrAfT' will all pass\n        }\n\n        country_code =\u003e {\n          type =\u003e 'string',\n          memberof =\u003e ['US', 'UK', 'CA', 'FR'],\n          case_sensitive =\u003e 0  # Accept 'us', 'US', 'Us', etc.\n        }\n\n    This flag has no effect on numeric types (`integer`, `number`, `float`) as numbers\n    do not have case.\n\n- `min`/`minimum`\n\n    The minimum length (for strings in characters not bytes), value (for numbers) or number of keys (for hashrefs).\n\n- `max`\n\n    The maximum length (for strings in characters not bytes), value (for numbers) or number of keys (for hashrefs).\n\n- `matches`\n\n    A regular expression that the parameter value must match.\n    Checks all members of arrayrefs.\n\n- `nomatch`\n\n    A regular expression that the parameter value must not match.\n    Checks all members of arrayrefs.\n\n- `position`\n\n    For routines and methods that take positional args,\n    this integer value defines which position the argument will be in.\n    If this is set for all arguments,\n    `validate_strict` will return a reference to an array, rather than a reference to a hash.\n\n- `regex`\n\n    Synonym of matches\n\n- `description`\n\n    The description of the rule\n\n- `callback`\n\n    A code reference to a subroutine that performs custom validation logic.\n    The subroutine should accept the parameter value, the argument list and the schema as arguments and return true if the value is valid, false otherwise.\n\n    Use this to test more complex examples:\n\n        my $schema = {\n          even_number =\u003e {\n            type =\u003e 'integer',\n            callback =\u003e sub { $_[0] % 2 == 0 }\n        };\n\n        # Specify the arguments for a routine which has a second, optional argument, which, if given, must be less than or equal to the first\n        my $schema = {\n          first =\u003e {\n            type =\u003e 'integer'\n          }, second =\u003e {\n            type =\u003e 'integer',\n            optional =\u003e 1,\n            callback =\u003e sub {\n              my($value, $args) = @_;\n              # The 'defined' is needed in case 'second' is evaluated before 'first'\n              return (defined($args-\u003e{first}) \u0026\u0026 $value \u003c= $args-\u003e{first}) ? 1 : 0\n            }\n          }\n        };\n\n- `optional`\n\n    A boolean value indicating whether the parameter is optional.\n    If true, the parameter is not required.\n    If false or omitted, the parameter is required.\n\n    It can be a reference to a code snippet that will return true or false,\n    to determine if the parameter is optional or not.\n    The code will be called with two arguments: the value of the parameter and hash ref of all parameters:\n\n        my $schema = {\n          optional_field =\u003e {\n            type =\u003e 'string',\n            optional =\u003e sub {\n              my ($value, $all_params) = @_;\n              return $all_params-\u003e{make_optional} ? 1 : 0;\n            }\n          },\n          make_optional =\u003e { type =\u003e 'boolean' }\n        };\n\n        my $result = validate_strict(schema =\u003e $schema, input =\u003e { make_optional =\u003e 1 });\n\n    If the parameter is not optional, it can be passed an undef value, which will not flag an error.\n    This is by design.\n    So this will not say that the required parameter 's' is missing:\n\n        validate_strict(\n            schema =\u003e { s =\u003e { type =\u003e 'string' } },\n            input  =\u003e { s =\u003e undef },\n        );\n\n- `default`\n\n    Populate missing optional parameters with the specified value.\n    Note that this value is not validated.\n\n        username =\u003e {\n          type =\u003e 'string',\n          optional =\u003e 1,\n          default =\u003e 'guest'\n        }\n\n- `element_type`\n\n    Extends the validation to individual elements of arrays.\n\n        tags =\u003e {\n          type =\u003e 'arrayref',\n          element_type =\u003e 'number',   # Float means the same\n          min =\u003e 1,   # this is the length of the array, not the min value for each of the numbers. For that, add a C\u003cschema\u003e rule\n          max =\u003e 5\n        }\n\n- `error_msg`\n\n    The custom error message to be used in the event of a validation failure.\n\n        age =\u003e {\n          type =\u003e 'integer',\n          min =\u003e 18,\n          error_msg =\u003e 'You must be at least 18 years old'\n        }\n\n- `nullable`\n\n    Like optional,\n    though this cannot be a coderef,\n    only a flag.\n\n- `schema`\n\n    You can validate nested hashrefs and arrayrefs using the `schema` property:\n\n        my $schema = {\n            user =\u003e {       # 'user' is a hashref\n                type =\u003e 'hashref',\n                schema =\u003e { # Specify what the elements of the hash should be\n                    name =\u003e { type =\u003e 'string' },\n                    age =\u003e { type =\u003e 'integer', min =\u003e 0 },\n                    hobbies =\u003e {    # 'hobbies' is an array ref that this user has\n                        type =\u003e 'arrayref',\n                        schema =\u003e { type =\u003e 'string' }, # Validate each hobby\n                        min =\u003e 1 # At least one hobby\n                    }\n                }\n            }, metadata =\u003e {\n                type =\u003e 'hashref',\n                schema =\u003e {\n                    created =\u003e { type =\u003e 'string' },\n                    tags =\u003e {\n                        type =\u003e 'arrayref',\n                        schema =\u003e {\n                            type =\u003e 'string',\n                            matches =\u003e qr/^[a-z]+$/ # Or you can say matches =\u003e '^[a-z]+$'\n                        }\n                    }\n                }\n            }\n        };\n\n- `validate`\n\n    A snippet of code that validates the input.\n    It's passed the input arguments,\n    and return a string containing a reason for rejection,\n    or undef if it's allowed.\n\n        my $schema = {\n          user =\u003e {\n            type =\u003e 'string',\n            validate =\u003e sub {\n              if($_[0]-\u003e{'password'} eq 'bar') {\n                return undef;\n              }\n              return 'Invalid password, try again';\n            }\n          }, password =\u003e {\n             type =\u003e 'string'\n          }\n        };\n\n- `transform`\n\n    A code reference to a subroutine that transforms/sanitizes the parameter value before validation.\n    The subroutine should accept the parameter value as an argument and return the transformed value.\n    The transformation is applied before any validation rules are checked, allowing you to normalize\n    or clean data before it is validated.\n\n    Common use cases include trimming whitespace, normalizing case, formatting phone numbers,\n    sanitizing user input, and converting between data formats.\n\n        # Simple string transformations\n        username =\u003e {\n          type =\u003e 'string',\n          transform =\u003e sub { lc(trim($_[0])) },  # lowercase and trim\n          matches =\u003e qr/^[a-z0-9_]+$/\n        }\n\n        email =\u003e {\n          type =\u003e 'string',\n          transform =\u003e sub { lc(trim($_[0])) },  # normalize email\n          matches =\u003e qr/^[\\w\\.\\-]+@[\\w\\.\\-]+\\.\\w+$/\n        }\n\n        # Array transformations\n        tags =\u003e {\n          type =\u003e 'arrayref',\n          transform =\u003e sub { [map { lc($_) } @{$_[0]}] },  # lowercase all elements\n          element_type =\u003e 'string'\n        }\n\n        keywords =\u003e {\n          type =\u003e 'arrayref',\n          transform =\u003e sub {\n            my @arr = map { lc(trim($_)) } @{$_[0]};\n            my %seen;\n            return [grep { !$seen{$_}++ } @arr];  # remove duplicates\n          }\n        }\n\n        # Numeric transformations\n        quantity =\u003e {\n          type =\u003e 'integer',\n          transform =\u003e sub { int($_[0] + 0.5) },  # round to nearest integer\n          min =\u003e 1\n        }\n\n        # Sanitization\n        slug =\u003e {\n          type =\u003e 'string',\n          transform =\u003e sub {\n            my $str = lc(trim($_[0]));\n            $str =~ s/[^\\w\\s-]//g;  # remove special characters\n            $str =~ s/\\s+/-/g;      # replace spaces with hyphens\n            return $str;\n          },\n          matches =\u003e qr/^[a-z0-9-]+$/\n        }\n\n        phone =\u003e {\n          type =\u003e 'string',\n          transform =\u003e sub {\n            my $str = $_[0];\n            $str =~ s/\\D//g;  # remove all non-digits\n            return $str;\n          },\n          matches =\u003e qr/^\\d{10}$/\n        }\n\n    The `transform` function is applied to the value before any validation checks (`min`/`minimum`, `max`,\n    `matches`, `callback`, etc.), ensuring that validation rules are checked against the cleaned data.\n\n    Transformations work with all parameter types including nested structures:\n\n        user =\u003e {\n          type =\u003e 'hashref',\n          schema =\u003e {\n            name =\u003e {\n              type =\u003e 'string',\n              transform =\u003e sub { trim($_[0]) }\n            }, email =\u003e {\n              type =\u003e 'string',\n              transform =\u003e sub { lc(trim($_[0])) }\n            }\n          }\n        }\n\n    Transformations can also be defined in custom types for reusability:\n\n        my $custom_types = {\n          email =\u003e {\n            type =\u003e 'string',\n            transform =\u003e sub { lc(trim($_[0])) },\n            matches =\u003e qr/^[\\w\\.\\-]+@[\\w\\.\\-]+\\.\\w+$/\n          }\n        };\n\n    Note that the transformed value is what gets returned in the validated result and is what\n    subsequent validation rules will check against. If a transformation might fail, ensure it\n    handles edge cases appropriately.\n    It is the responsibility of the transformer to ensure that the type of the returned value is correct,\n    since that is what will be validated.\n\n    Many validators also allow a code ref to be passed so that you can create your own, conditional validation rule, e.g.:\n\n        $schema = {\n          age =\u003e {\n            type =\u003e 'integer',\n            min =\u003e sub {\n                my ($value, $all_params) = @_;\n                return $all_params-\u003e{country} eq 'US' ? 21 : 18;\n            }\n          }\n        }\n\n- `validator`\n\n    A synonym of `validate`, for compatibility with [Data::Processor](https://metacpan.org/pod/Data%3A%3AProcessor).\n\n- `cross_validation`\n\n    A reference to a hash that defines validation rules that depend on more than one parameter.\n    Cross-field validations are performed after all individual parameter validations have passed,\n    allowing you to enforce business logic that requires checking relationships between different fields.\n\n    Each cross-validation rule is a key-value pair where the key is a descriptive name for the validation\n    and the value is a code reference that accepts a hash reference of all validated parameters.\n    The subroutine should return `undef` if the validation passes, or an error message string if it fails.\n\n        my $schema = {\n          password =\u003e { type =\u003e 'string', min =\u003e 8 },\n          password_confirm =\u003e { type =\u003e 'string' }\n        };\n\n        my $cross_validation = {\n          passwords_match =\u003e sub {\n            my $params = shift;\n            return $params-\u003e{password} eq $params-\u003e{password_confirm}\n              ? undef : \"Passwords don't match\";\n          }\n        };\n\n        my $validated = validate_strict(\n          schema =\u003e $schema,\n          input =\u003e $input,\n          cross_validation =\u003e $cross_validation\n        );\n\n    Common use cases include password confirmation, date range validation, numeric comparisons,\n    and conditional requirements:\n\n        # Date range validation\n        my $cross_validation = {\n          date_range_valid =\u003e sub {\n            my $params = shift;\n            return $params-\u003e{start_date} le $params-\u003e{end_date}\n              ? undef : \"Start date must be before or equal to end date\";\n          }\n        };\n\n        # Price range validation\n        my $cross_validation = {\n          price_range_valid =\u003e sub {\n            my $params = shift;\n            return $params-\u003e{min_price} \u003c= $params-\u003e{max_price}\n              ? undef : \"Minimum price must be less than or equal to maximum price\";\n          }\n        };\n\n        # Conditional required field\n        my $cross_validation = {\n          address_required_for_delivery =\u003e sub {\n            my $params = shift;\n            if ($params-\u003e{shipping_method} eq 'delivery' \u0026\u0026 !$params-\u003e{delivery_address}) {\n              return \"Delivery address is required when shipping method is 'delivery'\";\n            }\n            return undef;\n          }\n        };\n\n    Multiple cross-validations can be defined in the same hash, and they are all checked in order.\n    If any cross-validation fails, the function will `croak` with the error message returned by the validation:\n\n        my $cross_validation = {\n          passwords_match =\u003e sub {\n            my $params = shift;\n            return $params-\u003e{password} eq $params-\u003e{password_confirm}\n              ? undef : \"Passwords don't match\";\n          },\n          emails_match =\u003e sub {\n            my $params = shift;\n            return $params-\u003e{email} eq $params-\u003e{email_confirm}\n              ? undef : \"Email addresses don't match\";\n          },\n          age_matches_birth_year =\u003e sub {\n            my $params = shift;\n            my $current_year = (localtime)[5] + 1900;\n            my $calculated_age = $current_year - $params-\u003e{birth_year};\n            return abs($calculated_age - $params-\u003e{age}) \u003c= 1\n              ? undef : \"Age doesn't match birth year\";\n          }\n        };\n\n    Cross-validations receive the parameters after individual validation and transformation have been applied,\n    so you can rely on the data being in the correct format and type:\n\n        my $schema = {\n          email =\u003e {\n            type =\u003e 'string',\n            transform =\u003e sub { lc($_[0]) }  # Lowercased before cross-validation\n          },\n          email_confirm =\u003e {\n            type =\u003e 'string',\n            transform =\u003e sub { lc($_[0]) }\n          }\n        };\n\n        my $cross_validation = {\n          emails_match =\u003e sub {\n            my $params = shift;\n            # Both emails are already lowercased at this point\n            return $params-\u003e{email} eq $params-\u003e{email_confirm}\n              ? undef : \"Email addresses don't match\";\n          }\n        };\n\n    Cross-validations can access nested structures and optional fields:\n\n        my $cross_validation = {\n          guardian_required_for_minors =\u003e sub {\n            my $params = shift;\n            if ($params-\u003e{user}{age} \u003c 18 \u0026\u0026 !$params-\u003e{guardian}) {\n              return \"Guardian information required for users under 18\";\n            }\n            return undef;\n          }\n        };\n\n- metadata\n\n    Fields starting with \u003c\\_\u003e are generated by [App::Test::Generator::SchemaExtractor](https://metacpan.org/pod/App%3A%3ATest%3A%3AGenerator%3A%3ASchemaExtractor),\n    and are currently ignored.\n\n- schematic\n\n    TODO: gives an idea of what the field will be, e.g. `filename`.\n\n    All cross-validations must pass for the overall validation to succeed.\n\n- `relationships`\n\n    A reference to an array that defines validation rules based on relationships between parameters.\n    Relationship validations are performed after all individual parameter validations have passed,\n    but before cross-validations.\n\n    Each relationship is a hash reference with a `type` field and additional fields depending on the type:\n\n    - **mutually\\_exclusive**\n\n        Parameters that cannot be specified together.\n\n            relationships =\u003e [\n              {\n                type =\u003e 'mutually_exclusive',\n                params =\u003e ['file', 'content'],\n                description =\u003e 'Cannot specify both file and content'\n              }\n            ]\n\n    - **required\\_group**\n\n        At least one parameter from the group must be specified.\n\n            relationships =\u003e [\n              {\n                type =\u003e 'required_group',\n                params =\u003e ['id', 'name'],\n                logic =\u003e 'or',\n                description =\u003e 'Must specify either id or name'\n              }\n            ]\n\n    - **conditional\\_requirement**\n\n        If one parameter is specified, another becomes required.\n\n            relationships =\u003e [\n              {\n                type =\u003e 'conditional_requirement',\n                if =\u003e 'async',\n                then_required =\u003e 'callback',\n                description =\u003e 'When async is specified, callback is required'\n              }\n            ]\n\n    - **dependency**\n\n        One parameter requires another to be present.\n\n            relationships =\u003e [\n              {\n                type =\u003e 'dependency',\n                param =\u003e 'port',\n                requires =\u003e 'host',\n                description =\u003e 'port requires host to be specified'\n              }\n            ]\n\n    - **value\\_constraint**\n\n        Specific value requirements between parameters.\n\n            relationships =\u003e [\n              {\n                type =\u003e 'value_constraint',\n                if =\u003e 'ssl',\n                then =\u003e 'port',\n                operator =\u003e '==',\n                value =\u003e 443,\n                description =\u003e 'When ssl is specified, port must equal 443'\n              }\n            ]\n\n    - **value\\_conditional**\n\n        Parameter required when another has a specific value.\n\n            relationships =\u003e [\n              {\n                type =\u003e 'value_conditional',\n                if =\u003e 'mode',\n                equals =\u003e 'secure',\n                then_required =\u003e 'key',\n                description =\u003e \"When mode equals 'secure', key is required\"\n              }\n            ]\n\n    If a parameter is optional and its value is `undef`,\n    validation will be skipped for that parameter.\n\n    If the validation fails, the function will `croak` with an error message describing the validation failure.\n\n    If the validation is successful, the function will return a reference to a new hash containing the validated and (where applicable) coerced parameters.  Integer and number parameters will be coerced to their respective types.\n\n    The `description` field is optional but recommended for clearer error messages.\n\n## Example Usage\n\n    my $schema = {\n      host =\u003e { type =\u003e 'string' },\n      port =\u003e { type =\u003e 'integer' },\n      ssl =\u003e { type =\u003e 'boolean' },\n      file =\u003e { type =\u003e 'string', optional =\u003e 1 },\n      content =\u003e { type =\u003e 'string', optional =\u003e 1 }\n    };\n\n    my $relationships = [\n      {\n        type =\u003e 'mutually_exclusive',\n        params =\u003e ['file', 'content']\n      },\n      {\n        type =\u003e 'required_group',\n        params =\u003e ['host', 'file']\n      },\n      {\n        type =\u003e 'dependency',\n        param =\u003e 'port',\n        requires =\u003e 'host'\n      },\n      {\n        type =\u003e 'value_constraint',\n        if =\u003e 'ssl',\n        then =\u003e 'port',\n        operator =\u003e '==',\n        value =\u003e 443\n      }\n    ];\n\n    my $validated = validate_strict(\n      schema =\u003e $schema,\n      input =\u003e $input,\n      relationships =\u003e $relationships\n    );\n\n# MIGRATION FROM LEGACY VALIDATORS\n\n## From [Params::Validate](https://metacpan.org/pod/Params%3A%3AValidate)\n\n    # Old style\n    validate(@_, {\n        name =\u003e { type =\u003e SCALAR },\n        age =\u003e { type =\u003e SCALAR, regex =\u003e qr/^\\d+$/ }\n    });\n\n    # New style\n    validate_strict(\n        schema =\u003e {     # or \"members\"\n            name =\u003e 'string',\n            age =\u003e { type =\u003e 'integer', min =\u003e 0 }\n        },\n        args =\u003e { @_ }\n    );\n\n## From [Type::Params](https://metacpan.org/pod/Type%3A%3AParams)\n\n    # Old style\n    my ($name, $age) = validate_positional \\@_, Str, Int;\n\n    # New style - requires converting to named parameters first\n    my %args = (name =\u003e $_[0], age =\u003e $_[1]);\n    my $validated = validate_strict(\n        schema =\u003e { name =\u003e 'string', age =\u003e 'integer' },\n        args =\u003e \\%args\n    );\n\n# AUTHOR\n\nNigel Horne, `\u003cnjh at nigelhorne.com\u003e`\n\n# FORMAL SPECIFICATION\n\n    [PARAM_NAME, VALUE, TYPE_NAME, CONSTRAINT_VALUE]\n\n    ValidationRule ::= SimpleType | ComplexRule | UnionType\n\n    SimpleType ::= string | integer | number | scalar | scalarref | stringref | arrayref | hashref | coderef | object\n\n    UnionType ::= seq SimpleType    -- at least two members; written as type =\u003e ['a', 'b']\n\n    ComplexRule == [\n        type: SimpleType | UnionType;\n        min: ℕ₁;\n        max: ℕ₁;\n        optional: 𝔹;\n        matches: REGEX;\n        regex: REGEX;\n        nomatch: REGEX;\n        memberof: seq VALUE;\n        enum: seq VALUE;\n        values: seq VALUE;\n        notmemberof: seq VALUE;\n        callback: FUNCTION;\n        isa: TYPE_NAME;\n        can: METHOD_NAME\n    ]\n\n    Schema == PARAM_NAME ⇸ ValidationRule\n\n    Arguments == PARAM_NAME ⇸ VALUE\n\n    ValidatedResult == PARAM_NAME ⇸ VALUE\n\n    ∀ rule: ComplexRule •\n      rule.min ≤ rule.max ∧\n      ¬((rule.memberof ∨ rule.enum ∨ rule.values) ∧ rule.min) ∧\n      ¬((rule.memberof ∨ rule.enum ∨ rule.values) ∧ rule.max) ∧\n      ¬(rule.notmemberof ∧ rule.min) ∧\n      ¬(rule.notmemberof ∧ rule.max)\n\n    ∀ schema: Schema; args: Arguments •\n      dom(validate_strict(schema, args)) ⊆ dom(schema) ∪ dom(args)\n\n    validate_strict: Schema × Arguments → ValidatedResult\n\n    ∀ schema: Schema; args: Arguments •\n      let result == validate_strict(schema, args) •\n        (∀ name: dom(schema) ∩ dom(args) •\n          name ∈ dom(result) ⇒\n          type_matches(result(name), schema(name))) ∧\n        (∀ name: dom(schema) •\n          ¬optional(schema(name)) ⇒ name ∈ dom(args))\n\n    type_matches: VALUE × ValidationRule → 𝔹\n\n# EXAMPLE\n\n    use Params::Get;\n    use Params::Validate::Strict;\n\n    sub where_am_i\n    {\n        my $params = Params::Validate::Strict::validate_strict({\n            args =\u003e Params::Get::get_params(undef, \\@_),\n            description =\u003e 'Print a string of latitude and longitude',\n            error_msg =\u003e 'Latitude is a number between +/- 90, longitude is a number between +/- 180',\n            members =\u003e {\n                'latitude' =\u003e {\n                    type =\u003e 'number',\n                    min =\u003e -90,\n                    max =\u003e 90\n                }, 'longitude' =\u003e {\n                    type =\u003e 'number',\n                    min =\u003e -180,\n                    max =\u003e 180\n                }\n            }\n        });\n\n        print 'You are at ', $params-\u003e{'latitude'}, ', ', $params-\u003e{'longitude'}, \"\\n\";\n    }\n\n    where_am_i({ latitude =\u003e 3.14, longitude =\u003e -155 });\n\n# BUGS\n\n# SEE ALSO\n\n- [Test Dashboard](https://nigelhorne.github.io/Params-Validate-Strict/coverage/)\n- [Data::Processor](https://metacpan.org/pod/Data%3A%3AProcessor)\n- [Params::Get](https://metacpan.org/pod/Params%3A%3AGet)\n- [Params::Smart](https://metacpan.org/pod/Params%3A%3ASmart)\n- [Params::Validate](https://metacpan.org/pod/Params%3A%3AValidate)\n- [Return::Set](https://metacpan.org/pod/Return%3A%3ASet)\n- [App::Test::Generator](https://metacpan.org/pod/App%3A%3ATest%3A%3AGenerator)\n\n# SUPPORT\n\nThis module is provided as-is without any warranty.\n\nPlease report any bugs or feature requests to `bug-params-validate-strict at rt.cpan.org`,\nor through the web interface at\n[http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Params-Validate-Strict](http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Params-Validate-Strict).\nI will be notified, and then you'll\nautomatically be notified of progress on your bug as I make changes.\n\nYou can find documentation for this module with the perldoc command.\n\n    perldoc Params::Validate::Strict\n\nYou can also look for information at:\n\n- MetaCPAN\n\n    [https://metacpan.org/dist/Params-Validate-Strict](https://metacpan.org/dist/Params-Validate-Strict)\n\n- RT: CPAN's request tracker\n\n    [https://rt.cpan.org/NoAuth/Bugs.html?Dist=Params-Validate-Strict](https://rt.cpan.org/NoAuth/Bugs.html?Dist=Params-Validate-Strict)\n\n- CPAN Testers' Matrix\n\n    [http://matrix.cpantesters.org/?dist=Params-Validate-Strict](http://matrix.cpantesters.org/?dist=Params-Validate-Strict)\n\n- CPAN Testers Dependencies\n\n    [http://deps.cpantesters.org/?module=Params::Validate::Strict](http://deps.cpantesters.org/?module=Params::Validate::Strict)\n\n# LICENSE AND COPYRIGHT\n\nCopyright 2025-2026 Nigel Horne.\n\nThis program is released under the following licence: GPL2.\nIf you use it,\nplease let me know.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnigelhorne%2Fparams-validate-strict","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnigelhorne%2Fparams-validate-strict","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnigelhorne%2Fparams-validate-strict/lists"}