Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 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
6,884 changes: 2,112 additions & 4,772 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"plugins/*",
"support/*",
"utils/*",
"validators/*"
"validate/*"
],
"scripts": {
"clean": "find ./ '(' -name 'node_modules' -o -name 'dist' -o -name '.turbo' -o -name '.parcel-cache' ')' -type d -exec rm -rf {} +",
Expand Down
77 changes: 77 additions & 0 deletions validate/isbn/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<!-- START_INFOCARD -->
# @flatfile/plugin-validate-isbn

The `@flatfile/plugin-validate-isbn` Plugin is a powerful tool for validating ISBN (International Standard Book Number) fields during data import. It offers comprehensive validation for both ISBN-10 and ISBN-13 formats, including structure checks, check digit verification, and optional conversion between formats.

**Event Type:**
`listener.on('commit:created')`

<!-- END_INFOCARD -->

A Flatfile Listener plugin for validating ISBN (International Standard Book Number) fields in your data import process. This plugin provides robust ISBN validation, including format checks, check digit verification, and optional ISBN-10 to ISBN-13 conversion.

## Features

- Validates both ISBN-10 and ISBN-13 formats
- Checks for correct ISBN structure and valid check digits
- Provides specific error messages for different types of validation failures
- Optionally converts ISBN-10 to ISBN-13 automatically
- Optionally formats ISBN hyphenated
- Configurable for multiple ISBN fields within a sheet

## Installation

Install the plugin using npm:

```bash
npm install @flatfile/plugin-validate-isbn
```

## Example Usage

```javascript
import { FlatfileListener } from "@flatfile/listener";
import isbnValidator from "@flatfile/plugin-validate-isbn";

const listener = new FlatfileListener();

listener.use(
isbnValidator({
sheetSlug: "books",
isbnFields: ["isbn", "alternate_isbn"],
format: "isbn13",
autoFormat: true
})
);

// ... rest of your Flatfile configuration
```

## Configuration

The `isbnValidator` function accepts a configuration object with the following properties:

- `sheetSlug` (string): The slug of the sheet where ISBN validation should be applied.
- `isbnFields` (string[]): An array of field names that contain ISBN values to be validated.
- `autoFormat` (boolean): If true, ISBN numbers will be hyphenated.
- `format` (string, optional): Manually set the output format of the ISBN. Can be one of:
- 'isbn13': 13-digit ISBN without hyphens
- 'isbn13h': 13-digit ISBN with hyphens
- 'isbn10': 10-digit ISBN without hyphens
- 'isbn10h': 10-digit ISBN with hyphens
Comment thread
bangarang marked this conversation as resolved.

## Behavior

1. The plugin validates each specified ISBN field in every record of the configured sheet.
2. It removes hyphens and spaces from the ISBN before validation.
3. For ISBN-10:
- Checks if the format is valid (9 digits followed by a digit or 'X').
- Verifies the check digit.
- If `autoConvert` is true, converts valid ISBN-10 to ISBN-13.
4. For ISBN-13:
- Checks if the format is valid (13 digits).
- Verifies the check digit.
5. Adds appropriate error messages to the record for any validation failures.
6. If a field is empty, it adds an error indicating that ISBN is required.

This plugin enhances data quality by ensuring that ISBN fields contain valid and properly formatted book identifiers.
62 changes: 62 additions & 0 deletions validate/isbn/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"name": "@flatfile/plugin-validate-isbn",
"version": "0.0.0",
"description": "A Flatfile Listener plugin for ISBN validation with configurable options. Validates ISBN-10 and ISBN-13 formats, provides specific error messages, and optionally converts ISBN-10 to ISBN-13.",
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"browser": {
"./dist/index.cjs": "./dist/index.browser.cjs",
"./dist/index.mjs": "./dist/index.browser.mjs"
},
"exports": {
"types": "./dist/index.d.ts",
"node": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
},
"browser": {
"require": "./dist/index.browser.cjs",
"import": "./dist/index.browser.mjs"
},
"default": "./dist/index.mjs"
},
"source": "./src/index.ts",
"files": [
"dist/**"
],
"scripts": {
"build": "rollup -c",
"build:watch": "rollup -c --watch",
"build:prod": "NODE_ENV=production rollup -c",
"check": "tsc ./**/*.ts --noEmit --esModuleInterop",
"test": "jest ./**/*.spec.ts --config=../../jest.config.js --runInBand"
},
"keywords": [
"flatfile-plugins",
"category-validate"
],
"author": "Flatfile, Inc..",
"license": "ISC",
"dependencies": {
"@flatfile/plugin-record-hook": "^1.7.0",
"isbn3": "^1.2.0"
},
"peerDependencies": {
"@flatfile/listener": "^1.1.0"
},
"devDependencies": {
"@flatfile/rollup-config": "^0.1.1",
"@flatfile/utils-testing": "^0.3.0"
},
"repository": {
"type": "git",
"url": "https://github.com/FlatFilers/flatfile-plugins.git",
"directory": "validate/isbn"
},
"browserslist": [
"> 0.5%",
"last 2 versions",
"not dead"
]
}
5 changes: 5 additions & 0 deletions validate/isbn/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { buildConfig } from '@flatfile/rollup-config';

const config = buildConfig({});

export default config;
98 changes: 98 additions & 0 deletions validate/isbn/src/index.e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { FlatfileClient } from '@flatfile/api'
import {
createRecords,
deleteSpace,
getRecords,
setupListener,
setupSimpleWorkbook,
setupSpace,
} from '@flatfile/utils-testing'
import { validateISBN } from './index'

const api = new FlatfileClient()

describe('validateISBN e2e', () => {
const listener = setupListener()

let spaceId: string
let sheetId: string
Comment thread
bangarang marked this conversation as resolved.

beforeAll(async () => {
const space = await setupSpace()
spaceId = space.id
const workbook = await setupSimpleWorkbook(space.id, [
{ key: 'isbn', type: 'string' },
])
sheetId = workbook.sheets![0].id
})

afterAll(async () => {
await deleteSpace(spaceId)
})

afterEach(async () => {
listener.reset()
const records = await getRecords(sheetId)
if (records.length > 0) {
const ids = records.map((record) => record.id)
await api.records.delete(sheetId, { ids })
}
})

it('validates and formats ISBNs', async () => {
listener.use(validateISBN({ isbnFields: ['isbn'] }))

await createRecords(sheetId, [
{ isbn: '0306406152' },
{ isbn: '9780306406157' },
{ isbn: 'invalid-isbn' },
])

await listener.waitFor('commit:created')

const records = await getRecords(sheetId)

expect(records[0].values['isbn'].value).toBe('0-306-40615-2')
expect(records[0].values['isbn'].messages[0].message).toBe(
'Formatted ISBN-10'
)

expect(records[1].values['isbn'].value).toBe('978-0-306-40615-7')
expect(records[1].values['isbn'].messages[0].message).toBe(
'Formatted ISBN-13'
)

expect(records[2].values['isbn'].value).toBe('invalid-isbn')
expect(records[2].values['isbn'].messages[0].message).toBe(
'Invalid ISBN format'
)
})

it('converts ISBN-10 to ISBN-13 when format is specified', async () => {
listener.use(validateISBN({ isbnFields: ['isbn'], format: 'isbn13h' }))

await createRecords(sheetId, [{ isbn: '0306406152' }])

await listener.waitFor('commit:created')

const records = await getRecords(sheetId)

expect(records[0].values['isbn'].value).toBe('978-0-306-40615-7')
expect(records[0].values['isbn'].messages[1].message).toBe(
'Converted ISBN-13'
)
})

it('does not auto-format when autoFormat is false', async () => {
listener.use(validateISBN({ isbnFields: ['isbn'], autoFormat: false }))

await createRecords(sheetId, [{ isbn: '0306406152' }])

await listener.waitFor('commit:created')

const records = await getRecords(sheetId)

expect(records[0].values['isbn'].value).toBe('0306406152')
expect(records[0].values['isbn'].messages.length).toBe(0)
})
})
59 changes: 59 additions & 0 deletions validate/isbn/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { FlatfileListener } from '@flatfile/listener'
import { recordHook } from '@flatfile/plugin-record-hook'
import ISBN from 'isbn3'

interface ValidateISBNConfig {
sheetSlug?: string
isbnFields?: string[]
autoFormat?: boolean
format?: 'isbn13' | 'isbn13h' | 'isbn10' | 'isbn10h'
}

export function validateISBN(config: ValidateISBNConfig = {}) {
const {
sheetSlug = '**',
isbnFields = ['isbn'],
autoFormat = true,
format,
} = config

return (listener: FlatfileListener) => {
listener.use(
recordHook(sheetSlug, async (record) => {
for (const field of isbnFields) {
const isbn = record.get(field) as string
if (!isbn) break

Comment thread
bangarang marked this conversation as resolved.
const isbnObj = ISBN.parse(isbn.replace(/[-\s]/g, ''))
if (!isbnObj?.isValid) {
record.addError(field, 'Invalid ISBN format')
continue
}

if (autoFormat) {
const formattedISBN = isbnObj.isIsbn10
? isbnObj.isbn10h
: isbnObj.isbn13h
record.set(field, formattedISBN)
record.addInfo(
field,
`Formatted ISBN-${isbnObj.isIsbn10 ? '10' : '13'}`
)
}

if (format) {
record.set(field, isbnObj[format])
record.addInfo(
field,
`Converted ISBN-${isbnObj.isIsbn10 ? '13' : '10'}`
)
}
Comment thread
bangarang marked this conversation as resolved.
}

return record
})
)
}
}

export default validateISBN