field based schema validation (#1789)

* Clean up Page and Challenges schema validation
This commit is contained in:
Frank
2021-01-30 04:09:09 +08:00
committed by GitHub
parent 7f115bf458
commit 7fe32d7a5d
2 changed files with 73 additions and 46 deletions

View File

@@ -1,4 +1,5 @@
from marshmallow import ValidationError, pre_load
from marshmallow import validate
from marshmallow_sqlalchemy import field_for
from CTFd.models import Challenges, ma
@@ -9,29 +10,39 @@ class ChallengeSchema(ma.ModelSchema):
include_fk = True
dump_only = ("id",)
@pre_load
def validate_name(self, data):
name = data.get("name", "")
if len(name) > 80:
raise ValidationError(
"Challenge could not be saved. Challenge name too long",
field_names=["name"],
name = field_for(
Challenges,
"name",
validate=[
validate.Length(
min=0,
max=80,
error="Challenge could not be saved. Challenge name too long",
)
],
)
@pre_load
def validate_category(self, data):
category = data.get("category", "")
if len(category) > 80:
raise ValidationError(
"Challenge could not be saved. Challenge category too long",
field_names=["category"],
category = field_for(
Challenges,
"category",
validate=[
validate.Length(
min=0,
max=80,
error="Challenge could not be saved. Challenge category too long",
)
],
)
@pre_load
def validate_description(self, data):
description = data.get("description", "")
if len(description) >= 65536:
raise ValidationError(
"Challenge could not be saved. Challenge description is too long.",
field_names=["description"],
description = field_for(
Challenges,
"description",
allow_none=True,
validate=[
validate.Length(
min=0,
max=65535,
error="Challenge could not be saved. Challenge description too long",
)
],
)

View File

@@ -1,4 +1,5 @@
from marshmallow import ValidationError, pre_load
from marshmallow import pre_load, validate
from marshmallow_sqlalchemy import field_for
from CTFd.models import Pages, ma
from CTFd.utils import string_types
@@ -10,34 +11,49 @@ class PageSchema(ma.ModelSchema):
include_fk = True
dump_only = ("id",)
@pre_load
def validate_title(self, data):
title = data.get("title", "")
if len(title) > 128:
raise ValidationError(
"Page could not be saved. Your title is too long.",
field_names=["title"],
title = field_for(
Pages,
"title",
validate=[
validate.Length(
min=0,
max=128,
error="Page could not be saved. Your title is too long.",
)
],
)
route = field_for(
Pages,
"route",
allow_none=True,
validate=[
validate.Length(
min=0,
max=128,
error="Page could not be saved. Your route is too long.",
)
],
)
content = field_for(
Pages,
"content",
allow_none=True,
validate=[
validate.Length(
min=0,
max=65535,
error="Page could not be saved. Your content is too long.",
)
],
)
@pre_load
def validate_route(self, data):
route = data.get("route", "")
if route.startswith("/"):
route = data.get("route")
if route and route.startswith("/"):
data["route"] = route.strip("/")
if len(route) > 128:
raise ValidationError(
"Page could not be saved. Your route is too long.",
field_names=["route"],
)
@pre_load
def validate_content(self, data):
content = data.get("content", "")
if len(content) >= 65536:
raise ValidationError(
"Page could not be saved. Your content is too long.",
field_names=["content"],
)
def __init__(self, view=None, *args, **kwargs):
if view: