mirror of
https://github.com/rjNemo/deno_hello
synced 2026-06-12 12:56:38 +00:00
fix: validation
This commit is contained in:
parent
80788adc40
commit
bd1af7b44a
2 changed files with 7 additions and 5 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
import { ValidationError } from "./validation.ts";
|
import { idIsValid, ValidationError } from "./validation.ts";
|
||||||
import { getLocaleById, localesSize } from "./repository/locales.ts";
|
import { getLocaleById } from "./repository/locales.ts";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Display the iconic Hello, World
|
* Display the iconic Hello, World
|
||||||
|
|
@ -8,10 +8,8 @@ export const sayHello = (id?: number) => {
|
||||||
if (!id) {
|
if (!id) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (idInValidRange(id)) {
|
if (!idIsValid(id)) {
|
||||||
throw new ValidationError(`Invalid index: ${id}`);
|
throw new ValidationError(`Invalid index: ${id}`);
|
||||||
}
|
}
|
||||||
return getLocaleById(id);
|
return getLocaleById(id);
|
||||||
};
|
};
|
||||||
|
|
||||||
const idInValidRange = (id: number) => id < 1 || id >= localesSize;
|
|
||||||
|
|
|
||||||
|
|
@ -4,4 +4,8 @@
|
||||||
*
|
*
|
||||||
@example * `throw new ValidationError("Bad id")`
|
@example * `throw new ValidationError("Bad id")`
|
||||||
*/
|
*/
|
||||||
|
import { localesSize } from "./repository/locales.ts";
|
||||||
|
|
||||||
export class ValidationError extends Error {}
|
export class ValidationError extends Error {}
|
||||||
|
|
||||||
|
export const idIsValid = (id: number) => id > 0 && id < localesSize;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue