Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Make a Closure Based Validator Factory

Build a configurable validator factory that returns a function which validates values against configured rules.

Python practice15 minClosures & Function FactoriesIntermediateLast updated April 13, 2026

Problem statement

Implement make_validator(min_value=None, max_value=None, allowed_types=None, required=True) that returns a function validate(value). The returned validator should check the following: - If value is None: if required is True -> return (False, ['value required']), otherwise (True, []). - If allowed_types is provided (a tuple of types), and value is not an instance of any allowed type -> return (False, ['invalid type']). - If value is a number (int or float), min_value and max_value apply to the numeric value. - If value is a sequence (str, list, tuple), min_value and max_value apply to len(value). The validator returns a tuple (is_valid, errors) where errors is a list of string messages (possible messages: 'value required', 'invalid type', 'too small', 'too large'). Collect all applicable errors (except type mismatch should prevent range/length checks).

Task

Practice capturing configuration in closures and producing specialized functions (validators) that enforce type and range/length constraints.

Examples

Number validator

Input

validate = make_validator(min_value=0, max_value=10, allowed_types=(int, float)) validate(5)

Output

(True, [])

5 is numeric, within the configured range, and of an allowed type.

Input format

A single call to make_validator(...) returns a function which is then called with a single value. Tests will evaluate that call and compare the returned tuple.

Output format

The validator returns a tuple (bool, list) where bool indicates validity and list contains error messages.

Constraints

allowed_types, if provided, will be an iterable of types (you may convert it to a tuple). Do not use external libraries. Keep messages exactly as specified.

Samples

Sample 1

Input

make_validator(min_value=0, max_value=10)(5)

Output

(True, [])

5 is within range