Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add yup support for validation #9

Merged
merged 5 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ jobs:
node-version: 20.x

- name: Install Dependencies
run: npm install
run: npm install -g pnpm && pnpm install

- name: Create Release Pull Request or Publish to npm
id: changesets
uses: changesets/action@v1
with:
publish: npm run release
publish: pnpm run release
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ Effect Schema, allowing you to use your preferred validation tool.

### Schema Support

| Schema Library | Status | Notes |
| ---------------------------------------------------- | ------------------- | ------------------------------------------------------------------ |
| [Zod](https://github.com/colinhacks/zod) | ✅ Supported | Default schema tool for `xscrape` |
| [Effect/Schema](https://github.com/Effect-TS/effect) | ✅ Supported | Support for Effect/Schema for additional flexibility |
| [Joi](https://github.com/sideway/joi) | ✅ Supported | Support for Joi for those familiar with server-side validation |
| [Yup](https://github.com/jquense/yup) | 🚧 Planned | Adding Yup support for schema validation in front-end applications |
| Others... | 🔄 In Consideration | Potential support for other schema tools as per user feedback |
| Schema Library | Status | Notes |
| ---------------------------------------------------- | ------------------- | ------------------------------------------------------------- |
| [Zod](https://github.com/colinhacks/zod) | ✅ Supported | Default schema tool for `xscrape` |
| [Effect/Schema](https://github.com/Effect-TS/effect) | ✅ Supported | Support for Effect/Schema for additional flexibility |
| [Joi](https://github.com/sideway/joi) | ✅ Supported | Support for Joi for validation |
| [Yup](https://github.com/jquense/yup) | ✅ Supported | Support for Yup for validation |
| Others... | 🔄 In Consideration | Potential support for other schema tools as per user feedback |

## Installation

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"cheerio": "^1.0.0",
"effect": "^3.10.4",
"joi": "^17.13.3",
"yup": "^1.4.0",
"zod": "^3.23.8"
}
}
34 changes: 34 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

187 changes: 187 additions & 0 deletions src/createScraper.yup.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
import { createScraper } from '@/createScraper.js';
import { describe, test, expect } from 'vitest';
import * as yup from 'yup';
import { YupValidator } from '@/validators/yup.js';
import { type SchemaFieldDefinitions } from '@/types.js';

const schema = yup.object({
title: yup.string().default('No title'),
description: yup.string().required(),
keywords: yup.array().of(yup.string()).default([]),
views: yup.number().default(0),
});

const schemaWithNested = yup.object({
title: yup.string().default('No title nested'),
image: yup
.object({
url: yup.string().required(),
width: yup.number().required(),
height: yup.number().required(),
})
.default(() => ({ url: '', width: 0, height: 0 }))
.optional(),
});

type FieldDefinitions = SchemaFieldDefinitions<yup.InferType<typeof schema>>;
type NestedFieldDefinitions = SchemaFieldDefinitions<
yup.InferType<typeof schemaWithNested>
>;

const fields: FieldDefinitions = {
title: {
selector: 'title',
},
description: {
selector: 'meta[name="description"]',
attribute: 'content',
defaultValue: 'No description',
},
keywords: {
selector: 'meta[name="keywords"]',
attribute: 'content',
transform: (value) => value.split(','),
defaultValue: [],
},
views: {
selector: 'meta[name="views"]',
attribute: 'content',
transform: (value) => parseInt(value, 10),
defaultValue: 0,
},
};

const nestedFields: NestedFieldDefinitions = {
title: {
selector: 'title',
},
image: {
fields: {
url: {
selector: 'meta[property="og:image"]',
attribute: 'content',
},
width: {
selector: 'meta[property="og:image:width"]',
attribute: 'content',
transform: (value) => parseInt(value, 10),
},
height: {
selector: 'meta[property="og:image:height"]',
attribute: 'content',
transform: (value) => parseInt(value, 10),
},
},
},
};

const html = `
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="An example description.">
<meta name="keywords" content="typescript,html,parsing">
<meta name="views" content="1234">
<title>Example Title</title>
</head>
<body></body>
</html>
`;

const htmlWithNested = `
<!DOCTYPE html>
<html>
<head>
<title>Example Title</title>
<meta property="og:image" content="https://example.se/images/c12ffe73-3227-4a4a-b8ad-a3003cdf1d70?h=708&amp;tight=false&amp;w=1372">
<meta property="og:image:width" content="1372">
<meta property="og:image:height" content="708">
</head>
<body></body>
</html>
`;

describe('xscrape with Yup', () => {
test('extracts data from HTML', () => {
const validator = new YupValidator(schema);
const scraper = createScraper({
fields,
validator,
});
const data = scraper(html);

expect(data).toEqual({
title: 'Example Title',
description: 'An example description.',
keywords: ['typescript', 'html', 'parsing'],
views: 1234,
});
});

test('handles missing data', () => {
const validator = new YupValidator(schema);
const scraper = createScraper({
fields,
validator,
});
const data = scraper('<html><head></head><body></body></html>');

expect(data).toEqual({
title: 'No title',
description: 'No description',
keywords: [],
views: 0,
});
});

test('handles multiple values', () => {
const validator = new YupValidator(schema);
const scraper = createScraper({
fields,
validator,
});
const data = scraper(
'<html><head><meta name="keywords" content="typescript,html,parsing"></head><body></body></html>',
);

expect(data).toEqual({
title: 'No title',
description: 'No description',
keywords: ['typescript', 'html', 'parsing'],
views: 0,
});
});

test('handles invalid data', () => {
const validator = new YupValidator(schema);
const scraper = createScraper({
fields,
validator,
});
try {
scraper(
'<html><head><meta name="keywords" content="invalid"></head><body></body></html>',
);
} catch (error) {
expect(error).toBeInstanceOf(Error);
}
});

test('extracts nested data from HTML', () => {
const validator = new YupValidator(schemaWithNested);
const scraper = createScraper({
fields: nestedFields,
validator,
});
const data = scraper(htmlWithNested);

expect(data).toEqual({
title: 'Example Title',
image: {
url: 'https://example.se/images/c12ffe73-3227-4a4a-b8ad-a3003cdf1d70?h=708&tight=false&w=1372',
width: 1372,
height: 708,
},
});
});
});
1 change: 1 addition & 0 deletions src/validators/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from '@/validators/effect.js';
export * from '@/validators/zod.js';
export * from '@/validators/joi.js';
export * from '@/validators/yup.js';
25 changes: 25 additions & 0 deletions src/validators/yup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { type SchemaValidator } from '@/types.js';
import * as yup from 'yup';

export class YupValidator<T> implements SchemaValidator<T> {
constructor(private schema: yup.Schema<T>) {}

validate(data: unknown): T {
try {
return this.schema.validateSync(data, {
stripUnknown: true,
strict: false,
abortEarly: false,
});
} catch (error) {
if (error instanceof yup.ValidationError) {
throw new Error(this.formatError(error));
}
throw error;
}
}

private formatError(error: yup.ValidationError): string {
return error.errors.join('\n');
}
}