Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/witty-books-turn.md
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
Copy link
Contributor

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:

  • What stored constraints are
  • What problem this solves
  • Key benefits or use cases
  • Any notable implementation details or limitations

Example enhancement:

-Runs the stored constraints for an app
+Introduces a new plugin for executing stored constraints in Flatfile apps. This plugin:
+- Allows running predefined data validation rules stored in your Flatfile workspace
+- Automatically applies constraints during data processing
+- Supports both synchronous and asynchronous constraint execution
+- Integrates seamlessly with existing Flatfile listeners
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
Runs the stored constraints for an app
Introduces a new plugin for executing stored constraints in Flatfile apps. This plugin:
- Allows running predefined data validation rules stored in your Flatfile workspace
- Automatically applies constraints during data processing
- Supports both synchronous and asynchronous constraint execution
- Integrates seamlessly with existing Flatfile listeners

90 changes: 80 additions & 10 deletions package-lock.json

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

1 change: 1 addition & 0 deletions plugins/stored-constraints/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @flatfile/plugin-stored-constraints
27 changes: 27 additions & 0 deletions plugins/stored-constraints/README.md
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())
}
```
74 changes: 74 additions & 0 deletions plugins/stored-constraints/package.json
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"
}
}
1 change: 1 addition & 0 deletions plugins/stored-constraints/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './stored.constraint'
61 changes: 61 additions & 0 deletions plugins/stored-constraints/src/stored.constraint.ts
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,
}
})
}

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)
}
}
)
}
)
}
)
}
Loading