Returns a validator for a JSON property that requires it to have a value of the specified type. If optional=True, () is also allowed. The meaning of classinfo is the same as for isinstance().
(*classinfo, **kwargs)
| 104 | |
| 105 | |
| 106 | def of_type(*classinfo, **kwargs): |
| 107 | """Returns a validator for a JSON property that requires it to have a value of |
| 108 | the specified type. If optional=True, () is also allowed. |
| 109 | |
| 110 | The meaning of classinfo is the same as for isinstance(). |
| 111 | """ |
| 112 | |
| 113 | assert len(classinfo) |
| 114 | optional = kwargs.pop("optional", False) |
| 115 | assert not len(kwargs) |
| 116 | |
| 117 | def validate(value): |
| 118 | if (optional and value == ()) or isinstance(value, classinfo): |
| 119 | return value |
| 120 | else: |
| 121 | converted_value = _converter(value, classinfo) |
| 122 | if converted_value: |
| 123 | return converted_value |
| 124 | |
| 125 | if not optional and value == (): |
| 126 | raise ValueError("must be specified") |
| 127 | raise TypeError("must be " + " or ".join(t.__name__ for t in classinfo)) |
| 128 | |
| 129 | return validate |
| 130 | |
| 131 | |
| 132 | def default(default): |