-
Notifications
You must be signed in to change notification settings - Fork 11
Stored constraint plugin #700
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3bd4591
Init stored constraint plugin
meritmalling 100e2b5
Fix api usage
meritmalling 33b7a96
Prettier
meritmalling 2e43c07
jest -> vitest & README update
carlbrugger 67a18fb
Mostly types & replace lodash w/ vanilla JS flatMap
carlbrugger a37d295
Changeset
meritmalling 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
This file contains hidden or 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 @@ | ||
| --- | ||
| '@flatfile/plugin-stored-constraints': patch | ||
| --- | ||
|
|
||
| Runs the stored constraints for an app | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 @@ | ||
| # @flatfile/plugin-stored-constraints |
This file contains hidden or 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,27 @@ | ||
| <!-- START_INFOCARD --> | ||
|
|
||
| # @flatfile/plugin-stored-constraints | ||
|
|
||
| The `@flatfile/plugin-stored-constraints` plugin enables running stored constraints. | ||
|
|
||
| **Event Type:** | ||
| `listener.on('commit:created')` | ||
|
|
||
| <!-- END_INFOCARD --> | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash install | ||
| npm i @flatfile/plugin-stored-constraints | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ```ts listener.ts | ||
| import type { FlatfileListener } from "@flatfile/listener"; | ||
| import { storedConstraint } from '@flatfile/plugin-stored-constraints' | ||
|
|
||
| export default function (listener: FlatfileListener) { | ||
| listener.use(storedConstraint()) | ||
| } | ||
| ``` |
This file contains hidden or 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,74 @@ | ||
| { | ||
| "name": "@flatfile/plugin-stored-constraints", | ||
| "version": "0.0.0", | ||
| "url": "https://github.com/FlatFilers/flatfile-plugins/tree/main/plugins/stored-constraints", | ||
| "description": "A plugin for running stored constraints", | ||
| "type": "module", | ||
| "engines": { | ||
| "node": ">= 18" | ||
| }, | ||
| "registryMetadata": { | ||
| "category": "records" | ||
| }, | ||
| "exports": { | ||
| ".": { | ||
| "node": { | ||
| "types": { | ||
| "import": "./dist/index.d.ts", | ||
| "require": "./dist/index.d.cts" | ||
| }, | ||
| "import": "./dist/index.js", | ||
| "require": "./dist/index.cjs" | ||
| }, | ||
| "browser": { | ||
| "types": { | ||
| "import": "./dist/index.d.ts", | ||
| "require": "./dist/index.d.cts" | ||
| }, | ||
| "import": "./dist/index.browser.js", | ||
| "require": "./dist/index.browser.cjs" | ||
| }, | ||
| "default": "./dist/index.js" | ||
| }, | ||
| "./package.json": "./package.json" | ||
| }, | ||
| "main": "./dist/index.cjs", | ||
| "module": "./dist/index.js", | ||
| "source": "./src/index.ts", | ||
| "types": "./dist/index.d.ts", | ||
| "files": [ | ||
| "dist/**" | ||
| ], | ||
| "scripts": { | ||
| "build": "tsup", | ||
| "build:watch": "tsup --watch", | ||
| "build:prod": "NODE_ENV=production tsup", | ||
| "checks": "tsc --noEmit && attw --pack . && publint .", | ||
| "lint": "tsc --noEmit", | ||
| "test": "vitest run --mode defaults src/*.spec.ts --passWithNoTests", | ||
| "test:unit": "vitest run --mode defaults src/*.spec.ts --passWithNoTests --exclude src/*.e2e.spec.ts", | ||
| "test:e2e": "vitest run --mode defaults src/*.e2e.spec.ts --passWithNoTests --no-file-parallelism" | ||
| }, | ||
| "keywords": [], | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/FlatFilers/flatfile-plugins.git", | ||
| "directory": "plugins/stored-constraints" | ||
| }, | ||
| "license": "ISC", | ||
| "dependencies": { | ||
| "country-state-city": "^3.2.1", | ||
| "luxon": "^3.5.0", | ||
| "validator": "^13.12.0" | ||
| }, | ||
| "peerDependencies": { | ||
| "@flatfile/api": "^1.11.0", | ||
| "@flatfile/listener": "^1.1.0", | ||
| "@flatfile/plugin-record-hook": "^1.10.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@flatfile/plugin-record-hook": "^1.10.0", | ||
| "@flatfile/bundler-config-tsup": "^0.2.0", | ||
| "@flatfile/config-vitest": "^0.0.0" | ||
| } | ||
| } |
This file contains hidden or 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 @@ | ||
| export * from './stored.constraint' |
This file contains hidden or 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,61 @@ | ||
| import type { Flatfile } from '@flatfile/api' | ||
| import type { FlatfileRecord } from '@flatfile/hooks' | ||
| import type { FlatfileEvent } from '@flatfile/listener' | ||
| import { bulkRecordHook } from '@flatfile/plugin-record-hook' | ||
| import * as countryStateCity from 'country-state-city' | ||
| import { DateTime } from 'luxon' | ||
| import validator from 'validator' | ||
| import { | ||
| applyConstraintToRecord, | ||
| crossEach, | ||
| getAppConstraints, | ||
| getFields, | ||
| getSheet, | ||
| getStoredConstraints, | ||
| getValidator, | ||
| hasStoredConstraints, | ||
| } from './utils' | ||
|
|
||
| const deps = { validator, countryStateCity, luxon: DateTime } | ||
|
|
||
| export interface Constraint { | ||
| validator: string | ||
| function: string | ||
| type?: string | ||
| } | ||
|
|
||
| async function getValidators(event: FlatfileEvent): Promise<Constraint[]> { | ||
| const constraints = await getAppConstraints(event.context.appId) | ||
| return constraints.data.map((c: Flatfile.ConstraintResource) => { | ||
| return { | ||
| validator: c.validator, | ||
| function: c.function, | ||
| } | ||
| }) | ||
| } | ||
carlbrugger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| export function storedConstraint() { | ||
| return bulkRecordHook( | ||
| '**', | ||
| async (records: FlatfileRecord[], event: FlatfileEvent) => { | ||
| const sheet: Flatfile.SheetResponse = await getSheet(event) | ||
| const storedConstraintFields = | ||
| getFields(sheet).filter(hasStoredConstraints) | ||
| const validators = await getValidators(event) | ||
|
|
||
| crossEach( | ||
| [records, storedConstraintFields], | ||
| (record: FlatfileRecord, field: Flatfile.Property) => { | ||
| getStoredConstraints(field.constraints).forEach( | ||
| async ({ validator }: { validator: string }) => { | ||
| const constraint = await getValidator(validators, validator) | ||
| if (constraint) { | ||
| applyConstraintToRecord(constraint, record, field, deps, sheet) | ||
| } | ||
| } | ||
| ) | ||
| } | ||
| ) | ||
| } | ||
| ) | ||
| } | ||
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.
🛠️ Refactor suggestion
Enhance the changeset description
The current description is too brief and doesn't provide enough context for changelog readers. Consider expanding it to include:
Example enhancement:
📝 Committable suggestion