Skip to content

Commit

Permalink
Improves types and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarenaldi committed Aug 18, 2023
1 parent 2595f1d commit a738c9c
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 10 deletions.
89 changes: 84 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,21 @@ pnpm add @lens-protocol/metadata zod
## Usage

### Parsing and validating metadata
Assuming we have 2 JS objects:

```typescript
import { PublicationMetadataSchema } from '@lens-protocol/metadata';

const valid = {
/** example of valid publication metadata **/
/** example of valid metadata **/
};
const invalid = {
/** example of invalid publication metadata **/
/** example of invalid metadata **/
};
```

### Publication metadata

```typescript
import { PublicationMetadataSchema } from '@lens-protocol/metadata';

PublicationMetadataSchema.parse(valid); // => PublicationMetadata
PublicationMetadataSchema.parse(invalid); // => throws ZodError
Expand All @@ -49,6 +53,22 @@ PublicationMetadataSchema.safeParse(invalid);
// => { success: false, error: ZodError }
```

### Profile metadata

```typescript
import { ProfileMetadataSchema } from '@lens-protocol/metadata';

ProfileMetadataSchema.parse(valid); // => ProfileMetadata
ProfileMetadataSchema.parse(invalid); // => throws ZodError

// OR

ProfileMetadataSchema.safeParse(valid);
// => { success: true, data: ProfileMetadata }
ProfileMetadataSchema.safeParse(invalid);
// => { success: false, error: ZodError }
```

### Format validation error

`ZodError` contains all the information needed to inform you about the validation error, but it's not very user friendly. You can use `formatZodError` to get a more readable error message.
Expand Down Expand Up @@ -92,6 +112,65 @@ switch (publicationMetadata.$schema) {
}
```

### Useful types

The package also exports all enums and types that you might need to work with the metadata.

Some examples:

```typescript
import {
// enums
MediaAudioKind,
MediaAudioMimeType,
MediaImageMimeType,
MediaVideoMimeType,
MetadataAttributeType
PublicationMainFocus,
ThreeDFormat,

// main types
ArticleMetadata,
AudioMetadata,
CheckingInMetadata,
EmbedMetadata,
EventMetadata,
ImageMetadata,
LinkMetadata,
LivestreamMetadata,
MintMetadata,
ProfileMetadata,
PublicationMetadata,
SpaceMetadata,
StoryMetadata,
TextOnlyMetadata,
ThreeDMetadata,
TransactionMetadata,
VideoMetadata,

// others
MetadataAttribute,
MediaAudio,
MediaImage,
MediaVideo,
AnyMedia,
GeoLocation,
BooleanAttribute,
DateAttribute,
NumberAttribute,
StringAttribute,
JSONAttribute,

// branded aliases
Locale,
Markdown,
Signature,
URI,
AppId,
Datetime,
} from '@lens-protocol/metadata';
```

## JSON schemas

Importing JSON schema in TypeScript is a simple as:
Expand Down
15 changes: 10 additions & 5 deletions src/MetadataAttribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,47 @@ export enum MetadataAttributeType {
JSON = 'JSON',
}

const BooleanAttributeSchema = z.object({
export const BooleanAttributeSchema = z.object({
type: z.literal(MetadataAttributeType.Boolean),
key: notEmptyString("The attribute's unique identifier."),
value: z
.enum(['true', 'false'])
.describe("A boolean value serialized as string. It's consumer responsibility to parse it."),
});
export type BooleanAttribute = z.infer<typeof BooleanAttributeSchema>;

const DateAttributeSchema = z.object({
export const DateAttributeSchema = z.object({
type: z.literal(MetadataAttributeType.Date),
key: notEmptyString("The attribute's unique identifier."),
value: z
.string()
.datetime()
.describe("A valid ISO 8601 date string. It's consumer responsibility to parse it."),
});
export type DateAttribute = z.infer<typeof DateAttributeSchema>;

const NumberAttributeSchema = z.object({
export const NumberAttributeSchema = z.object({
type: z.literal(MetadataAttributeType.Number),
key: notEmptyString("The attribute's unique identifier."),
value: notEmptyString(
"A valid JS number serialized as string. It's consumer responsibility to parse it.",
),
});
export type NumberAttribute = z.infer<typeof NumberAttributeSchema>;

const StringAttributeSchema = z.object({
export const StringAttributeSchema = z.object({
type: z.literal(MetadataAttributeType.String),
key: notEmptyString("The attribute's unique identifier."),
value: notEmptyString('A string value.'),
});
export type StringAttribute = z.infer<typeof StringAttributeSchema>;

const JSONAttributeSchema = z.object({
export const JSONAttributeSchema = z.object({
type: z.literal(MetadataAttributeType.JSON),
key: notEmptyString("The attribute's unique identifier."),
value: notEmptyString("A valid JSON string. It's consumer responsibility to parse it."),
});
export type JSONAttribute = z.infer<typeof JSONAttributeSchema>;

export const MetadataAttributeSchema = z.discriminatedUnion('type', [
BooleanAttributeSchema,
Expand Down

0 comments on commit a738c9c

Please sign in to comment.