Skip to content

Commit bdce391

Browse files
mattRedBoxroll
authored andcommitted
Make escapeChar and quoteChar mutually exclusive (#104)
* Fixes #102. * Updated profiles. #102 #104
1 parent 4a5ee57 commit bdce391

File tree

11 files changed

+643
-376
lines changed

11 files changed

+643
-376
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,4 @@ jspm_packages
5050
dist/
5151
lib/
5252
package-lock.json
53+
.idea/

src/config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const DEFAULT_DIALECT = {
1313
doubleQuote: true,
1414
lineTerminator: '\r\n',
1515
quoteChar: '"',
16-
escapeChar: '\\',
1716
skipInitialSpace: true,
1817
header: true,
1918
caseSensitiveHeader: false,

src/helpers.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const isPlainObject = require('lodash/isPlainObject')
77
const jsonpointer = require('json-pointer')
88
const {DataPackageError} = require('./errors')
99
const config = require('./config')
10+
const omit = require('lodash/omit')
1011

1112

1213
// Locate descriptor
@@ -173,7 +174,7 @@ function expandResourceDescriptor(descriptor) {
173174
// Dialect
174175
const dialect = descriptor.dialect
175176
if (dialect !== undefined) {
176-
for (const [key, value] of Object.entries(config.DEFAULT_DIALECT)) {
177+
for (const [key, value] of Object.entries(filterDefaultDialect(validateDialect(dialect)))) {
177178
if (!dialect.hasOwnProperty(key)) {
178179
dialect[key] = value
179180
}
@@ -183,9 +184,22 @@ function expandResourceDescriptor(descriptor) {
183184
return descriptor
184185
}
185186

186-
187187
// Miscellaneous
188188

189+
// quoteChar and escapeChar are mutually exclusive: https://frictionlessdata.io/specs/csv-dialect/#specification
190+
function filterDefaultDialect(dialect = {}) {
191+
const defaultDialects = dialect.hasOwnProperty('escapeChar') ? omit(config.DEFAULT_DIALECT, 'quoteChar') : config.DEFAULT_DIALECT
192+
return defaultDialects
193+
}
194+
195+
// quoteChar and escapeChar are mutually exclusive: https://frictionlessdata.io/specs/csv-dialect/#specification
196+
function validateDialect(dialect = {}) {
197+
if (dialect.hasOwnProperty('escapeChar') && dialect.hasOwnProperty('quoteChar')) {
198+
throw new DataPackageError('Resource.table dialect options quoteChar and escapeChar are mutually exclusive.')
199+
}
200+
return dialect
201+
}
202+
189203
function isRemotePath(path) {
190204
// TODO: improve implementation
191205
return path.startsWith('http')
@@ -218,6 +232,7 @@ module.exports = {
218232
dereferenceResourceDescriptor,
219233
expandPackageDescriptor,
220234
expandResourceDescriptor,
235+
validateDialect,
221236
isRemotePath,
222237
isSafePath,
223238
}

src/profiles/data-package.json

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
"title": "Path",
104104
"description": "A fully qualified URL, or a POSIX file path..",
105105
"type": "string",
106+
"pattern": "^(?=^[^./~])(^((?!\\.{2}).)*$).*$",
106107
"examples": [
107108
"{\n \"path\": \"file.csv\"\n}\n",
108109
"{\n \"path\": \"http://example.com/file.csv\"\n}\n"
@@ -189,6 +190,7 @@
189190
"title": "Path",
190191
"description": "A fully qualified URL, or a POSIX file path..",
191192
"type": "string",
193+
"pattern": "^(?=^[^./~])(^((?!\\.{2}).)*$).*$",
192194
"examples": [
193195
"{\n \"path\": \"file.csv\"\n}\n",
194196
"{\n \"path\": \"http://example.com/file.csv\"\n}\n"
@@ -208,7 +210,7 @@
208210
},
209211
"context": "This property is not legally binding and does not guarantee that the package is licensed under the terms defined herein.",
210212
"examples": [
211-
"{\n \"licenses\": [\n {\n \"name\": \"odc-pddl-1.0\",\n \"uri\": \"http://opendatacommons.org/licenses/pddl/\"\n }\n ]\n}\n"
213+
"{\n \"licenses\": [\n {\n \"name\": \"odc-pddl-1.0\",\n \"path\": \"http://opendatacommons.org/licenses/pddl/\",\n \"title\": \"Open Data Commons Public Domain Dedication and License v1.0\"\n }\n ]\n}\n"
212214
]
213215
},
214216
"resources": {
@@ -268,6 +270,7 @@
268270
"title": "Path",
269271
"description": "A fully qualified URL, or a POSIX file path..",
270272
"type": "string",
273+
"pattern": "^(?=^[^./~])(^((?!\\.{2}).)*$).*$",
271274
"examples": [
272275
"{\n \"path\": \"file.csv\"\n}\n",
273276
"{\n \"path\": \"http://example.com/file.csv\"\n}\n"
@@ -281,6 +284,7 @@
281284
"title": "Path",
282285
"description": "A fully qualified URL, or a POSIX file path..",
283286
"type": "string",
287+
"pattern": "^(?=^[^./~])(^((?!\\.{2}).)*$).*$",
284288
"examples": [
285289
"{\n \"path\": \"file.csv\"\n}\n",
286290
"{\n \"path\": \"http://example.com/file.csv\"\n}\n"
@@ -348,7 +352,7 @@
348352
"title": "Sources",
349353
"description": "The raw sources for this resource.",
350354
"type": "array",
351-
"minItems": 1,
355+
"minItems": 0,
352356
"items": {
353357
"title": "Source",
354358
"description": "A source file.",
@@ -369,6 +373,7 @@
369373
"title": "Path",
370374
"description": "A fully qualified URL, or a POSIX file path..",
371375
"type": "string",
376+
"pattern": "^(?=^[^./~])(^((?!\\.{2}).)*$).*$",
372377
"examples": [
373378
"{\n \"path\": \"file.csv\"\n}\n",
374379
"{\n \"path\": \"http://example.com/file.csv\"\n}\n"
@@ -387,7 +392,7 @@
387392
}
388393
},
389394
"examples": [
390-
"{\n \"sources\": [\n {\n \"name\": \"World Bank and OECD\",\n \"uri\": \"http://data.worldbank.org/indicator/NY.GDP.MKTP.CD\"\n }\n ]\n}\n"
395+
"{\n \"sources\": [\n {\n \"title\": \"World Bank and OECD\",\n \"path\": \"http://data.worldbank.org/indicator/NY.GDP.MKTP.CD\"\n }\n ]\n}\n"
391396
]
392397
},
393398
"licenses": {
@@ -414,6 +419,7 @@
414419
"title": "Path",
415420
"description": "A fully qualified URL, or a POSIX file path..",
416421
"type": "string",
422+
"pattern": "^(?=^[^./~])(^((?!\\.{2}).)*$).*$",
417423
"examples": [
418424
"{\n \"path\": \"file.csv\"\n}\n",
419425
"{\n \"path\": \"http://example.com/file.csv\"\n}\n"
@@ -433,7 +439,7 @@
433439
},
434440
"context": "This property is not legally binding and does not guarantee that the package is licensed under the terms defined herein.",
435441
"examples": [
436-
"{\n \"licenses\": [\n {\n \"name\": \"odc-pddl-1.0\",\n \"uri\": \"http://opendatacommons.org/licenses/pddl/\"\n }\n ]\n}\n"
442+
"{\n \"licenses\": [\n {\n \"name\": \"odc-pddl-1.0\",\n \"path\": \"http://opendatacommons.org/licenses/pddl/\",\n \"title\": \"Open Data Commons Public Domain Dedication and License v1.0\"\n }\n ]\n}\n"
437443
]
438444
},
439445
"format": {
@@ -506,7 +512,7 @@
506512
"title": "Sources",
507513
"description": "The raw sources for this resource.",
508514
"type": "array",
509-
"minItems": 1,
515+
"minItems": 0,
510516
"items": {
511517
"title": "Source",
512518
"description": "A source file.",
@@ -527,6 +533,7 @@
527533
"title": "Path",
528534
"description": "A fully qualified URL, or a POSIX file path..",
529535
"type": "string",
536+
"pattern": "^(?=^[^./~])(^((?!\\.{2}).)*$).*$",
530537
"examples": [
531538
"{\n \"path\": \"file.csv\"\n}\n",
532539
"{\n \"path\": \"http://example.com/file.csv\"\n}\n"
@@ -545,7 +552,7 @@
545552
}
546553
},
547554
"examples": [
548-
"{\n \"sources\": [\n {\n \"name\": \"World Bank and OECD\",\n \"uri\": \"http://data.worldbank.org/indicator/NY.GDP.MKTP.CD\"\n }\n ]\n}\n"
555+
"{\n \"sources\": [\n {\n \"title\": \"World Bank and OECD\",\n \"path\": \"http://data.worldbank.org/indicator/NY.GDP.MKTP.CD\"\n }\n ]\n}\n"
549556
]
550557
}
551558
}

src/profiles/data-resource.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"title": "Path",
5151
"description": "A fully qualified URL, or a POSIX file path..",
5252
"type": "string",
53+
"pattern": "^(?=^[^./~])(^((?!\\.{2}).)*$).*$",
5354
"examples": [
5455
"{\n \"path\": \"file.csv\"\n}\n",
5556
"{\n \"path\": \"http://example.com/file.csv\"\n}\n"
@@ -63,6 +64,7 @@
6364
"title": "Path",
6465
"description": "A fully qualified URL, or a POSIX file path..",
6566
"type": "string",
67+
"pattern": "^(?=^[^./~])(^((?!\\.{2}).)*$).*$",
6668
"examples": [
6769
"{\n \"path\": \"file.csv\"\n}\n",
6870
"{\n \"path\": \"http://example.com/file.csv\"\n}\n"
@@ -130,7 +132,7 @@
130132
"title": "Sources",
131133
"description": "The raw sources for this resource.",
132134
"type": "array",
133-
"minItems": 1,
135+
"minItems": 0,
134136
"items": {
135137
"title": "Source",
136138
"description": "A source file.",
@@ -151,6 +153,7 @@
151153
"title": "Path",
152154
"description": "A fully qualified URL, or a POSIX file path..",
153155
"type": "string",
156+
"pattern": "^(?=^[^./~])(^((?!\\.{2}).)*$).*$",
154157
"examples": [
155158
"{\n \"path\": \"file.csv\"\n}\n",
156159
"{\n \"path\": \"http://example.com/file.csv\"\n}\n"
@@ -169,7 +172,7 @@
169172
}
170173
},
171174
"examples": [
172-
"{\n \"sources\": [\n {\n \"name\": \"World Bank and OECD\",\n \"uri\": \"http://data.worldbank.org/indicator/NY.GDP.MKTP.CD\"\n }\n ]\n}\n"
175+
"{\n \"sources\": [\n {\n \"title\": \"World Bank and OECD\",\n \"path\": \"http://data.worldbank.org/indicator/NY.GDP.MKTP.CD\"\n }\n ]\n}\n"
173176
]
174177
},
175178
"licenses": {
@@ -196,6 +199,7 @@
196199
"title": "Path",
197200
"description": "A fully qualified URL, or a POSIX file path..",
198201
"type": "string",
202+
"pattern": "^(?=^[^./~])(^((?!\\.{2}).)*$).*$",
199203
"examples": [
200204
"{\n \"path\": \"file.csv\"\n}\n",
201205
"{\n \"path\": \"http://example.com/file.csv\"\n}\n"
@@ -215,7 +219,7 @@
215219
},
216220
"context": "This property is not legally binding and does not guarantee that the package is licensed under the terms defined herein.",
217221
"examples": [
218-
"{\n \"licenses\": [\n {\n \"name\": \"odc-pddl-1.0\",\n \"uri\": \"http://opendatacommons.org/licenses/pddl/\"\n }\n ]\n}\n"
222+
"{\n \"licenses\": [\n {\n \"name\": \"odc-pddl-1.0\",\n \"path\": \"http://opendatacommons.org/licenses/pddl/\",\n \"title\": \"Open Data Commons Public Domain Dedication and License v1.0\"\n }\n ]\n}\n"
219223
]
220224
},
221225
"format": {

0 commit comments

Comments
 (0)