Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
dist/
node_modules/
node_modules/
docs/
53 changes: 51 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ validate(custom).then(errors => {
});
```

## Requiring Optional Fields
## Additional Validators
The `@IsRequired()`, `@IsOneOfInstance()`, and `@IsLink()` decorators are available for use in your custom classes. These additional decorators are used to enforce additional validation rules on your custom classes.

### Requiring Optional Fields with `@IsRequired()`
Many fields in the Activity Streams specification are optional, but you may want to make them required your own validation purposes.

Extend the classes you need and then use the `@IsRequired()` decorator for these fields.
Expand All @@ -129,4 +132,50 @@ validate(note); // fails
note.content = "If you can dodge a wrench, you can dodge a ball.";

validate(note); // works
```
```

### Additional Validation for Specific Types with `@IsOneOfInstance()` and `@IsLink()`
The `@IsOneOfInstance()` and `@IsLink()` decorators are used to enforce additional validation rules on your custom classes. The `@IsOneOfInstance()` decorator is used to ensure that a field is an instance of one of the provided classes, while the `@IsLink()` decorator is used to ensure that a field is a valid link or URL.

For example, using the `@IsLink()` decorator can ensure that certain fields are only references to other Activity Streams objects (such as a user) and don't require creation of a new object:

```typescript
import { Note, IsLink } from '@yuforium/activity-streams';

export class CreateNoteDto extends Note {
@IsLink()
public attributedTo;
}
```

## Resolving Links
ActivityStreams frequently uses link references instead for properties instead of explicitly defined data structures. For example, the following Object contains a link reference for its `attributedTo` field:

```json
{
"type": "Note",
"content": "Oh, I don't think I'm a lot dumber than you think that I thought that I thought I was once.",
"attributedTo": "https://yuforium.com/users/white-goodman"
}
```

You can resolve Links to their respective classes using the `resolve()` method on the link.

```typescript
import 'reflect-metadata';
import { ActivityStreams, Link, Note } from '../lib';

// Use the built-in HTTP resolver. You can also define your own resolver by extending the `ActivityStreams.Resolver` class.
ActivityStreams.resolver.setNext(new ActivityStreams.HttpFetchResolver());

const l = new Link('https://yuforium.dev/users/chris');

l.resolve().then(person => console.log(person));
// Person {
// '@context': 'https://www.w3.org/ns/activitystreams',
// type: 'Person',
// id: 'https://yuforium.dev/users/chris'
//}
```

Note that the `resolve()` method returns a Promise, so you will need to use `await` or `.then()` to access the resolved object. Additionally, `resolve()` works on all of this library's built-in classes, not just `Link`, so no type checking is required to use it.
2 changes: 1 addition & 1 deletion examples/composite-classes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ActivityStreams } from '../src';
import { ActivityStreams } from '../lib';
import 'reflect-metadata';

class Duck extends ActivityStreams.object('Duck') {
Expand Down
2 changes: 1 addition & 1 deletion examples/custom-classes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Expose } from 'class-transformer';
import { IsString, validate } from 'class-validator';
import { ActivityStreams } from '../src';
import { ActivityStreams } from '../lib';
import 'reflect-metadata';


Expand Down
14 changes: 14 additions & 0 deletions examples/resolve.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'reflect-metadata';
import { ActivityStreams, Link, Note } from '../lib';

ActivityStreams.resolver.setNext(new ActivityStreams.HttpFetchResolver());

const l = new Link('https://yuforium.dev/users/chris');

l.resolve().then((a) => {
console.log(a);
});

// l.resolve().then(r => {
// console.log('the resolved', r);
// });
4 changes: 2 additions & 2 deletions examples/validation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'reflect-metadata';
import { Note } from '../src';
import { Note } from '../lib';
import { validate } from 'class-validator';

const note = new Note();
Expand All @@ -21,4 +21,4 @@ validateNote(); // the note is valid

note.id = 'invalid, id must be a valid URL';

validateNote(); // the note is invalid
validateNote(); // the note is invalid
1 change: 0 additions & 1 deletion src/activities.ts → lib/activities.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Expose, Transform, Type } from "class-transformer";
import { IsOptional } from "class-validator";
import { ActivityStreams } from "./activity-streams";
import { ASObjectOrLink } from "./interfaces";
Expand Down
Loading