Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/amauryd/bodyguard
https://github.com/amauryd/bodyguard
Last synced: 5 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/amauryd/bodyguard
- Owner: AmauryD
- Created: 2023-02-12T09:51:08.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-02-12T09:54:30.000Z (almost 2 years ago)
- Last Synced: 2024-12-16T07:55:35.181Z (7 days ago)
- Language: TypeScript
- Size: 24.4 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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.