Skip to content

Commit

Permalink
Initial (early) version of package data validation
Browse files Browse the repository at this point in the history
Somewhat incomplete and may possibly trigger false alarms on some valid inputs
  • Loading branch information
yrashk committed Sep 30, 2012
1 parent f69dd89 commit eac11ac
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/package.ex
Expand Up @@ -107,4 +107,6 @@ defrecord Expm.Package,
pkg
end

defdelegate [valid?(package), validate(package)], to: Expm.Package.Validator

end
66 changes: 66 additions & 0 deletions lib/package/validator.ex
@@ -0,0 +1,66 @@
defmodule Expm.Package.Validator do
alias Validatex, as: V

def validation(package) do
[
{:name, package.name, V.Format.new(re: %r/^([a-z]|_|-|[0-9])+$/i)},
{:description, package.description, V.Format.new(re: %r/.*/i, allow_nil: true, allow_empty: true)},
{:version, package.version, V.Union.new(options: [V.Type.new(is: :atom, allow_nil: false, allow_undefined: false),
V.Format.new(re: %r/([0-9])(([0-9]\.)*(-.+)?)?/i)])},
{:keywords, package.keywords, V.Type.new(is: :list)},
{:maintainers, package.maintainers, V.Type.new(is: :list)},
{:maintainers, package.maintainers, V.Length.new(is: V.Range.new(from: 1))},
{:contributors, package.maintainers, V.Type.new(is: :list)},
{:homepage, package.homepage, V.Type.new(is: :string, allow_nil: true)},
{:directories, package.directories, V.Type.new(is: :list)},
{:repositories, package.repositories, V.Type.new(is: :list)},
{:repositories, package.repositories, V.Length.new(is: V.Range.new(from: 1))},
] ++
lc keyword inlist package.keywords do
{{:keyword, keyword}, keyword, V.Neg.new(message: :disallowed_characters, validation: V.Format.new(re: %r/[\s,]+/i))}
end ++
lc directory inlist package.directories do
{{:directory, directory}, directory, V.Type.new(is: :string)}
end ++
List.flatten(
lc maintainer inlist package.maintainers do
person_validation(:maintainer, maintainer)
end) ++
List.flatten(
lc contributor inlist package.contributors do
person_validation(:contributor, contributor)
end) ++
List.flatten(
lc repository inlist package.repositories do
[
{{:repository, repository, :type}, repository[:github] || repository[:git], V.Neg.new(validation: nil, message: :scm_type_required)},
] ++
if repository[:github] do
[{{:repository, repository, :github}, repository[:github], V.Format.new(re: "[a-z0-9_-]+/[a-z0-9_-]+", allow_nil: true, allow_empty: true)}]
else
[]
end ++
if repository[:git] do
[{{:repository, repository, :git}, repository[:git], V.Format.new(re: ".+", allow_nil: true, allow_empty: true)}]
else
[]
end
end)
end

def validate(package) do
V.validate(validation(package))
end

def valid?(package) do
validate(package) == []
end

defp person_validation(t, v) do
[
{{t, v}, v[:name], V.Length.new(is: V.Range.new(from: 1))},
{{t, v}, v[:name], V.Type.new(is: :string)},
{{t, v}, v[:email],V.Type.new(is: :string, allow_nil: true)},
]
end
end
1 change: 1 addition & 0 deletions mix.exs
Expand Up @@ -17,6 +17,7 @@ defmodule Expm.Mixfile do

defp deps do
[
{:validatex, %r(.*), github: "yrashk/validatex"},
{:hackney, %r(.*), github: "benoitc/hackney"},
{:mimetypes, %r(.*), github: "spawngrid/mimetypes"},
{:edown, %r(.*), github: "esl/edown"},
Expand Down
3 changes: 2 additions & 1 deletion mix.lock
Expand Up @@ -5,4 +5,5 @@
"lager": "9981ca0b4d7a57a421f7a6bba71e2087c0d614ed",
"lagerex": "4152858bfb058b9e2fc91cd7df9270ecb621a019",
"mimetypes": "e9dfab5aec98963589ecf13bdfcf0490667a730d",
"ranch": "cd099983b1b807b87fa050d1e4ff0a13aba25b49" ]
"ranch": "cd099983b1b807b87fa050d1e4ff0a13aba25b49",
"validatex": "f6e1d934647388f297132edcf80bcf7036b5eb09" ]

0 comments on commit eac11ac

Please sign in to comment.