From 218e596ede118e7ee089b096818eba35a5ddfbd8 Mon Sep 17 00:00:00 2001 From: Sebastian Seedorf Date: Tue, 26 May 2020 00:20:44 +0200 Subject: [PATCH] deno fmt --- Validator.test.ts | 21 ++++++++------ Validator.ts | 60 +++++++++++++++++++++++++-------------- mod.ts | 11 +++++-- validators/string.test.ts | 11 ++++--- validators/string.ts | 8 +++--- 5 files changed, 72 insertions(+), 39 deletions(-) diff --git a/Validator.test.ts b/Validator.test.ts index ddd07ae..2ca3f69 100644 --- a/Validator.test.ts +++ b/Validator.test.ts @@ -1,15 +1,20 @@ -import { assertEquals, assertNotEquals } from "https://deno.land/std@0.53.0/testing/asserts.ts"; import { - validate, Validatable, ArraySymbol, - isString + assertEquals, + assertNotEquals, +} from "https://deno.land/std@0.53.0/testing/asserts.ts"; +import { + validate, + Validatable, + ArraySymbol, + isString, } from "./mod.ts"; Deno.test("validate schema (match)", async () => { const values: [any, Validatable][] = [ ["string", isString], ["string", [isString]], - [["arr", "ay"], {[ArraySymbol]: isString}], - [{foo: "bar", lorem: "ipsum"}, {foo: isString, lorem: [isString]}], + [["arr", "ay"], { [ArraySymbol]: isString }], + [{ foo: "bar", lorem: "ipsum" }, { foo: isString, lorem: [isString] }], ]; for (const [value, constraints] of values) { assertEquals([], await validate(value, constraints)); @@ -20,10 +25,10 @@ Deno.test("validate schema (no match)", async () => { const values: [any, Validatable][] = [ [6, isString], [false, [isString]], - [["arr", ["ay"]], {[ArraySymbol]: isString}], - [{foo: {}, lorem: "ipsum"}, {foo: isString, lorem: [isString]}], + [["arr", ["ay"]], { [ArraySymbol]: isString }], + [{ foo: {}, lorem: "ipsum" }, { foo: isString, lorem: [isString] }], ]; for (const [value, constraints] of values) { assertNotEquals([], await validate(value, constraints)); } -}); \ No newline at end of file +}); diff --git a/Validator.ts b/Validator.ts index f8960b9..bb77032 100644 --- a/Validator.ts +++ b/Validator.ts @@ -1,24 +1,32 @@ -export type Args = {[_: string]: any}; +export type Args = { [_: string]: any }; export interface Validator { type: string; - check: (value: any) => Promise|Args|undefined; - message: (value: any, args?: Args) => Promise|string|undefined; + check: (value: any) => Promise | Args | undefined; + message: ( + value: any, + args?: Args, + ) => Promise | string | undefined; } export interface ValidationError { type: string; param?: string[]; - message?: string|null; + message?: string | null; args?: Args; } -export type Validatable = Schema|Validator|Validator[]; +export type Validatable = Schema | Validator | Validator[]; export const ArraySymbol: unique symbol = Symbol("ArraySymbol"); -export type Schema = {[key: string]: Validatable}|{[ArraySymbol]: Validatable}; +export type Schema = { [key: string]: Validatable } | { + [ArraySymbol]: Validatable; +}; -export async function validate(value: any, validators: Validatable): Promise { +export async function validate( + value: any, + validators: Validatable, +): Promise { if (instanceofValidatorArray(validators) || instanceofValidator(validators)) { return validateValue(value, validators); } else { @@ -34,10 +42,13 @@ function instanceofValidator(value: any): value is Validator { } function instanceofValidatorArray(value: any): value is Validator[] { - return Array.isArray(value) && value.every(x => instanceofValidator(x)); + return Array.isArray(value) && value.every((x) => instanceofValidator(x)); } -async function validateValue(value: any, validators: Validator|Validator[]): Promise { +async function validateValue( + value: any, + validators: Validator | Validator[], +): Promise { if (!Array.isArray(validators)) { validators = [validators]; } @@ -49,33 +60,40 @@ async function validateValue(value: any, validators: Validator|Validator[]): Pro result.push({ type: validator.type, args, - message + message, }); } } return result; } -async function validateSchema(value: any, validators: Schema): Promise { +async function validateSchema( + value: any, + validators: Schema, +): Promise { if (validators.hasOwnProperty(ArraySymbol)) { - const v = validators as {[ArraySymbol]: Validatable}; + const v = validators as { [ArraySymbol]: Validatable }; if (Array.isArray(value)) { - const arr = await Promise.all(value.map(val => validate(val, v[ArraySymbol]))); - const errors = arr.flatMap((val, idx) => (val ?? []).map(error => ({ - ...error, - param: [`[${idx}]`, ...(error.param || [])] - }))); + const arr = await Promise.all( + value.map((val) => validate(val, v[ArraySymbol])), + ); + const errors = arr.flatMap((val, idx) => + (val ?? []).map((error) => ({ + ...error, + param: [`[${idx}]`, ...(error.param || [])], + })) + ); return errors; } else { return [{ type: "array", param: ["[]"], message: "Array expected!", - args: {} + args: {}, }]; } } - const v = validators as {[key: string]: Validatable}; + const v = validators as { [key: string]: Validatable }; const valErrors: ValidationError[] = []; for (const prop in validators) { if (!validators.hasOwnProperty(prop)) { @@ -89,9 +107,9 @@ async function validateSchema(value: any, validators: Schema): Promise { const values = [ "", "foo", new String(), - new String("bar") + new String("bar"), ]; for (const value of values) { assertEquals([], await validate(value, isString)); @@ -25,9 +28,9 @@ Deno.test("isString (no match)", async () => { () => {}, function named() {}, new Object(), - Symbol() + Symbol(), ]; for (const value of values) { assertNotEquals([], await validate(value, isString)); } -}); \ No newline at end of file +}); diff --git a/validators/string.ts b/validators/string.ts index 034b986..89cedbe 100644 --- a/validators/string.ts +++ b/validators/string.ts @@ -3,11 +3,11 @@ import { Validator, Args } from "../mod.ts"; export const isString: Validator = { type: "isString", check: (value: any) => { - if (typeof value !== 'string' && !(value instanceof String)) { + if (typeof value !== "string" && !(value instanceof String)) { return {}; } }, message: (value: any, args?: Args) => { - return `The value '${value && value.toString()}' has to be a string.` - } -} \ No newline at end of file + return `The value '${value && value.toString()}' has to be a string.`; + }, +};