# lakmus-michaelmyers

> Fluent validation.

Latest version **0.3.0** (published 2017-12-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install lakmus-michaelmyers
pnpm add lakmus-michaelmyers
yarn add lakmus-michaelmyers
bun add lakmus-michaelmyers
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2017-12-27 |
| First published | 2017-12-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Boris Mitchenko |
| Maintainers | michaelmyers |
| Keywords | JavaScript, TypeScript, Fluent, Validation, Lakmus |

## Links

- npm: https://www.npmjs.com/package/lakmus-michaelmyers
- Repository: https://github.com/bmitchenko/lakmus
- Homepage: https://github.com/bmitchenko/lakmus#readme
- Issues: https://github.com/bmitchenko/lakmus/issues
- npm.io page: https://npm.io/package/lakmus-michaelmyers

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.3.0 (latest) — 2017-12-27
- 0.2.2 — 2017-12-27
- 0.2.1 — 2017-12-26

## README

# Lakmus
Fluent validation in TypeScript.

## Features
  - 20+ built-in validators
  - Refactoring-friendly (generic property selectors)
  - Multilanguage
 
Inspired by .NET [FluentValidation](https://github.com/JeremySkinner/FluentValidation)

## Demo
```typescript
export class Person {
    public id: number;
    public name: string;
    public address: Address;
    public phoneNumbers: PhoneNumber[];
    public hasDiscount: boolean;
    public discountValue: number;
}

export class Address {
    public country: string;
    public city: string;
    public street: string;
    public houseNumber: string;
}

export class PhoneNumber {
    public country: string;
    public code: string;
    public number: string;
}

export class PersonValidator extends Validator<Person> {
    constructor() {
        super();

        // not null & max length;
        this.ruleFor(x => x.name)
            .notNull()
            .maxLength(100);

        // nested objection validation;
        this.ruleFor(x => x.address)
            .notNull()
            .setValidator(new AddressValidator());

        // collection validation;
        this.ruleFor(x => x.phoneNumbers)
            .notNull()
            .setCollectionValidator(new PhoneNumberValidator());

        this.when(x => x.hasDiscount, () => {
            this.ruleFor(x => x.discountValue)
                .notNull();
        });
    }
}

export class AddressValidator extends Validator<Address> {
    constructor() {
        super();

        this.ruleFor(x => x.city)
            .notNull().maxLength(50);

        this.ruleFor(x => x.street)
            .notNull().maxLength(50);
    }
}

export class PhoneNumberValidator extends Validator<PhoneNumber> {
    constructor() {
        super();

        this.ruleFor(x => x.country)
            .notNull().maxLength(3);

        this.ruleFor(x => x.code)
            .notNull().maxLength(5);

        // predicate and custom error message;
        this.ruleFor(x => x.number)
            .notNull().maxLength(7)
            .must((number, phoneNumber) => (phoneNumber.code || "").length + (phoneNumber.country || "").length + (phoneNumber.number || "").length == 11)
            .withMessage("Please enter a valid phone number.");
    }
}

// set custom property name resolver: discountValue -> Discount Value
ValidatorOptions.propertyNameResolver = ApStyleTitleCasePropertyNameResolver;

// initialize object to validate;
var person = new Person();

// create validator;
var validator = new PersonValidator();
// or get it from factory (recommended) 
validator = ValidatorFactory.getValidator(PersonValidator);

// validate;
var result = validator.validate(person);

// process result;
if (result.isValid) {
    console.log("Is valid.");
}
else {
    console.log(result.errors.map(x => x.errorMessage).join("\n"));
}
```

## License
MIT

---
_Source: https://npm.io/package/lakmus-michaelmyers · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
