nti.schema.field¶
Schema fields.
These fields produce better validation errors than the standard
zope.schema
fields do. All the standard fields are also aliased
to be imported from this module.
-
class
Bool
(*args, **kw)[source]¶ Bases:
zope.schema._bootstrapfields.Field
A field representing a Bool.
Changed in version 4.8.0: Implement
zope.schema.interfaces.IFromBytes
-
fromBytes
(value)[source]¶ >>> from zope.schema._bootstrapfields import Bool >>> from zope.schema.interfaces import IFromBytes >>> b = Bool() >>> IFromBytes.providedBy(b) True >>> b.fromBytes(b'True') True >>> b.fromBytes(b'') False >>> b.fromBytes(b'true') True >>> b.fromBytes(b'false') or b.fromBytes(b'False') False >>> b.fromBytes(u'\u2603'.encode('utf-8')) False
-
fromUnicode
(value)[source]¶ >>> from zope.schema._bootstrapfields import Bool >>> from zope.schema.interfaces import IFromUnicode >>> b = Bool() >>> IFromUnicode.providedBy(b) True >>> b.fromUnicode('True') True >>> b.fromUnicode('') False >>> b.fromUnicode('true') True >>> b.fromUnicode('false') or b.fromUnicode('False') False >>> b.fromUnicode(u'\u2603') False
-
-
class
Choice
(values=None, vocabulary=None, source=None, **kw)[source]¶ Bases:
zope.schema._bootstrapfields.Field
Choice fields can have a value found in a constant or dynamic set of values given by the field definition.
-
class
Complex
(min=None, max=None, default=None, **kw)[source]¶ Bases:
zope.schema._bootstrapfields.Number
A field representing a
numbers.Complex
and implementingzope.schema.interfaces.IComplex
.The
fromUnicode()
method is like that forNumber
, but doesn’t allow Decimals:>>> from zope.schema._bootstrapfields import Complex >>> f = Complex() >>> f.fromUnicode(u"1") 1 >>> f.fromUnicode(u"125.6") 125.6 >>> f.fromUnicode(u"1+0j") (1+0j) >>> f.fromUnicode(u"1/2") Fraction(1, 2) >>> f.fromUnicode(str(2**31234) + '.' + str(2**256)) ... inf >>> f.fromUnicode(u"not a number") Traceback (most recent call last): ... InvalidNumberLiteral: Invalid literal for Decimal: 'not a number'
Similarly for
fromBytes()
:>>> from zope.schema._bootstrapfields import Complex >>> f = Complex() >>> f.fromBytes(b"1") 1 >>> f.fromBytes(b"125.6") 125.6 >>> f.fromBytes(b"1+0j") (1+0j) >>> f.fromBytes(b"1/2") Fraction(1, 2) >>> f.fromBytes((str(2**31234) + '.' + str(2**256)).encode('ascii')) ... # doctest: +ELLIPSIS inf >>> f.fromBytes(b"not a number") # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... InvalidNumberLiteral: Invalid literal for Decimal: 'not a number'
New in version 4.6.0.
-
class
Date
(min=None, max=None, default=None, **kw)[source]¶ Bases:
zope.schema._bootstrapfields.Orderable
,zope.schema._bootstrapfields.Field
Field containing a date.
-
class
Datetime
(*args, **kw)[source]¶ Bases:
zope.schema._bootstrapfields.Orderable
,zope.schema._bootstrapfields.Field
Field containing a datetime.
-
class
Decimal
(min=None, max=None, default=None, **kw)[source]¶ Bases:
zope.schema._bootstrapfields.Number
A field representing a native
decimal.Decimal
and implementingzope.schema.interfaces.IDecimal
.The
fromUnicode()
method only accepts values that can be parsed by theDecimal
constructor:>>> from zope.schema._field import Decimal >>> f = Decimal() >>> f.fromUnicode("1") Decimal('1') >>> f.fromUnicode("125.6") Decimal('125.6') >>> f.fromUnicode("1+0j") Traceback (most recent call last): ... InvalidDecimalLiteral: Invalid literal for Decimal(): 1+0j >>> f.fromUnicode("1/2") Traceback (most recent call last): ... InvalidDecimalLiteral: Invalid literal for Decimal(): 1/2 >>> f.fromUnicode(str(2**31234) + '.' + str(2**256)) ... Decimal('2349...936') >>> f.fromUnicode("not a number") Traceback (most recent call last): ... InvalidDecimalLiteral: could not convert string to float: not a number
Likewise for
fromBytes()
:>>> from zope.schema._field import Decimal >>> f = Decimal() >>> f.fromBytes(b"1") Decimal('1') >>> f.fromBytes(b"125.6") Decimal('125.6') >>> f.fromBytes(b"1+0j") Traceback (most recent call last): ... InvalidDecimalLiteral: Invalid literal for Decimal(): 1+0j >>> f.fromBytes(b"1/2") Traceback (most recent call last): ... InvalidDecimalLiteral: Invalid literal for Decimal(): 1/2 >>> f.fromBytes((str(2**31234) + '.' + str(2**256)).encode("ascii")) ... Decimal('2349...936') >>> f.fromBytes(b"not a number") Traceback (most recent call last): ... InvalidDecimalLiteral: could not convert string to float: not a number
-
class
DecodingValidTextLine
(*args, **kw)[source]¶ Bases:
nti.schema.field.ValidTextLine
A text type that will attempt to decode non-unicode data as UTF-8.
This primarily exists for legacy support (tests and persisted data).
-
class
Dict
(key_type=None, value_type=None, **kw)[source]¶ Bases:
zope.schema._field.MutableMapping
A field representing a Dict.
-
class
DictFromObject
(*args, **kwargs)[source]¶ Bases:
nti.schema.field._ValueTypeAddingDocMixin
,nti.schema.field._MapFromObjectMixin
,nti.schema.field.FieldValidationMixin
,zope.schema._field.Mapping
The
key_type
andvalue_type
must be supportingIFromObject
orIFromUnicode
orIFromBytes
Changed in version 1.4.0: Subclass
zope.schema.Mapping
instead ofzope.schema.Dict
, allowing for any mapping (such as BTrees), not just dicts. However, the validated value is still a dict.Changed in version 1.9.0: Implementations of
zope.schema.interfaces.ICollection
(likezope.schema.List
) andzope.schema.interfaces.IMapping
(likezope.schema.Dict
) will automatically be given afromObject
method when used as the value_type of this object if their value_type is anzope.schema.interfaces.IObject
(recursively).
-
class
FieldValidationMixin
[source]¶ Bases:
object
A field mixin that causes slightly better errors to be created.
-
class
Float
(min=None, max=None, default=None, **kw)[source]¶ Bases:
nti.schema.field.FieldValidationMixin
,zope.schema._field.Float
-
class
HTTPURL
(min_length=0, max_length=None, **kw)[source]¶ Bases:
nti.schema.field.ValidURI
A URI field that ensures and requires its value to be an absolute HTTP/S URL.
-
class
IndexedIterable
(value_type=<Not Given>, unique=<Not Given>, **kw)[source]¶ Bases:
nti.schema.field._ValueTypeAddingDocMixin
,nti.schema.field.FieldValidationMixin
,zope.schema._field.Sequence
An arbitrary (indexable) iterable, not necessarily a list or tuple; either of those would be acceptable at any time (however, so would a string, so be careful. Try ListOrTuple if that’s a problem).
The values may be homogeneous by setting the value_type.
Changed in version 1.4.0: Subclass
zope.schema.Sequence
instead ofzope.schema.List
, which adds checking that the value is indeed a sequence.
-
class
Int
(min=None, max=None, default=None, **kw)[source]¶ Bases:
nti.schema.field.FieldValidationMixin
,zope.schema._bootstrapfields.Int
-
class
Integral
(min=None, max=None, default=None, **kw)[source]¶ Bases:
zope.schema._bootstrapfields.Rational
A field representing a
numbers.Integral
and implementingzope.schema.interfaces.IIntegral
.The
fromUnicode()
method only allows integral values:>>> from zope.schema._bootstrapfields import Integral >>> f = Integral() >>> f.fromUnicode("125") 125 >>> f.fromUnicode("125.6") Traceback (most recent call last): ... InvalidIntLiteral: invalid literal for int(): 125.6
Similarly for
fromBytes()
:>>> from zope.schema._bootstrapfields import Integral >>> f = Integral() >>> f.fromBytes(b"125") 125 >>> f.fromBytes(b"125.6") #doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... InvalidIntLiteral: invalid literal for int(): 125.6
New in version 4.6.0.
-
class
Iterable
(title=u'', description=u'', __name__='', required=True, readonly=False, constraint=None, default=None, defaultFactory=None, missing_value=<Not Given>)[source]¶ Bases:
zope.schema._bootstrapfields.Container
-
class
List
(value_type=<Not Given>, unique=<Not Given>, **kw)[source]¶ Bases:
zope.schema._field.MutableSequence
A field representing a List.
-
class
ListOrTuple
(value_type=<Not Given>, unique=<Not Given>, **kw)[source]¶ Bases:
nti.schema.field.IndexedIterable
Restrict sequence values specifically to list and tuple.
-
class
ListOrTupleFromObject
(*args, **kwargs)[source]¶ Bases:
nti.schema.field._SequenceFromObjectMixin
,nti.schema.field.ListOrTuple
The
value_type
MUST be aVariant
, or more generally, something supportingIFromObject
,IFromUnicode
orIFromBytes
.Changed in version 1.9.0: Implementations of
zope.schema.interfaces.ICollection
(likezope.schema.List
) andzope.schema.interfaces.IMapping
(likezope.schema.Dict
) will automatically be given afromObject
method when used as the value_type of this object if their value_type is anzope.schema.interfaces.IObject
(recursively).
-
class
Mapping
(key_type=None, value_type=None, **kw)[source]¶ Bases:
zope.schema._bootstrapfields.MinMaxLen
,zope.schema._bootstrapfields.Iterable
A field representing a mapping.
New in version 4.6.0.
-
class
MutableMapping
(key_type=None, value_type=None, **kw)[source]¶ Bases:
zope.schema._field.Mapping
A field representing a mutable mapping.
New in version 4.6.0.
-
class
MutableSequence
(value_type=<Not Given>, unique=<Not Given>, **kw)[source]¶ Bases:
zope.schema._field.Sequence
A field representing a mutable sequence.
New in version 4.6.0.
-
class
Number
(min=None, max=None, default=None, **kw)[source]¶ Bases:
nti.schema.field.FieldValidationMixin
,zope.schema._field.Float
A field that parses like a float from a string, but accepts any number.
Deprecated since version 1.4.0: Use
zope.schema.Number
if you really want arbitrary numbers (including fractions and decimals). You probably wantzope.schema.Real
instead, though.
-
class
Object
(schema=<Not Given>, **kw)[source]¶ Bases:
nti.schema.field.FieldValidationMixin
,zope.schema._bootstrapfields.Object
Improved
zope.schema.Object
.
-
class
ObjectLen
(sch, min_length=0, max_length=None, **kwargs)[source]¶ Bases:
nti.schema.field.FieldValidationMixin
,zope.schema._bootstrapfields.MinMaxLen
,zope.schema._bootstrapfields.Object
Allows specifying a length for arbitrary object fields (though the objects themselves must support the
len
function.
-
class
Rational
(min=None, max=None, default=None, **kw)[source]¶ Bases:
zope.schema._bootstrapfields.Real
A field representing a
numbers.Rational
and implementingzope.schema.interfaces.IRational
.The
fromUnicode()
method is like that forReal
, but does not allow arbitrary floating point numbers:>>> from zope.schema._bootstrapfields import Rational >>> f = Rational() >>> f.fromUnicode("1") 1 >>> f.fromUnicode("1/2") Fraction(1, 2) >>> f.fromUnicode("125.6") Fraction(628, 5) >>> f.fromUnicode("1+0j") Traceback (most recent call last): ... InvalidNumberLiteral: Invalid literal for Fraction: '1+0j' >>> f.fromUnicode(str(2**31234) + '.' + str(2**256)) ... Fraction(777..., 330...) >>> f.fromUnicode("not a number") Traceback (most recent call last): ... InvalidNumberLiteral: Invalid literal for Decimal: 'not a number'
New in version 4.6.0.
-
class
Real
(min=None, max=None, default=None, **kw)[source]¶ Bases:
zope.schema._bootstrapfields.Complex
A field representing a
numbers.Real
and implementingzope.schema.interfaces.IReal
.The
fromUnicode()
method is like that forComplex
, but doesn’t allow Decimals or complex numbers:>>> from zope.schema._bootstrapfields import Real >>> f = Real() >>> f.fromUnicode("1") 1 >>> f.fromUnicode("125.6") 125.6 >>> f.fromUnicode("1+0j") Traceback (most recent call last): ... InvalidNumberLiteral: Invalid literal for Fraction: '1+0j' >>> f.fromUnicode("1/2") Fraction(1, 2) >>> f.fromUnicode(str(2**31234) + '.' + str(2**256)) ... inf >>> f.fromUnicode("not a number") Traceback (most recent call last): ... InvalidNumberLiteral: Invalid literal for Decimal: 'not a number'
New in version 4.6.0.
-
class
Set
(*args, **kwargs)[source]¶ Bases:
zope.schema._field._AbstractSet
A field representing a set.
-
class
Sequence
(value_type=<Not Given>, unique=<Not Given>, **kw)[source]¶ Bases:
zope.schema._field.Collection
A field representing an ordered sequence.
New in version 4.6.0.
-
class
StrippedValidTextLine
(*args, **kw)[source]¶ Bases:
nti.schema.field.DecodingValidTextLine
A text line that strips whitespace from incoming values.
New in version 1.13.0.
Changed in version 1.13.1: Handle single character values correctly.
-
fromUnicode
(value)[source]¶ >>> from zope.schema import Text >>> t = Text(constraint=lambda v: 'x' in v) >>> t.fromUnicode(b"foo x spam") # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... zope.schema._bootstrapinterfaces.WrongType: ('foo x spam', <type 'unicode'>, '') >>> result = t.fromUnicode(u"foo x spam") >>> isinstance(result, bytes) False >>> str(result) 'foo x spam' >>> t.fromUnicode(u"foo spam") # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... zope.schema._bootstrapinterfaces.ConstraintNotSatisfied: (u'foo spam', '')
-
-
class
Text
(*args, **kw)[source]¶ Bases:
zope.schema._bootstrapfields.MinMaxLen
,zope.schema._bootstrapfields.Field
A field containing text used for human discourse.
-
fromUnicode
(value)[source]¶ >>> from zope.schema import Text >>> t = Text(constraint=lambda v: 'x' in v) >>> t.fromUnicode(b"foo x spam") # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... zope.schema._bootstrapinterfaces.WrongType: ('foo x spam', <type 'unicode'>, '') >>> result = t.fromUnicode(u"foo x spam") >>> isinstance(result, bytes) False >>> str(result) 'foo x spam' >>> t.fromUnicode(u"foo spam") # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... zope.schema._bootstrapinterfaces.ConstraintNotSatisfied: (u'foo spam', '')
-
-
class
TextLine
(*args, **kw)[source]¶ Bases:
zope.schema._bootstrapfields.Text
A text field with no newlines.
-
class
Timedelta
(min=None, max=None, default=None, **kw)[source]¶ Bases:
zope.schema._bootstrapfields.Orderable
,zope.schema._bootstrapfields.Field
Field containing a timedelta.
-
class
Tuple
(value_type=<Not Given>, unique=<Not Given>, **kw)[source]¶ Bases:
zope.schema._field.Sequence
A field representing a Tuple.
-
class
TupleFromObject
(*args, **kwargs)[source]¶ Bases:
nti.schema.field._ValueTypeAddingDocMixin
,nti.schema.field._SequenceFromObjectMixin
,nti.schema.field.FieldValidationMixin
,zope.schema._field.Tuple
The
value_type
MUST be aVariant
, or more generally, something supportingIFromObject
,IFromUnicode
orIFromBytes
.When setting through this object, we will automatically convert lists and only lists to tuples (for convenience coming in through JSON).
Changed in version 1.9.0: Implementations of
zope.schema.interfaces.ICollection
(likezope.schema.List
) andzope.schema.interfaces.IMapping
(likezope.schema.Dict
) will automatically be given afromObject
method when used as the value_type of this object if their value_type is anzope.schema.interfaces.IObject
(recursively).
-
class
UniqueIterable
(*args, **kwargs)[source]¶ Bases:
nti.schema.field.ValidSet
An arbitrary iterable, not necessarily an actual
set
object, but one whose contents are unique. Use this when you can return aset
,frozenset
or empty tuple. These should be sequences that suport thein
operator.Changed in version 1.5.0: Implement
IFromObject
.Changed in version 1.9.0: Implementations of
zope.schema.interfaces.ICollection
(likezope.schema.List
) andzope.schema.interfaces.IMapping
(likezope.schema.Dict
) will automatically be given afromObject
method when used as the value_type of this object if their value_type is anzope.schema.interfaces.IObject
(recursively).
-
class
ValidBytes
(min_length=0, max_length=None, **kw)[source]¶ Bases:
nti.schema.field.FieldValidationMixin
,zope.schema._field.Bytes
-
class
ValidBytesLine
(min_length=0, max_length=None, **kw)[source]¶ Bases:
nti.schema.field.FieldValidationMixin
,zope.schema._field.BytesLine
-
class
ValidChoice
(values=None, vocabulary=None, source=None, **kw)[source]¶ Bases:
nti.schema.field.FieldValidationMixin
,zope.schema._field.Choice
-
class
ValidDatetime
(*args, **kw)[source]¶ Bases:
nti.schema.field.FieldValidationMixin
,zope.schema._field.Datetime
Unlike the standard datetime, this will check that the given object is an instance of IDatetime, and raise the same error as object does.
-
schema
= <InterfaceClass zope.interface.common.idatetime.IDateTime>¶
-
-
ValidRegEx
¶
-
class
ValidSet
(*args, **kwargs)[source]¶ Bases:
nti.schema.field._ValueTypeAddingDocMixin
,nti.schema.field._SequenceFromObjectMixin
,nti.schema.field.FieldValidationMixin
,zope.schema._field.Set
A set that is validated.
Changed in version 1.5.0: Implement
IFromObject
.Changed in version 1.9.0: Implementations of
zope.schema.interfaces.ICollection
(likezope.schema.List
) andzope.schema.interfaces.IMapping
(likezope.schema.Dict
) will automatically be given afromObject
method when used as the value_type of this object if their value_type is anzope.schema.interfaces.IObject
(recursively).
-
class
ValidText
(*args, **kw)[source]¶ Bases:
nti.schema.field.FieldValidationMixin
,zope.schema._bootstrapfields.Text
A text line that produces slightly better error messages. They will all have the ‘field’ property.
We also fire
IBeforeTextAssignedEvent
, which the normal mechanism does not.
-
class
ValidTextLine
(*args, **kw)[source]¶ Bases:
nti.schema.field.FieldValidationMixin
,zope.schema._bootstrapfields.TextLine
A text line that produces slightly better error messages. They will all have the ‘field’ property.
We also fire
IBeforeTextLineAssignedEvent
, which the normal mechanism does not.
-
class
ValidURI
(min_length=0, max_length=None, **kw)[source]¶ Bases:
nti.schema.field.FieldValidationMixin
,zope.schema._field.URI
-
class
Variant
(fields, variant_raise_when_schema_provided=False, **kwargs)[source]¶ Bases:
nti.schema.field.FieldValidationMixin
,zope.schema._bootstrapfields.Field
Similar to
zope.schema.Object
, but accepts one of many non-overlapping interfaces.Changed in version 1.9.0: Implementations of
zope.schema.interfaces.ICollection
(likezope.schema.List
) andzope.schema.interfaces.IMapping
(likezope.schema.Dict
) will automatically be given afromObject
method when they are used as a field of this object if their value_type is anzope.schema.interfaces.IObject
(recursively).Changed in version 1.16.0: If one of the fields of the variant is (recursively) a
IMapping
such as aDict
, both the key and value type must be specified. Previously, if they were leftNone
, a validation-timeAttributeError
would be raised. Now, constructing the variant will raise aRequiredMissing
.-
fromObject
(obj)[source]¶ Similar to
fromUnicode
, attempts to turn the given object into something acceptable and valid for this field. Raises aVariantValidationError
if this isn’t possible. Adaptation is attempted in the order in which fields were given to the constructor. Some fields cannot be used to adapt.Changed in version 1.8.0: Raise
VariantValidationError
instead of whatever last error we got.Changed in version 1.9.1: Respect
self.missing_value
and don’t raise an exception if it is passed to this method and we’re not required.
-
getExtraDocLines
()[source]¶ Return a list of ReST formatted lines that will be added to the docstring returned by
getDoc()
.By default, this will include information about the various properties of this object, such as required and readonly status, required type, and so on.
This implementation uses a field list for this.
Subclasses may override or extend.
New in version 4.6.0.
-