Validate script vars

This commit is contained in:
Alex Kotov 2023-10-01 00:11:32 +04:00
parent b0c7de3aae
commit b83e340dbe
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
2 changed files with 15 additions and 3 deletions

View File

@ -18,6 +18,10 @@ module Referator
NAME_RE = /\A\w+(_\w+)*\z/
SLUG_RE = /\A\w+(-\w+)*\z/
SCRIPT_ENUMS = %i[format template].sort.freeze
SCRIPT_OBJS = %i[notes].sort.freeze
SCRIPT_VARS = [*SCRIPT_ENUMS, *SCRIPT_OBJS].sort.freeze
def self.validate_name!(name)
name.freeze
unless name.instance_of? Symbol
@ -37,4 +41,14 @@ module Referator
slug
end
def self.validate_script_var!(var)
var.freeze
unless var.instance_of? Symbol
raise TypeError, "Expected #{String}, got #{slug.class}"
end
raise 'Invalid var' unless SCRIPT_VARS.include? var
var
end
end

View File

@ -105,9 +105,7 @@ module Referator
def vars=(vars)
@vars = [*vars].uniq.sort.freeze.each do |var|
unless var.instance_of? Symbol
raise TypeError, "Expected #{Symbol}, got #{var.class}"
end
Referator.validate_script_var! var
end
end