From ff891cd3033dfbd430651bc14eee7e4b4056c2ae Mon Sep 17 00:00:00 2001 From: Sebastian Seedorf Date: Thu, 28 May 2020 01:44:42 +0200 Subject: [PATCH] Added readme --- README.md | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Validator.ts | 2 +- mod.ts | 4 +-- t.ts | 11 +++++++ 4 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 README.md create mode 100644 t.ts diff --git a/README.md b/README.md new file mode 100644 index 0000000..e9d4998 --- /dev/null +++ b/README.md @@ -0,0 +1,88 @@ +# valideno + +Validate your values and schemas with **valideno**. Convert values to your desired type if needed. All you need is a single function. + +`validate(data, validators, options = {})`; + +## Examples + +```ts +import { + ValidationError, validate, ArraySymbol, + isString, isEmail, fulfillsRegex, + isNumber, isInRange, +} from "./mod.ts"; + +const errorsA: ValidationError[] = await validate(3.14, isNumber()); +console.log(errorsA); +// [] + +const errorsB: ValidationError[] = await validate(3.14, isString()); +console.log(errorsB); +// [ { type: "isString", args: {}, message: "This value has to be a string." } ] + +const errorsC: ValidationError[] = await validate( + "john.doe@example.com", + [isString(), isEmail()], +); +console.log(errorsC); +// [] + +const entity = { + name: "John Doe", + age: 25, + emails: [ + { type: "work", address: "john.doe@example.com" }, + { type: "home", address: "fancy_joe69@rainbow.com" }, + ], +}; +const scheme = { + name: isString(), + age: isInRange({ greaterThanOrEqualTo: 18 }), + emails: { + [ArraySymbol]: { + type: fulfillsRegex({ regex: /^[a-z]+$/ }), + address: isEmail(), + }, + }, +}; +const errorsD: ValidationError[] = await validate(entity, scheme); +console.log(errorsD); +// [] +``` + +## Conversion + +To allow automatic conversion to the desired data type add the `doConversion: true` option. To retreive the converted values, you need to pass a reference to an object to `converted`. If the conversion is not possible (e.g. when passing a `string` to `isBoolean()`) no conversion will take place and the value stays the same. In this case errors are thrown, because the non-converted `string` is not a Boolean. + +The converted data is not in the reference passed to `converted` in the property `output`. This also works for schemes. + +```ts +import { + ValidationError, validate, + isNumber +} from "./mod.ts"; + +const converted: any = {}; +const errors: ValidationError[] = await validate("420", isNumber(), { doConversion: true, converted }); +console.log(errors); +// [] +console.log(converted); +// { output: 420 } +``` + +To disable validation and do "best effort" conversion only, disable validation with `doValidation: false`. + +```ts +import { + ValidationError, validate, + isBoolean +} from "./mod.ts"; + +const converted: any = {}; +const errors: ValidationError[] = await validate("foobar", isBoolean(), { doConversion: true, converted }); +console.log(errors); +// [ { type: "isBoolean", args: {}, message: "This value has to be a boolean." } ] +console.log(converted); +// { output: "foobar" } +``` \ No newline at end of file diff --git a/Validator.ts b/Validator.ts index 109ad97..3f162bd 100644 --- a/Validator.ts +++ b/Validator.ts @@ -15,7 +15,7 @@ export interface ValidationError { type: string; param?: string[]; message?: string | null; - args?: Args; + args: Args; } export type Validatable = Schema | Validator | Validator[]; diff --git a/mod.ts b/mod.ts index 5b0eca1..7d7704d 100644 --- a/mod.ts +++ b/mod.ts @@ -6,8 +6,8 @@ export { Validatable, ArraySymbol, } from "./Validator.ts"; -export { isString, isUrl, isEmail } from "./validators/string.ts"; -export { isNumber, isInteger } from "./validators/number.ts"; +export { isString, isUrl, isEmail, fulfillsRegex } from "./validators/string.ts"; +export { isNumber, isInteger, isInRange, hasParity } from "./validators/number.ts"; export { isArray } from "./validators/array.ts"; export { or } from "./validators/logic.ts"; export { isRequired, isDefined, isNotEmpty } from "./validators/empty.ts"; diff --git a/t.ts b/t.ts new file mode 100644 index 0000000..9525a6c --- /dev/null +++ b/t.ts @@ -0,0 +1,11 @@ +import { + ValidationError, validate, + isBoolean +} from "./mod.ts"; + +const converted: any = {}; +const errors: ValidationError[] = await validate("foobar", isBoolean(), { doConversion: true, converted }); +console.log(errors); +// [ { type: "isBoolean", args: {}, message: "This value has to be a boolean." } ] +console.log(converted); +// { output: "foobar" } \ No newline at end of file