mirror of
https://github.com/aljazceru/CTFd.git
synced 2026-02-02 04:44:25 +01:00
field based schema validation (#1789)
* Clean up Page and Challenges schema validation
This commit is contained in:
@@ -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",
|
||||
)
|
||||
],
|
||||
)
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user