Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/amauryd/bodyguard


https://github.com/amauryd/bodyguard

Last synced: 5 days ago
JSON representation

Awesome Lists containing this project

README

        

# BODYGUARD

An aggressive object Proxy protecting your objects.

## What it does

Controls a resource integrity and access with a provided Schema.

## Example

See the *tests* folder for more examples.

```ts
class ArticleResource {
firstName: string
}

class Actor {
name: string = 'amaury'
role: string = 'admin'
}

const alwaysValidSchema: ResourceSchema = {
structure: {
firstName: {
type: 'string'
}
}
validator: {
isFieldValid: () => true
},
authorization: {
authorizer: {
canUpdateField: () => true,
canDelete: () => true,
canAccessField: (resource: T, field: StringKeyOf, value: unknown, actor: unknown) => {
if (actor.role === 'admin') {
return true;
}
return false;
},
canCreate: () => true
}
}
};

const resourceWithAnonymousActor = createResource(
new ArticleResource(),
alwaysValidSchema
);

// throws error because canAccessField = false
console.log(resourceWithAnonymousActor.firstName);

const resourceWithAdminActor = createResource(
new ArticleResource(),
alwaysValidSchema,
new Actor()
);

// does not throw
console.log(resourceWithAdminActor.firstName);
```

## Why ?

A small challenge writing a library in TDD and using Proxy.