Skip to content
This repository has been archived by the owner on May 9, 2023. It is now read-only.

theturnout/black-tie

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

black-tie

A formal approach to semantic validation

What is this?

black-tie is a simple, decorator based approach for semantic validation of TypeScript classes. Black-tie uses the Object Constraint Language to perform these validations.

Invariants

Suppose we have a class that looks like this:

class MediaItem {
    name: string;
    mediaType: 'book' | 'movie';
    isbn: string;
}

We want to enforce the rule that any book should have an ISBN number.

@ContextFor(`inv bookHasIsbn: self.mediaType = "book" implies not self.isbn.oclIsUndefined()`)
class MediaItem {
    name: string;
    mediaType: 'book' | 'movie';
    isbn: string;
}

const myFavoriteBook: MediaItem = {
    name: "Adventures of Huckleberry Finn",
    mediaType: "book",
    isbn: "9781101628270"
}

const errors = await validator.validate(myFavoriteBook);
// no errors will be returned

Validating nested objects

If your object contains nested objects and you want the validator to perform their validation too, then you need to use the @ValidateNested() decorator:

export class Post {
    @ValidateNested()
    user: User;
}

Roadmap

  • Support for preconditions and postconditions.
  • Integration with class-validator.

About

A formal approach to semantic validation

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published