Skip to content

Commit 6ee8ef9

Browse files
committed
feat: add custom validator callbacks
1 parent 1ae7e94 commit 6ee8ef9

File tree

5 files changed

+166
-79
lines changed

5 files changed

+166
-79
lines changed

.tool-versions

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
nodejs 4.8.0
1+
nodejs 10.16.0

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flatfile-csv-importer",
3-
"version": "0.1.16",
3+
"version": "0.1.17",
44
"description": "A simple adapter for elegantly importing CSV files via flatfile.io (Typescript, ES6, Browser)",
55
"main": "build/main/index.js",
66
"typings": "build/main/index.d.ts",
@@ -33,6 +33,7 @@
3333
"send-coverage": "nyc report --reporter=lcov > coverage.lcov && codecov",
3434
"docs": "yarn docs:html && opn build/docs/index.html",
3535
"docs:html": "typedoc src/index.ts --excludePrivate --mode file --theme default --out build/docs --includeDeclarations --excludeExternals",
36+
"docs:md": "typedoc src --readme none --theme markdown --excludePrivate --mode file --out ../Developers/docs/sdk --includeDeclarations --excludeExternals --platform docusaurus",
3637
"docs:json": "typedoc --mode file --json build/docs/typedoc.json src/index.ts",
3738
"docs:publish": "yarn docs:html && gh-pages -d build/docs",
3839
"changelog": "standard-version",
@@ -81,7 +82,8 @@
8182
"trash-cli": "^1.4.0",
8283
"tslint": "^5.4.3",
8384
"tslint-config-standard": "^6.0.1",
84-
"typedoc": "^0.8.0",
85+
"typedoc": "^0.15.0",
86+
"typedoc-plugin-markdown": "^2.1.0",
8587
"typescript": "^2.4.1",
8688
"uglify-js": "^3.3.4"
8789
},

src/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Meta from './obj.meta'
1010
import RecordObject from './obj.record'
1111
import CustomerObject from './obj.customer'
1212
import LoadOptionsObject from './obj.load-options'
13+
import IValidationResponse from './obj.validation-response'
1314

1415
export default class FlatfileImporter extends EventEmitter {
1516

@@ -30,6 +31,7 @@ export default class FlatfileImporter extends EventEmitter {
3031

3132
private $resolver: (data: any) => any
3233
private $rejecter: (err: any) => any
34+
private $validatorCallback?: (row: {[key: string]: string | number}) => Array<IValidationResponse> | Promise<Array<IValidationResponse>>
3335

3436
constructor (apiKey: string, options: object, customer?: CustomerObject) {
3537
super()
@@ -175,6 +177,16 @@ export default class FlatfileImporter extends EventEmitter {
175177
})
176178
}
177179

180+
/**
181+
* Set the customer information for this import
182+
*/
183+
registerValidatorCallback (callback: FlatfileImporter['$validatorCallback']): void {
184+
this.$validatorCallback = callback
185+
this.$ready.then((child) => {
186+
child.parentHasValidator()
187+
})
188+
}
189+
178190
/**
179191
* Call close() from the parent window in order to hide the importer. You can do this after
180192
* handling the import callback so your users don't have to click the confirmation button
@@ -230,9 +242,15 @@ export default class FlatfileImporter extends EventEmitter {
230242
this.emit('close')
231243
this.handleClose()
232244
},
245+
validatorCallback: (row) => {
246+
return this.$validatorCallback ? this.$validatorCallback(row) : undefined
247+
},
233248
ready: () => {
234249
this.handshake.promise.then((child) => {
235250
this.$resolver(child)
251+
if (this.customer) {
252+
child.setUser(this.customer)
253+
}
236254
}).catch((err) => {
237255
console.error(err)
238256
})

src/obj.validation-response.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export default interface IValidationResponse {
2+
/**
3+
* A string referencing the key affected by the problem
4+
*/
5+
key: string
6+
7+
/**
8+
* The validation error message
9+
*/
10+
message: string
11+
12+
/**
13+
* The type of validation response - currently only 'error' is supported
14+
*/
15+
level?: 'error'
16+
}

0 commit comments

Comments
 (0)