Skip to content

Commit 711ccab

Browse files
committed
Determine reserved routes
1 parent 41c83cb commit 711ccab

1 file changed

Lines changed: 58 additions & 30 deletions

File tree

lib/code_corps/validators/slug_validator.ex

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,69 @@ defmodule CodeCorps.Validators.SlugValidator do
55

66
alias Ecto.Changeset
77

8+
@doc """
9+
Validates a slug.
10+
11+
Matches slugs with:
12+
- only letters
13+
- prefixed/suffixed underscores
14+
- prefixed/suffixed numbers
15+
- single inside dashes
16+
- single/multiple inside underscores
17+
- one character
18+
19+
Prevents slugs with:
20+
- prefixed symbols
21+
- prefixed/suffixed dashes
22+
- multiple consecutive dashes
23+
- single/multiple/multiple consecutive slashes
24+
25+
Also prevents slugs that conflict with reserved routes for either the API or the web.
26+
"""
827
def validate_slug(changeset, field_name) do
9-
# Matches slugs with:
10-
# - only letters
11-
# - prefixed/suffixed underscores
12-
# - prefixed/suffixed numbers
13-
# - single inside dashes
14-
# - single/multiple inside underscores
15-
# - one character
16-
#
17-
# Prevents slugs with:
18-
# - prefixed symbols
19-
# - prefixed/suffixed dashes
20-
# - multiple consecutive dashes
21-
# - single/multiple/multiple consecutive slashes
2228
valid_slug_pattern = ~r/\A((?:(?:(?:[^-\W]-?))*)(?:(?:(?:[^-\W]-?))*)\w+)\z/
2329

24-
# Prevents slugs that conflict with reserved routes
25-
reserved_routes = ~w(
26-
about account admin android api app apps blog bug bugs cache charter
27-
comment comments contact contributor contributors cookies
28-
developer developers discover donate engineering enterprise explore
29-
facebook favorites feed followers following github help home image images
30-
integration integrations invite invitations ios issue issues jobs learn
31-
likes lists log-in log-out login logout mention mentions new news
32-
notification notifications oauth oauth_clients organization organizations
33-
ping popular
34-
press pricing privacy
35-
project projects repositories role roles rules search security session
36-
sessions settings shop showcases sidekiq sign-in sign-out signin signout
37-
signup sitemap slug slugs spotlight stars status tag tags
38-
task-image task-images task-like task-likes task
39-
tasks team teams terms token tokens training trends trust tour twitter
40-
user-role user-roles user-skill user-skills user users watching year
30+
# Routes for the API – api. subdomain
31+
api_routes = ~w(
32+
api
33+
categories comments contributors
34+
images issues
35+
mentions
36+
notifications
37+
oauth oauth_clients organizations organization-memberships
38+
ping projects project-categories
39+
project-skills
40+
repositories roles
41+
skills slugged-route stars
42+
tags tasks task-images task-likes
43+
teams token tokens
44+
user-roles user-skills user users
4145
)
4246

47+
# Routes for the web – www. subdomain
48+
web_routes = ~w(
49+
about account admin android app apps
50+
blog
51+
charter contact cookies
52+
developer developers discover donate
53+
engineering enterprise explore
54+
facebook favorites feed followers following
55+
github
56+
help home
57+
integrations invite invitations ios
58+
jobs
59+
learn likes lists log-in log-out login logout
60+
news notifications
61+
popular press pricing privacy projects
62+
search security session sessions settings shop showcases
63+
sign-in sign-out signin signout signup sitemap spotlight start
64+
team terms training trends trust tour twitter
65+
watching
66+
year
67+
)
68+
69+
reserved_routes = api_routes ++ web_routes
70+
4371
changeset
4472
|> Changeset.validate_format(field_name, valid_slug_pattern)
4573
|> Changeset.validate_exclusion(field_name, reserved_routes)

0 commit comments

Comments
 (0)