The expected type of the input to the decorated method.
The expected type of the authentication context.
The expected type of the output of the decorated method.
The contract options including requires
, ensures
, and invariants
.
A method decorator function.
class UserService {
@contract<UserCreateInput, User, AuthContext>({
requires: [auth("admin"), validates(UserSchema)],
ensures: [returns(UserSchema)],
invariants: [(input, output) => output.id === input.id],
layer: "business"
})
async createUser(input: UserCreateInput, context?: AuthContext): Promise<User> {
// Business logic to create a user
return { id: "123", name: input.name, email: input.email, roles: ["user"] };
}
}
The main contract decorator. Applies a set of pre-conditions, post-conditions, and invariants to a method.