0.1.9 • Published 4 years ago

@codeallnight/falidator v0.1.9

Weekly downloads
2
License
ISC
Repository
github
Last release
4 years ago

CircleCI

Falidator

Make running validations against a JavaScript value fun again, because:

  • No if and no dependencies to run
  • Any exception thrown by validation function, is quickly handled as Invalid
  • Similarly for multiple async validations, any rejection is accumulated as Invalid
  • Typed with typescript, complete with type guards and other useful type aliases

JavaScript sample

const above18 = (person) => 
    person.age > 18 ? person : { errorMessage: "not above 18" };

const john = { age: 24 }
falidator.runValidations([above18], john) // returns  { age: 24 }

const jane = { age: 18 }
falidator.runValidations([above18], jane) // returns [{ errorMessage: "not above 18" }]

TypeScript sample - synchronous

interface Person { age: number; };

const above18: Validate<Person> = (person) => 
    person.age > 18 ? person : { errorMessage: "Not above 18" };

const john = { age: 24 };
runValidations([above18], john); // returns  { age: 24 }

const jane = { age: 18 };
runValidations([above18], jane); // returns [{ errorMessage: "not above 18" }]

Type Script sample - asynchronous

const isAllowed = async (person: Person): Promise<InvalidOr<Person>> => {
    const allowed = (person.name === 'Banned') ? new Invalid("Must not be in the banned list") : person;
    return allowed;
};

const nonEmptyName: EventuallyValidate<Person> = async (person: Person) => {
    return  (person.name !== "") ? person : new Invalid('Name cannot be empty')
};

const jack = { name: "Jack" };
runAsyncValidations([isAllowed, nonEmptyName], jack); // returns { name: Jack }

const banned = { name: "Banned" };
runAsyncValidations([isAllowed, nonEmptyName], banned) // returns [{errorMessage: "Must not be in the banned list" }]
0.1.9

4 years ago

0.1.7

4 years ago

0.1.4

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.3

4 years ago

0.1.0

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago