Пример #1
0
class WorkspaceSchema(AutoSchema):

    name = fields.String(required=True,
                         validate=validate.Regexp(
                             r"^[a-z0-9][a-z0-9\_\$\(\)\+\-\/]*$",
                             0,
                             error="ERORROROR"))
    stats = SelfNestedField(WorkspaceSummarySchema())
    duration = SelfNestedField(WorkspaceDurationSchema())
    _id = fields.Integer(dump_only=True, attribute='id')
    scope = MutableField(
        PrimaryKeyRelatedField('name', many=True, dump_only=True),
        fields.List(fields.String))
    active = fields.Boolean(dump_only=True)

    create_date = fields.DateTime(attribute='create_date', dump_only=True)

    update_date = fields.DateTime(attribute='update_date', dump_only=True)

    class Meta:
        model = Workspace
        fields = ('_id', 'id', 'customer', 'description', 'active', 'duration',
                  'name', 'public', 'scope', 'stats', 'create_date',
                  'update_date', 'readonly')

    @post_load
    def post_load_duration(self, data, **kwargs):
        # Unflatten duration (move data[duration][*] to data[*])
        duration = data.pop('duration', None)
        if duration:
            data.update(duration)
        if 'start_date' in data and 'end_date' in data and data[
                'start_date'] and data['end_date']:
            if data['start_date'] > data['end_date']:
                raise ValidationError("start_date is bigger than end_date.")
        return data
Пример #2
0
from marshmallow import Schema, fields, validate

dimension_name_validation = validate.Regexp('^[a-z_][a-z0-9_]*$')

ParamsDefinition = fields.Dict(keys=fields.String(
    validate=validate.Regexp('^[A-Za-z_][A-Za-z0-9_.]*$')))


class DimensionValueDefinition(Schema
                               ):  # pylint: disable = missing-class-docstring
    params = ParamsDefinition


class Dimension(Schema):
    '''
    One dimension including all its possible value definitions.
    '''
    priority = fields.Number(required=True)
    values = fields.Dict(
        keys=fields.String(validate=dimension_name_validation),
        values=fields.Nested(DimensionValueDefinition))


class Execution(Schema):
    '''
    One conceptual model for how to execute tasks or services on a
    particular backend.
    '''
    name = fields.String(required=True)
    driver = fields.String(required=True, validate=validate.OneOf(['nomad']))
    logstore = fields.String(required=True, validate=validate.OneOf(['nomad']))
Пример #3
0
def test_regexp_custom_message():
    rex = r'[0-9]+'
    v = validate.Regexp(rex, error='{input} does not match {regex}')
    with pytest.raises(ValidationError) as excinfo:
        v('a')
    assert 'a does not match [0-9]+' in str(excinfo)
Пример #4
0
 def __init__(self) -> None:
     super().__init__(required=True, validate=validate.Regexp(r"^[A-Z]{2}$"))
Пример #5
0
 def __init__(self, *args, **kwargs):
     kwargs['validate'] = validate.Regexp(
         r'^9\d{9}$', error='Указан не мобильный телефон')
     super().__init__(*args, **kwargs)
Пример #6
0
class ManifestSchema(BaseSchema):
    # Required fields
    name = fields.Str(
        required=True,
        validate=[
            validate.Length(min=1, max=100),
            validate.Regexp(r"^[^:;/,@\<\>]+$",
                            error="The next chars [:;/,@<>] are not allowed"),
        ],
    )
    version = fields.Str(required=True,
                         validate=validate.Length(min=1, max=50))

    # Optional fields

    authors = fields.Nested(AuthorSchema, many=True)
    description = fields.Str(validate=validate.Length(min=1, max=1000))
    homepage = fields.Url(validate=validate.Length(min=1, max=255))
    license = fields.Str(validate=validate.Length(min=1, max=255))
    repository = fields.Nested(RepositorySchema)
    dependencies = fields.Nested(DependencySchema, many=True)

    # library.json
    export = fields.Nested(ExportSchema)
    examples = fields.Nested(ExampleSchema, many=True)
    downloadUrl = fields.Url(validate=validate.Length(min=1, max=255))

    keywords = StrictListField(
        fields.Str(validate=[
            validate.Length(min=1, max=50),
            validate.Regexp(r"^[a-z\d\-\+\. ]+$",
                            error="Only [a-z0-9-+. ] chars are allowed"),
        ]))
    platforms = StrictListField(
        fields.Str(validate=[
            validate.Length(min=1, max=50),
            validate.Regexp(r"^([a-z\d\-_]+|\*)$",
                            error="Only [a-z0-9-_*] chars are allowed"),
        ]))
    frameworks = StrictListField(
        fields.Str(validate=[
            validate.Length(min=1, max=50),
            validate.Regexp(r"^([a-z\d\-_]+|\*)$",
                            error="Only [a-z0-9-_*] chars are allowed"),
        ]))

    # platform.json specific
    title = fields.Str(validate=validate.Length(min=1, max=100))

    # package.json specific
    system = StrictListField(
        fields.Str(validate=[
            validate.Length(min=1, max=50),
            validate.Regexp(r"^[a-z\d\-_]+$",
                            error="Only [a-z0-9-_] chars are allowed"),
        ]))

    @validates("version")
    def validate_version(self, value):  # pylint: disable=no-self-use
        try:
            value = str(value)
            assert "." in value
            semantic_version.Version.coerce(value)
        except (AssertionError, ValueError):
            raise ValidationError(
                "Invalid semantic versioning format, see https://semver.org/")

    @validates("license")
    def validate_license(self, value):
        try:
            spdx = self.load_spdx_licenses()
        except requests.exceptions.RequestException:
            raise ValidationError(
                "Could not load SPDX licenses for validation")
        for item in spdx.get("licenses", []):
            if item.get("licenseId") == value:
                return True
        raise ValidationError(
            "Invalid SPDX license identifier. See valid identifiers at "
            "https://spdx.org/licenses/")

    @staticmethod
    @memoized(expire="1h")
    def load_spdx_licenses():
        spdx_data_url = "https://dl.bintray.com/platformio/dl-misc/spdx-licenses-3.json"
        return json.loads(fetch_remote_content(spdx_data_url))
Пример #7
0
import json
from marshmallow import Schema, fields, validate
from aiohttp.web import HTTPUnprocessableEntity
from app_types.users import UserType
from schemas.base import BasePaginationSchema

phone_validator = validate.Regexp("^((7)+([0-9]){10})$", 0,
                                  'Invalid phone format')


def _deserialize_user_type(value):
    value = value.upper()
    if value in [t.name for t in UserType]:
        return UserType[value]
    else:
        raise HTTPUnprocessableEntity(text=json.dumps({
            "type": ["Unsupported value"],
        }))


class UserSchema(Schema):
    uuid = fields.UUID()

    login = fields.String(allow_none=True)
    password = fields.String(required=False)

    name = fields.String(allow_none=True)
    surname = fields.String(allow_none=True)
    middle_name = fields.String(allow_none=True)

    phone = fields.String(allow_none=True)
Пример #8
0
from marshmallow import Schema, fields, validate
import string

# THis is used for more sensitive fields
generalString = fields.String(required=True, allow_none=False, 
    validate=validate.Regexp("^[a-zA-Z0-9 ,-_\(\)]+$", 
    error="Special Characters Not Allowed"))

# More general-er version of a string
generalerString = fields.String(required=True, allow_none=False)

generalerNotRequiredString = fields.String(required=False, allow_none=True)

generalInteger = fields.Integer(required=True, allow_none=False, error="That's not an integer")

generalTimeStamp = fields.AwareDateTime(format="iso8601", default_timezone="utc", required=True, allow_none=False, error="Not a valid ISO timecode, try again")

generalID = fields.String(required=True, allow_none=False,
    validate=[validate.Length(equal=32)], error="That's not a valid UUID")

email = fields.Email(required=True, allow_none=False)

username = fields.String(required=True, allow_none=False, 
    validate=[validate.Length(max=50), validate.Regexp("^[a-zA-Z0-9_-]+$", 
    error="Special characters (other than _ and -) not allowed in username")])

password = fields.String(required=True, validate=[validate.Length(min=8, max=256), 
    validate.Regexp("^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W)(?!.*\s).{8,}$",
    error="Password not strong enough, requires at least 8 digits, at least one upper case \
character, one lower case character and a special character")])
Пример #9
0
 def __init__(self, pattern, nested_field, *args, **kwargs):
     fields.Field.__init__(self, *args, **kwargs)
     self.key_field = fields.Str(validate=validate.Regexp(pattern))
     self.nested_field = nested_field
Пример #10
0
def get_field_validator(field_name):
    allowed_values = ALLOWED_VALUES[field_name]
    return validate.OneOf(allowed_values) if isinstance(allowed_values, list) else validate.Regexp(allowed_values)
Пример #11
0
class MessageErrorSchema(Schema):

    message = fields.Str(required=True,
                         validate=validate.Regexp(CONST.MESSAGE_ERROR_REGEX))
Пример #12
0
class SystemProfileSchema(MarshmallowSchema):
    number_of_cpus = fields.Int()
    number_of_sockets = fields.Int()
    cores_per_socket = fields.Int()
    system_memory_bytes = fields.Int()
    infrastructure_type = fields.Str(validate=marshmallow_validate.Length(
        max=100))
    infrastructure_vendor = fields.Str(validate=marshmallow_validate.Length(
        max=100))
    network_interfaces = fields.List(fields.Nested(NetworkInterfaceSchema()))
    disk_devices = fields.List(fields.Nested(DiskDeviceSchema()))
    bios_vendor = fields.Str(validate=marshmallow_validate.Length(max=100))
    bios_version = fields.Str(validate=marshmallow_validate.Length(max=100))
    bios_release_date = fields.Str(validate=marshmallow_validate.Length(
        max=50))
    cpu_flags = fields.List(
        fields.Str(validate=marshmallow_validate.Length(max=30)))
    operating_system = fields.Nested(OperatingSystemSchema())
    os_release = fields.Str(validate=marshmallow_validate.Length(max=100))
    os_kernel_version = fields.Str(validate=marshmallow_validate.Length(
        max=100))
    arch = fields.Str(validate=marshmallow_validate.Length(max=50))
    kernel_modules = fields.List(
        fields.Str(validate=marshmallow_validate.Length(max=255)))
    last_boot_time = fields.Str(validate=marshmallow_validate.Length(max=50))
    running_processes = fields.List(
        fields.Str(validate=marshmallow_validate.Length(max=1000)))
    subscription_status = fields.Str(validate=marshmallow_validate.Length(
        max=100))
    subscription_auto_attach = fields.Str(validate=marshmallow_validate.Length(
        max=100))
    katello_agent_running = fields.Bool()
    satellite_managed = fields.Bool()
    cloud_provider = fields.Str(validate=marshmallow_validate.Length(max=100))
    yum_repos = fields.List(fields.Nested(YumRepoSchema()))
    dnf_modules = fields.List(fields.Nested(DnfModuleSchema()))
    installed_products = fields.List(fields.Nested(InstalledProductSchema()))
    insights_client_version = fields.Str(validate=marshmallow_validate.Length(
        max=50))
    insights_egg_version = fields.Str(validate=marshmallow_validate.Length(
        max=50))
    captured_date = fields.Str(validate=marshmallow_validate.Length(max=32))
    installed_packages = fields.List(
        fields.Str(validate=marshmallow_validate.Length(max=512)))
    installed_services = fields.List(
        fields.Str(validate=marshmallow_validate.Length(max=512)))
    enabled_services = fields.List(
        fields.Str(validate=marshmallow_validate.Length(max=512)))
    sap_system = fields.Bool()
    sap_sids = fields.List(
        fields.Str(validate=[
            marshmallow_validate.Length(max=3),
            marshmallow_validate.Regexp(regex=r"^[A-Z][A-Z0-9]{2}$")
        ]))
    sap_instance_number = fields.Str(validate=[
        marshmallow_validate.Length(max=2),
        marshmallow_validate.Regexp(regex=r"(?!99)(?!98)^[0-9]{2}$")
    ])
    sap_version = fields.Str(validate=[
        marshmallow_validate.Length(max=22),
        marshmallow_validate.Regexp(
            regex=r"^[0-9].[0-9]{2}.[0-9]{3}.[0-9]{2}.[0-9]{10}$"),
    ])
    tuned_profile = fields.Str(validate=marshmallow_validate.Length(max=256))
    selinux_current_mode = fields.Str(validate=marshmallow_validate.OneOf(
        ["enforcing", "permissive", "disabled"]))
    selinux_config_file = fields.Str(validate=marshmallow_validate.OneOf(
        ["enforcing", "permissive", "disabled"]))
Пример #13
0
class MessageSchema(Schema):
    """ Schema for message validation """
    message = fields.Str(required=True, validate=validate.Length(
        min=1, error="Message can't be blank."))
    phone_number = fields.Str(required=True, validate=[validate.Length(min=1, error="Phone number can't be empty."), validate.Regexp(
        '^\+(?:[0-9]){6,14}[0-9]$', error="Must be a valid phone number with country code.")])
    token = fields.Str(required=True, validate=validate.Length(
        min=1, error="Token can't be empty."))
Пример #14
0
# -*- coding: utf-8 -*-
#
# Copyright (C) 2020 CERN.
#
# Invenio-Records-Resources is free software; you can redistribute it and/or
# modify it under the terms of the MIT License; see LICENSE file for more
# details.

"""Vocabulary service schema."""

from invenio_records_resources.services.records.schema import RecordSchema
from marshmallow import EXCLUDE, Schema, fields, validate

i18n_string = fields.Dict(
    allow_none=False,
    keys=fields.Str(validate=validate.Regexp("^[a-z]{2}$")),
    values=fields.Str(),
)


class VocabularyMetadataSchema(Schema):
    """."""

    class Meta:
        """Meta class to reject unknown fields."""

        unknown = EXCLUDE

    title = i18n_string
    description = i18n_string
    icon = fields.Str(allow_none=False)
Пример #15
0
class IndyProofReqAttrSpecSchema(OpenAPISchema):
    """Schema for attribute specification in indy proof request."""

    name = fields.String(example="favouriteDrink",
                         description="Attribute name",
                         required=False)
    names = fields.List(
        fields.String(example="age"),
        description="Attribute name group",
        required=False,
    )
    restrictions = fields.List(
        fields.Dict(
            keys=fields.Str(
                validate=validate.Regexp(
                    "^schema_id|"
                    "schema_issuer_did|"
                    "schema_name|"
                    "schema_version|"
                    "issuer_did|"
                    "cred_def_id|"
                    "attr::.+::value$"  # indy does not support attr::...::marker here
                ),
                example="cred_def_id",  # marshmallow/apispec v3.0 ignores
            ),
            values=fields.Str(example=INDY_CRED_DEF_ID["example"]),
        ),
        description=
        ("If present, credential must satisfy one of given restrictions: specify "
         "schema_id, schema_issuer_did, schema_name, schema_version, "
         "issuer_did, cred_def_id, and/or attr::<attribute-name>::value "
         "where <attribute-name> represents a credential attribute name"),
        required=False,
    )
    non_revoked = fields.Nested(IndyProofReqNonRevokedSchema(), required=False)

    @validates_schema
    def validate_fields(self, data, **kwargs):
        """
        Validate schema fields.

        Data must have exactly one of name or names; if names then restrictions are
        mandatory.

        Args:
            data: The data to validate

        Raises:
            ValidationError: if data has both or neither of name and names

        """
        if ("name" in data) == ("names" in data):
            raise ValidationError(
                "Attribute specification must have either name or names but not both"
            )
        restrictions = data.get("restrictions")
        if ("names" in data) and (not restrictions
                                  or all(not r for r in restrictions)):
            raise ValidationError(
                "Attribute specification on 'names' must have non-empty restrictions"
            )
Пример #16
0
class BioMeta(Schema):
    postcode_regex = '([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9][A-Za-z]?)))))'
    get_counties = [
        'BEDFORDSHIRE', 'BERKSHIRE', 'BRISTOL', 'BUCKINGHAMSHIRE',
        'CAMBRIDGESHIRE', 'CHESHIRE', 'CITY OF LONDON', 'CORNWALL',
        'COUNTY DURHAM', 'CUMBRIA', 'DERBYSHIRE', 'DEVON', 'DORSET',
        'BIRMINGHAM', 'EAST RIDING OF YORKSHIRE', 'EAST SUSSEX', 'ESSEX',
        'GLOUCESTERSHIRE', 'GREATER LONDON', 'GREATER MANCHESTER', 'HAMPSHIRE',
        'HEREFORDSHIRE', 'HERTFORDSHIRE', 'ISLE OF WIGHT', 'KENT',
        'LANCASHIRE', 'LEICESTERSHIRE', 'LINCOLNSHIRE', 'MERSEYSIDE',
        'NORFOLK', 'NORTH YORKSHIRE', 'NORTHAMPTONSHIRE', 'NORTHUMBERLAND',
        'NOTTINGHAMSHIRE', 'OXFORDSHIRE', 'RUTLAND', 'SHROPSHIRE', 'SOMERSET',
        'SOUTH YORKSHIRE', 'STAFFORDSHIRE', 'SUFFOLK', 'SURREY',
        'TYNE AND WEAR', 'WARWICKSHIRE', 'WEST MIDLANDS', 'WEST SUSSEX',
        'WEST YORKSHIRE', 'WILTSHIRE', 'WORCESTERSHIRE', 'MIDDLESEX'
    ]

    central_sample_id = fields.Str(
        data_key="COG Sample ID",
        required=True,
        validate=validate.Regexp("^NORW-[a-zA-Z0-9]{5,6}$"))
    biosample_source_id = fields.Str(data_key="NNUH Sample ID", required=True)
    adm1 = fields.Str(missing="UK-ENG")
    adm2 = fields.Str(data_key="County", validate=validate.OneOf(get_counties))
    source_age = fields.Integer(data_key="Age",
                                validate=validate.Range(min=0, max=120))
    source_sex = fields.Str(data_key="Sex",
                            validate=validate.OneOf(['M', 'F']))
    received_date = fields.Str()
    collection_date = fields.Date(data_key="Collection date",
                                  validate=lambda x: x > collection_date_min)
    sample_type_collected = fields.Str(
        data_key="Source",
        validate=validate.OneOf(["dry swab", "swab", "sputum", "aspirate"]))
    swab_site = fields.Str(data_key="Body site",
                           validate=validate.OneOf([
                               "nose", "throat", "nose-throat", "endotracheal",
                               "rectal"
                           ]))
    collecting_org = fields.Str(data_key="Collecting organisation")
    library_name = fields.Str()
    library_seq_kit = fields.Str(missing='Nextera')
    library_seq_protocol = fields.Str(missing='Nextera LITE')
    library_layout_config = fields.Str(missing='PAIRED')
    library_selection = fields.Str(missing='PCR')
    library_source = fields.Str(missing='VIRAL_RNA')
    library_strategy = fields.Str(missing='AMPLICON')
    library_primers = fields.Integer(missing=3)
    library_protocol = fields.Str(missing='ARTICv2')
    run_name = fields.Str()
    previous_runs = fields.Str()
    instrument_make = fields.Str(missing='ILLUMINA')
    instrument_model = fields.Str(missing='NextSeq 500')
    adm2_private = fields.Str(data_key="Outer Postcode",
                              validate=validate.Regexp(postcode_regex))
    date_sequenced = fields.Str()
    repeat_sample_id = fields.Str(data_key="Repeat Sample ID")
    is_surveillance = fields.Str(missing='Y')
    is_icu_patient = fields.Str(data_key="ICU admission",
                                validate=validate.OneOf(['Y', 'N', 'Unknown']))
    ct_1_ct_value = fields.Str()
    ct_2_ct_value = fields.Str()

    @pre_load
    def clean_up(self, in_data, **kwargs):
        if in_data.get('Collecting organisaton'):
            in_data['Collecting organisation'] = in_data.get(
                'Collecting organisaton')
        if in_data.get('Collecting organisaton') == 'JPH':
            in_data['Collecting organisation'] = 'JPUH'

        for k, v in dict(in_data).items():
            if v in ['', 'to check', 'Not stated']:
                in_data.pop(k)
            elif k in ['County', 'Collecting organisation', 'Outer Postcode'
                       ] and v.upper() in [
                           'NOT AVAILABLE', 'UNKNOWN', 'NA', 'NO ADDRESS',
                           'NO POST CODE', 'UNKOWN'
                       ]:
                in_data.pop(k)
            elif k in ['Sex'] and v.upper() in ['U', 'N', 'UNKNOWN', 'UNKOWN']:
                in_data.pop(k)
            elif k in ['ICU admission'
                       ] and v.upper() in ['U', 'UKNOWN', 'N/A']:
                in_data.pop(k)
            elif isinstance(v, str):
                in_data[k] = v.strip()
        if in_data.get(''):
            in_data['ct_2_ct_value'] = str(in_data.get(''))
        if in_data.get('PCR Ct value', ''):
            in_data['ct_1_ct_value'] = str(in_data.get('PCR Ct value'))
        if in_data.get('Sex', '').lower() in ['male']:
            in_data['Sex'] = 'M'
        if in_data.get('Sex', '').lower() in ['female']:
            in_data['Sex'] = 'F'
        if in_data.get('Source', '').lower() in [
                'endotracheal aspirate', 'bronchial washings',
                'bronchial washing'
        ]:
            in_data['Source'] = 'aspirate'
        if in_data.get("County"):
            in_data["County"] = in_data["County"].upper()
        if in_data.get("County", '').upper() in [
                'CAMBS', 'CAMBRIDESHIRE', 'CAMBRIDGE', 'CAMBRIDGSHIRE'
        ]:
            in_data["County"] = 'CAMBRIDGESHIRE'
        if in_data.get("County", '').upper() == 'LINC':
            in_data["County"] = 'LINCOLNSHIRE'
        if in_data.get("County", '').upper() == 'LONDON':
            in_data["County"] = 'GREATER LONDON'
        if in_data.get("County", '').upper() == 'COLCHESTER':
            in_data["County"] = 'ESSEX'
        if in_data.get("County", '').lower() == 'leicestshire':
            in_data["County"] = 'LEICESTERSHIRE'
        if in_data.get("Source"):
            in_data["Source"] = in_data["Source"].lower()
        if in_data.get('Body site'):
            if in_data.get('Body site').lower() in [
                    'nose and throat', 'nose &throat', 'nose & troat',
                    'nose & throat', 'throat/nose', 'nose/throat'
            ]:
                in_data['Body site'] = 'nose-throat'
            elif in_data.get('Body site').lower() in ['lung', "tracheostomy"]:
                in_data['Body site'] = 'endotracheal'
            elif in_data.get('Body site').lower() in ['mouth', 'throat/swab']:
                in_data['Body site'] = 'throat'
            else:
                in_data['Body site'] = in_data.get('Body site').lower()
        if in_data.get('ICU admission', '').lower() in ['yes']:
            in_data['ICU admission'] = 'Y'
        if in_data.get('ICU admission', '').lower() in ['no']:
            in_data['ICU admission'] = 'N'
        if in_data.get('Collected by QIB'):
            in_data["received_date"] = self.handle_dates(
                in_data['Collected by QIB'])
        if in_data.get('Collection date'):
            in_data['Collection date'] = self.handle_dates(
                in_data['Collection date'])
        elif not in_data.get("received_date"):
            in_data["received_date"] = datetime.datetime.now().strftime(
                '%Y-%m-%d')
        return in_data

    def handle_dates(self, date_string):
        try:
            datetime.datetime.strptime(date_string, '%Y-%m-%d')
            # String is fine, return itself.
            return date_string
        except ValueError:
            try:
                datetime_obj = datetime.datetime.strptime(
                    date_string, '%d/%m/%Y')
                return datetime_obj.strftime('%Y-%m-%d')
            except ValueError:
                try:
                    datetime_obj = datetime.datetime.strptime(
                        date_string, '%d.%m.%Y')
                    return datetime_obj.strftime('%Y-%m-%d')
                except ValueError:
                    raise
Пример #17
0
                                 status_code=400,
                                 errno=108)

    @post_load
    def convert_chid(self, data):
        if "channelID" in data:
            # ALWAYS RETURN CHID AS .HEX, NO DASH
            data["channelID"] = data["channelID"].hex


class TokenSchema(SubInfoSchema):
    """Filters allowed values from body data"""
    token = fields.Str(allow_none=True)


valid_token = validate.Regexp("^[^ ]{8,}$")


class GCMTokenSchema(SubInfoSchema):
    token = fields.Str(allow_none=False,
                       validate=valid_token,
                       error="Missing required token value")


class APNSTokenSchema(SubInfoSchema):
    token = fields.Str(allow_none=False,
                       validate=valid_token,
                       error="Missing required token value")
    aps = fields.Dict(allow_none=True)

Пример #18
0
class ProblemReportSchema(AgentMessageSchema):
    """Schema for ProblemReport base class."""

    class Meta:
        """Problem report schema metadata."""

        model_class = ProblemReport

    msg_catalog = fields.Str(
        data_key="@msg_catalog",
        required=False,
        description="Reference to a message catalog",
        example="did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/error-codes",
    )
    locale = fields.Str(
        data_key="@locale", required=False, description="Locale", example="en-US"
    )
    explain_ltxt = fields.Str(
        data_key="explain-ltxt",
        required=False,
        description="Localized message",
        example="Item not found",
    )
    explain_l10n = fields.Dict(
        keys=fields.Str(description="Locale"),
        values=fields.Str(description="Localized message"),
        required=False,
        description="Dictionary of localizations",
    )
    problem_items = fields.List(
        fields.Dict(
            keys=fields.Str(description="Problematic parameter or item"),
            values=fields.Str(description="Problem text/number/value"),
            description="Problem item",
        ),
        data_key="problem-items",
        required=False,
        description="List of problem items",
    )
    who_retries = fields.Str(
        data_key="who-retries",
        required=False,
        description="Party to retry: you, me, both, none",
        example="you",
        validate=validate.OneOf(["you", "me", "both", "none"]),
    )
    fix_hint_ltxt = fields.Dict(
        keys=fields.Str(description="Locale", example="en-US"),
        values=fields.Str(
            description="Localized message", example="Synchronize time to NTP"
        ),
        data_key="fix-hint-ltxt",
        required=False,
        description="Human-readable localized suggestions how to fix problem",
    )
    impact = fields.Str(
        required=False,
        description="Breadth of impact of problem: message, thread, or connection",
        example="thread",
        validate=validate.OneOf(["message", "thread", "connection"]),
    )
    where = fields.Str(
        required=False,
        description="Where the error occurred, from reporter perspective",
        example="you - agency",
        validate=validate.Regexp(r"(you)|(me)|(other) - .+"),
    )
    time_noticed = fields.Str(
        data_key="time-noticed",
        required=False,
        description="Problem detection time, precision at least day up to millisecond",
        example="1970-01-01 00:00:00.000Z",
        validate=validate.Regexp(
            r"^\d{4}-\d\d-\d\d"
            r"(?:(?: \d\d:\d\d(?:\:\d\d(?:\.\d{1,6})?)(?:[+=]\d\d:?\d\d|Z)?)?)$"
        ),
    )
    tracking_uri = fields.Str(
        data_key="tracking-uri",
        required=False,
        description="URI allowing recipient to track error status",
        example="http://myservice.com/status",
    )
    escalation_uri = fields.Str(
        data_key="escalation-uri",
        required=False,
        description="URI to supply additional help",
        example="mailto://[email protected]",
    )
Пример #19
0
class AdministratorAdminSchema(ma.Schema):
    """Admin schema for Administrator model"""

    # Rules:
    #  1) 3 out of 4:
    #    a) Upper case
    #    b) Lower case
    #    c) Number
    #    d) Non-alpha character
    #  2) 8-40 characters
    re_password = ''.join([
        r'^(?:',
        r'(?=.*[a-z])',
        r'(?:(?=.*[A-Z])(?=.*[\d\W])|(?=.*\W)(?=.*\d))',
        r'|(?=.*\W)(?=.*[A-Z])(?=.*\d)',
        r').{8,40}$'
    ])

    class Meta:
        """AdministratorAdminSchema meta data"""

        model = Administrator

        # fields to expose
        fields = ('id', 'username', 'email', 'first_name', 'last_name',
                  'roles', 'status', 'status_changed_at', 'uri', 'created_at',
                  'updated_at', 'password', 'password_changed_at', 'joined_at')
        load_only = ['password']
        unknown = EXCLUDE  # fix for `role` property after marshmallow 3

    # hyperlinks
    uri = ma.AbsoluteUrlFor('admin_administrators.get_administrator',
                            values=dict(administrator_id='<id>'))

    # nested schema
    roles = fields.Nested('RoleAdminSchema', only=('id', 'name'), many=True,
                          dump_only=True)

    # field validation
    # @todo: validate roles is a list
    id = fields.Integer()
    username = fields.String(
        required=True,
        validate=[
            validate.Length(
                2, 40,
                error="Value must be between 2 and 40 characters long."),
            validate.Regexp(
                r'(?!^\d+$)^.+$', 0,
                error='Value must not be a number.'),
            validate.Regexp(
                r'^\w+$', 0,
                error=''.join(["Value must contain only alphanumeric ",
                               "characters and the underscore."])),
        ])
    email = fields.Email(required=True)
    first_name = fields.String(
        required=True,
        validate=validate.Length(
            1, 40,
            error="Value must be between 1 and 40 characters long."))
    last_name = fields.String(
        required=True,
        validate=validate.Length(
            2, 40,
            error="Value must be between 2 and 40 characters long."))
    status = fields.Integer(required=True)
    password_changed_at = fields.DateTime(format=Formats.ISO_8601_DATETIME)
    joined_at = fields.DateTime(
        required=True, format=Formats.ISO_8601_DATETIME)
    status_changed_at = fields.DateTime(format=Formats.ISO_8601_DATETIME)
    created_at = fields.DateTime(format=Formats.ISO_8601_DATETIME)
    updated_at = fields.DateTime(format=Formats.ISO_8601_DATETIME)
    password = fields.String(
        required=True,
        validate=validate.Regexp(
            re_password, 0,
            error='Please choose a more complex password.'))
class BetRequestSchema(Schema):
    bet_type = fields.Integer(validate=[Range(min=1, max=3, error='Not negative')], required=True)
    bet_amount = fields.Integer(validate=[Range(min=1, error='> 1')], required=True)
    bet_content = fields.String(validate=validate.Regexp('^([0-9]{1,2}-[0-9]{1,2}|[0-9])$'), required=True)
Пример #21
0
 def __init__(self, **metadata):
     super().__init__(**metadata)
     self.validators = [validate.Regexp("^#[a-fA-F0-9]{3,6}$")] + list(
         self.validators)
Пример #22
0
class UserAccountSchema(ma.Schema):
    """Schema for UserAccount model"""

    # Rules:
    #  1) 3 out of 4:
    #    a) Upper case
    #    b) Lower case
    #    c) Number
    #    d) Non-alpha character
    #  2) 8-40 characters
    re_password = ''.join([
        r'^(?:', r'(?=.*[a-z])',
        r'(?:(?=.*[A-Z])(?=.*[\d\W])|(?=.*\W)(?=.*\d))',
        r'|(?=.*\W)(?=.*[A-Z])(?=.*\d)', r').{8,40}$'
    ])

    class Meta:
        """UserAccountSchema meta data"""

        # fields to expose
        fields = ('id', 'username', 'email', 'password', 'password2', 'tos_id',
                  'first_name', 'last_name', 'password_changed_at',
                  'joined_at', 'is_verified')
        load_only = ['password', 'password2', 'tos_id']
        dump_only = ['joined_at', 'is_verified', 'password_changed_at']

    # field validation
    id = fields.Integer()
    username = fields.String(
        required=True,
        validate=[
            validate.Length(
                2, 40,
                error="Value must be between 2 and 40 characters long."),
            validate.Regexp(r'(?!^\d+$)^.+$',
                            0,
                            error='Value must not be a number.'),
            validate.Regexp(r'^\w+$',
                            0,
                            error=''.join([
                                "Value must contain only alphanumeric ",
                                "characters and the underscore."
                            ])),
        ])
    email = fields.Email(required=True)
    password = fields.String(
        required=True,
        validate=validate.Regexp(
            re_password, 0, error='Please choose a more complex password.'))
    password2 = fields.String(required=True)
    tos_id = fields.Integer(required=True)
    password_changed_at = fields.DateTime(format=Formats.ISO_8601_DATETIME)
    joined_at = fields.DateTime(format=Formats.ISO_8601_DATETIME)
    is_verified = fields.Boolean()

    first_name = fields.String(
        required=True,
        validate=validate.Length(
            1, 40, error="Value must be between 1 and 40 characters long."))
    last_name = fields.String(
        required=True,
        validate=validate.Length(
            2, 40, error="Value must be between 2 and 40 characters long."))
Пример #23
0
 def __init__(self, *args, **kwargs):
     kwargs['validate'] = validate.Regexp(
         r'(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)',
         error='Указан невалидный почтовый адрес')
     super().__init__(*args, **kwargs)
Пример #24
0
def test_regexp_compile():
    assert validate.Regexp(re.compile(r"a"))("a") == "a"
    assert validate.Regexp(re.compile(r"\w"))("_") == "_"
    assert validate.Regexp(re.compile(r"\s"))(" ") == " "
    assert validate.Regexp(re.compile(r"1"))("1") == "1"
    assert validate.Regexp(re.compile(r"[0-9]+"))("1") == "1"
    assert validate.Regexp(re.compile(r"a", re.IGNORECASE))("A") == "A"
    assert validate.Regexp(re.compile(r"a", re.IGNORECASE),
                           re.IGNORECASE)("A") == "A"

    with pytest.raises(ValidationError):
        validate.Regexp(re.compile(r"[0-9]+"))("a")
    with pytest.raises(ValidationError):
        validate.Regexp(re.compile(r"[a-z]+"))("1")
    with pytest.raises(ValidationError):
        validate.Regexp(re.compile(r"a"))("A")
    with pytest.raises(ValidationError):
        validate.Regexp(re.compile(r"a"), re.IGNORECASE)("A")
Пример #25
0
class AddElectricMeterSchema(Schema):
    type = fields.String(required=True)
    name = fields.String(required=True,
                         validate=validate.Regexp(r'[a-zA-Z][A-Za-z0-9]*'))
    custom = fields.Dict()
Пример #26
0
def test_regexp_custom_message():
    rex = r"[0-9]+"
    v = validate.Regexp(rex, error="{input} does not match {regex}")
    with pytest.raises(ValidationError) as excinfo:
        v("a")
    assert "a does not match [0-9]+" in str(excinfo)
Пример #27
0
def test_regexp_compile():
    assert validate.Regexp(re.compile(r'a'))('a') == 'a'
    assert validate.Regexp(re.compile(r'\w'))('_') == '_'
    assert validate.Regexp(re.compile(r'\s'))(' ') == ' '
    assert validate.Regexp(re.compile(r'1'))('1') == '1'
    assert validate.Regexp(re.compile(r'[0-9]+'))('1') == '1'
    assert validate.Regexp(re.compile(r'a', re.IGNORECASE))('A') == 'A'
    assert validate.Regexp(re.compile(r'a', re.IGNORECASE),
                           re.IGNORECASE)('A') == 'A'

    with pytest.raises(ValidationError):
        validate.Regexp(re.compile(r'[0-9]+'))('a')
    with pytest.raises(ValidationError):
        validate.Regexp(re.compile(r'[a-z]+'))('1')
    with pytest.raises(ValidationError):
        validate.Regexp(re.compile(r'a'))('A')
    with pytest.raises(ValidationError):
        validate.Regexp(re.compile(r'a'), re.IGNORECASE)('A')
Пример #28
0
class ColorSchema(Schema):
    color = fields.Str(
        required=True,
        validate=validate.Regexp('^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$'))
Пример #29
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self.validators.append(validate.Regexp(regex='^[a-zA-Z0-9_]+$'))
                                 status_code=400,
                                 errno=108)

    @post_load
    def convert_chid(self, data):
        if "channelID" in data:
            # ALWAYS RETURN CHID AS .HEX, NO DASH
            data["channelID"] = data["channelID"].hex


class TokenSchema(SubInfoSchema):
    """Filters allowed values from body data"""
    token = fields.Str(allow_none=True)


valid_token = validate.Regexp("^[^ ]{8,}$")
valid_adm_token = validate.Regexp("^amzn1.adm-registration.v3.[^ ]{256,}$")


class GCMTokenSchema(SubInfoSchema):
    token = fields.Str(allow_none=False,
                       validate=valid_token,
                       error="Missing required token value")


class APNSTokenSchema(SubInfoSchema):
    token = fields.Str(allow_none=False,
                       validate=valid_token,
                       error="Missing required token value")
    aps = fields.Dict(allow_none=True)