-
Notifications
You must be signed in to change notification settings - Fork 8
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
validator: string #650
Merged
Merged
validator: string #650
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
04b2152
koala: initial commit
bangarang c650fcf
feat: cleanup and test
bangarang 47b1a5e
feat: cleanup
bangarang f0cc231
feat: Readme updates
bangarang 16f316e
feat: refactor to unit tests
bangarang 3c1207c
feat: update package.json
bangarang 27b8cba
chore: remove metadata.json
bangarang 9addc9c
feat: update test scripts
bangarang 5e0a5a3
feat: install deps
bangarang f7c0303
feat: jest.config.js
bangarang 2d94a9f
feat: jest.config.js
bangarang 6b696af
feat: fix phone test
bangarang ad43001
Apply suggestions from code review
bangarang fcf4a0a
feat: move some logic into utils file
bangarang f2b294c
feat: 0.0.0
bangarang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module.exports = { | ||
testEnvironment: 'node', | ||
|
||
transform: { | ||
'^.+\\.tsx?$': 'ts-jest', | ||
}, | ||
setupFiles: ['../../test/dotenv-config.js'], | ||
setupFilesAfterEnv: [ | ||
'../../test/betterConsoleLog.js', | ||
'../../test/unit.cleanup.js', | ||
], | ||
testTimeout: 60_000, | ||
globalSetup: '../../test/setup-global.js', | ||
forceExit: true, | ||
passWithNoTests: true, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<!-- START_INFOCARD --> | ||
|
||
The `@flatfile/plugin-validate-string` plugin for string configuration and validation. This plugin combines multiple string validations in a single configuration, including regex pattern matching, length validation, case sensitivity, trimming options, and custom transformations. | ||
|
||
**Event Type:** | ||
`listener.on('commit:created')` | ||
|
||
<!-- END_INFOCARD --> | ||
## Features | ||
|
||
- Regular expression pattern matching | ||
- Length validation (min, max, exact) | ||
- Case type enforcement (lowercase, uppercase, titlecase) | ||
- Whitespace trimming (leading, trailing) | ||
- Custom transformation functions | ||
- Configurable error messages | ||
- Common regex patterns for email, phone, and URL validation | ||
- Empty string handling | ||
|
||
## Installation | ||
|
||
To install the plugin, use npm: | ||
|
||
```bash | ||
npm install @flatfile/plugin-validate-string | ||
``` | ||
|
||
## Example Usage | ||
|
||
```typescript | ||
import { FlatfileListener } from '@flatfile/listener'; | ||
import { validateString } from '@flatfile/plugin-validate-string'; | ||
|
||
const listener = new FlatfileListener(); | ||
|
||
const stringConfig = { | ||
fields: ['name'], | ||
minLength: 2, | ||
maxLength: 50, | ||
caseType: 'titlecase', | ||
errorMessages: { | ||
length: 'Name must be between 2 and 50 characters', | ||
case: 'Name must be in Title Case', | ||
}, | ||
|
||
}; | ||
|
||
listener.use(validateString(stringConfig)); | ||
``` | ||
|
||
**Pattern usage:** | ||
```typescript | ||
const config = { | ||
fields: ['email'], | ||
pattern: 'email' // Uses predefined email pattern | ||
}; | ||
|
||
// Or with a custom pattern: | ||
const customConfig = { | ||
fields: ['customField'], | ||
pattern: /^[A-Z]{3}-\d{2}$/ // Custom pattern for format like 'ABC-12' | ||
}; | ||
``` | ||
bangarang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Configuration | ||
|
||
The `validateString` accepts a `StringValidationConfig` object with the following properties: | ||
- `fields`: string[] - Fields to validate | ||
- `sheetSlug`: string - Sheet slug to validate (defaults to '**' all sheets) | ||
- `pattern`: RegExp - A regular expression pattern to match against | ||
- `pattern`: keyof typeof commonRegexPatterns | RegExp - A regular expression pattern to match against. You can use one of the predefined patterns ('email', 'phone', 'url') or provide a custom RegExp. The predefined patterns are: | ||
bangarang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- `email`: Validates email addresses | ||
- `phone`: Validates phone numbers (10-14 digits, optional '+' prefix) | ||
- `url`: Validates URLs (with or without protocol) | ||
- `minLength`: number - Minimum length of the string | ||
- `maxLength`: number - Maximum length of the string | ||
- `exactLength`: number - Exact required length of the string | ||
- `caseType`: 'lowercase' | 'uppercase' | 'titlecase' - Enforces specific case type | ||
- `trim`: { leading?: boolean, trailing?: boolean } - Trims whitespace | ||
- `emptyStringAllowed`: boolean - Whether empty strings are allowed | ||
- `errorMessages`: Object with custom error messages for different validations | ||
|
||
## Behavior | ||
|
||
The plugin processes each record in the Flatfile import, applying the configured validations to the specified fields. If a validation fails, an error is added to the record for that field. If a custom transformation is specified and all validations pass, the transformed value is set for the field. | ||
|
||
The plugin uses the `recordHook` to process individual records, allowing for efficient and flexible validation and transformation of string fields during the import process. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module.exports = { | ||
testEnvironment: 'node', | ||
|
||
transform: { | ||
'^.+\\.tsx?$': 'ts-jest', | ||
}, | ||
setupFiles: ['../../test/dotenv-config.js'], | ||
setupFilesAfterEnv: [ | ||
'../../test/betterConsoleLog.js', | ||
'../../test/unit.cleanup.js', | ||
], | ||
testTimeout: 60_000, | ||
globalSetup: '../../test/setup-global.js', | ||
forceExit: true, | ||
passWithNoTests: true, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
{ | ||
"name": "@flatfile/plugin-validate-string", | ||
"version": "0.0.0", | ||
"description": "A Flatfile plugin for string configuration and validation", | ||
"url": "https://github.com/FlatFilers/flatfile-plugins/tree/main/validate/string", | ||
"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 src/*.spec.ts --detectOpenHandles", | ||
"test:unit": "jest src/*.spec.ts --testPathIgnorePatterns=.*\\.e2e\\.spec\\.ts$ --detectOpenHandles", | ||
"test:e2e": "jest src/*.e2e.spec.ts --detectOpenHandles" | ||
}, | ||
"keywords": [ | ||
"flatfile-plugins", | ||
"category-transform" | ||
], | ||
"author": "Flatfile, Inc", | ||
"license": "ISC", | ||
"dependencies": { | ||
"@flatfile/plugin-record-hook": "^1.7.0" | ||
}, | ||
"peerDependencies": { | ||
"@flatfile/listener": "^1.0.5" | ||
}, | ||
"devDependencies": { | ||
"@flatfile/rollup-config": "^0.1.1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/FlatFilers/flatfile-plugins.git", | ||
"directory": "validate/string" | ||
}, | ||
"browserslist": [ | ||
"> 0.5%", | ||
"last 2 versions", | ||
"not dead" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './validate.string.plugin' | ||
export * from './validate.string.utils' |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.