Skip to content

Commit

Permalink
Validate provided values match what ESI requires (#93)
Browse files Browse the repository at this point in the history
* Validate provided values match what ESI requires
* Allow single value of same type in array contexts
* Add period to required errors and add semicolon in built in functions
  • Loading branch information
Blacksmoke16 authored Mar 26, 2022
1 parent d53a57f commit 9fcae1a
Show file tree
Hide file tree
Showing 4 changed files with 324 additions and 279 deletions.
22 changes: 18 additions & 4 deletions src/parser/eve_swagger.cr
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,16 @@ module EveSwagger
io << "function #{@name}("
parameters.join(", ", io) { |param, join_io| param.to_s join_io }
io.puts "): SheetsArray {"
parameters.select(&.required).each { |param| io.puts " if (!#{param.name}) throw new Error(`#{param.name} is required`);" }

parameters.each do |p|
p.to_function io
end

io << " return invoke('#{@name}', { "

parameters.join(", ", io) { |param, join_io| param.name.to_s join_io }

io << " })"
io << " });"

io.puts "\n}"
end
Expand Down Expand Up @@ -339,7 +343,7 @@ module EveSwagger
io << '?' unless required
end

io << ": #{self.type}"
io << ':' << ' ' << self.type_string

@default_value.try do |default|
io << ' ' << '=' << ' '
Expand All @@ -348,12 +352,22 @@ module EveSwagger
end
end

def type_string : String
return "" unless (type = @type)

type.ends_with?("[]") ? "#{type[0..-3]}|#{type}" : type
end

def to_doc_s(io : IO) : Nil
io << " * @param {#{self.type}} #{@name} - "
io << " * @param {#{self.type_string}} #{@name} - "

io.puts @description
end

def to_function(io : IO) : Nil
io.puts " if (!#{@name}) throw new Error(`#{@name} is required.`);" if @required
end

def <=>(other : self) : Int32?
@required.try do |required|
return -1 if required
Expand Down
27 changes: 27 additions & 0 deletions src/script/esi_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ class ESIClient {

let result: SheetsArray = [];

if (params.show_column_headings !== undefined && typeof params.show_column_headings !== 'boolean') {
throw new Error(`Expected optional argument show_column_headings to be a boolean, but got ${typeof params.show_column_headings}.`);
}

// Add the header row if its not set, or set to true
if (params.show_column_headings) result.push(endpoint.headers.map((header: IHeader) => header.name));

Expand Down Expand Up @@ -135,6 +139,25 @@ class ESIClient {
// Process this endpoint's parameters
endpoint.parameters.forEach((param: IParameter) => {
const paramValue = params[param.name];
const required = param.required ? 'required' : 'optional';

let paramType = param.type;
let isArrayType = false;

if (paramType.endsWith('[]')) {
paramType = paramType.slice(0, -2);
isArrayType = true;
}

if (isArrayType && 'string' === paramType && '#NAME?' === paramValue) {
throw new Error(`Expected ${required} argument ${param.name} to be a string|string[], but got invalid named range. Put the value in double quotes.`);
} else if (!isArrayType && 'string' === paramType && '#NAME?' === paramValue) {
throw new Error(`Expected ${required} argument ${param.name} to be a string, but got invalid named range. Put the value in double quotes.`);
} else if (!isArrayType && (typeof paramValue !== paramType && (!param.required && undefined !== paramValue))) {
throw new Error(`Expected ${required} argument ${param.name} to be a ${paramType}, but got a ${typeof paramValue}.`);
} else if (isArrayType && (!Array.isArray(paramValue) && typeof paramValue !== paramType)) {
throw new Error(`Expected ${required} argument ${param.name} to be a ${paramType}|${paramType}[], but got a ${typeof paramValue}.`);
}

if (param.in === 'path' && paramValue) {
path = path.replace(`{${param.name}}`, paramValue);
Expand All @@ -147,6 +170,10 @@ class ESIClient {
Array.isArray(paramValue[0]) ?
paramValue.filter((item: any) => item[0]).map((item: any) => item[0]) :
paramValue;

if (isArrayType && !payload.every((i: any) => typeof i === paramType)) {
throw new Error(`Expected ${param.name} to be ${paramType}[], but not every item in the array is a ${paramType}.`);
}
} else {
throw param.type + ' is an unexpected body type.';
}
Expand Down
Loading

0 comments on commit 9fcae1a

Please sign in to comment.