@natlibfi/marc-record-validators-melinda 10.15.6-alpha.1 → 10.16.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = _default;
7
+ // Fix country codes in 008/15-17
8
+ //
9
+ // This is proto. Extend later...
10
+ //
11
+ // Author(s): Nicholas Volk
12
+
13
+ //import createDebugLogger from 'debug';
14
+ //import {fieldToString, nvdebug} from './utils';
15
+
16
+ function _default() {
17
+ return {
18
+ description: 'Fix deprecated country codes',
19
+ validate,
20
+ fix
21
+ };
22
+ function fix(record) {
23
+ //nvdebug(`FIX ME`);
24
+ const res = {
25
+ message: [],
26
+ fix: [],
27
+ valid: true
28
+ };
29
+ const [field008] = record.get('008');
30
+ if (!field008) {
31
+ return res;
32
+ }
33
+ fixCountryCode(field008);
34
+ return res;
35
+ }
36
+ function validate(record) {
37
+ const res = {
38
+ message: [],
39
+ valid: true
40
+ };
41
+ const [field008] = record.get('008');
42
+ if (!field008) {
43
+ return res;
44
+ }
45
+ const originalCountryCode = getCountryCodeFromField008(field008);
46
+ const modifiedCountryCode = deprecatedCountryCodeToCurrentCountryCode(originalCountryCode);
47
+ if (originalCountryCode === modifiedCountryCode) {
48
+ return res;
49
+ }
50
+ res.message.push(`Modify 008/15-17: '${originalCountryCode}' => '${modifiedCountryCode}'`); // eslint-disable-line functional/immutable-data
51
+
52
+ res.valid = false; // eslint-disable-line functional/immutable-data
53
+ return res;
54
+ }
55
+ function fixCountryCode(field008) {
56
+ const originalCountryCode = getCountryCodeFromField008(field008);
57
+ const modifiedCountryCode = deprecatedCountryCodeToCurrentCountryCode(originalCountryCode);
58
+ if (originalCountryCode !== modifiedCountryCode && modifiedCountryCode.length === 3) {
59
+ field008.value = `${field008.value.substring(0, 15)}${modifiedCountryCode}${field008.value.substring(18)}`; // eslint-disable-line functional/immutable-data
60
+ return;
61
+ }
62
+ }
63
+ function getCountryCodeFromField008(field) {
64
+ return field.value.substring(15, 18); // return 008/15-17
65
+ }
66
+ function deprecatedCountryCodeToCurrentCountryCode(countryCode) {
67
+ if (countryCode === 'air') {
68
+ // Armenia
69
+ return 'ai ';
70
+ }
71
+ if (countryCode === 'ajr') {
72
+ // Azerbaidzan
73
+ return 'aj ';
74
+ }
75
+ // Australia
76
+ if (['aca', 'qua', 'tma', 'vra', 'wea', 'xga', 'xna', 'xoa', 'xra'].includes(countryCode)) {
77
+ return 'at ';
78
+ }
79
+
80
+ // Canada
81
+ if (['abc', 'bcc', 'cn ', 'mbc', 'nfc', 'nkc', 'nsc', 'ntc', 'nuc', 'onc', 'pic', 'quc', 'snc', 'ykc'].includes(countryCode)) {
82
+ return 'xxc';
83
+ }
84
+ // Great Britain:
85
+ if (['enk', 'nik', 'stk', 'uik', 'uk ', 'wlk'].includes(countryCode)) {
86
+ return 'xxk';
87
+ }
88
+ // United States:
89
+ if (['aku', 'alu', 'aru', 'azu', 'cau', 'cou', 'ctu', 'dcu', 'deu', 'flu', 'gau', 'hiu', 'iau', 'idu', 'ilu', 'inu', 'kgr', 'ksu', 'kyu', 'lau', 'mau', 'mdu', 'meu', 'miu', 'mnu', 'mou', 'msu', 'mtu', 'nbu', 'ncu', 'nhu', 'nju', 'nmu', 'nvu', 'nyu', 'ohu', 'oku', 'oru', 'pau', 'riu', 'scu', 'sdu', 'tnu', 'txu', 'us ', 'utu', 'vau', 'vtu', 'wau', 'wiu', 'wvu', 'wyu'].includes(countryCode)) {
90
+ return 'xxu';
91
+ }
92
+ return countryCode;
93
+ }
94
+ }
95
+ //# sourceMappingURL=fix-country-codes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fix-country-codes.js","names":["_default","description","validate","fix","record","res","message","valid","field008","get","fixCountryCode","originalCountryCode","getCountryCodeFromField008","modifiedCountryCode","deprecatedCountryCodeToCurrentCountryCode","push","length","value","substring","field","countryCode","includes"],"sources":["../src/fix-country-codes.js"],"sourcesContent":["// Fix country codes in 008/15-17\n//\n// This is proto. Extend later...\n//\n// Author(s): Nicholas Volk\n\n//import createDebugLogger from 'debug';\n//import {fieldToString, nvdebug} from './utils';\n\n\nexport default function () {\n\n return {\n description: 'Fix deprecated country codes',\n validate, fix\n };\n\n function fix(record) {\n //nvdebug(`FIX ME`);\n const res = {message: [], fix: [], valid: true};\n\n const [field008] = record.get('008');\n if (!field008) {\n return res;\n }\n\n fixCountryCode(field008);\n\n return res;\n }\n\n function validate(record) {\n const res = {message: [], valid: true};\n\n const [field008] = record.get('008');\n\n if (!field008) {\n return res;\n }\n\n const originalCountryCode = getCountryCodeFromField008(field008);\n const modifiedCountryCode = deprecatedCountryCodeToCurrentCountryCode(originalCountryCode);\n if (originalCountryCode === modifiedCountryCode) {\n return res;\n }\n\n res.message.push(`Modify 008/15-17: '${originalCountryCode}' => '${modifiedCountryCode}'`); // eslint-disable-line functional/immutable-data\n\n res.valid = false; // eslint-disable-line functional/immutable-data\n return res;\n }\n\n function fixCountryCode(field008) {\n const originalCountryCode = getCountryCodeFromField008(field008);\n const modifiedCountryCode = deprecatedCountryCodeToCurrentCountryCode(originalCountryCode);\n if (originalCountryCode !== modifiedCountryCode && modifiedCountryCode.length === 3) {\n field008.value = `${field008.value.substring(0, 15)}${modifiedCountryCode}${field008.value.substring(18)}`; // eslint-disable-line functional/immutable-data\n return;\n }\n }\n\n function getCountryCodeFromField008(field) {\n return field.value.substring(15, 18); // return 008/15-17\n }\n\n function deprecatedCountryCodeToCurrentCountryCode(countryCode) {\n if (countryCode === 'air') { // Armenia\n return 'ai ';\n }\n if (countryCode === 'ajr') { // Azerbaidzan\n return 'aj ';\n }\n // Australia\n if (['aca', 'qua', 'tma', 'vra', 'wea', 'xga', 'xna', 'xoa', 'xra'].includes(countryCode)) {\n return 'at ';\n }\n\n // Canada\n if (['abc', 'bcc', 'cn ', 'mbc', 'nfc', 'nkc', 'nsc', 'ntc', 'nuc', 'onc', 'pic', 'quc', 'snc', 'ykc'].includes(countryCode)) {\n return 'xxc';\n }\n // Great Britain:\n if (['enk', 'nik', 'stk', 'uik', 'uk ', 'wlk'].includes(countryCode)) {\n return 'xxk';\n }\n // United States:\n if (['aku', 'alu', 'aru', 'azu', 'cau', 'cou', 'ctu', 'dcu', 'deu', 'flu', 'gau', 'hiu', 'iau', 'idu', 'ilu', 'inu', 'kgr', 'ksu', 'kyu', 'lau', 'mau', 'mdu', 'meu', 'miu', 'mnu', 'mou', 'msu', 'mtu', 'nbu', 'ncu', 'nhu', 'nju', 'nmu', 'nvu', 'nyu', 'ohu', 'oku', 'oru', 'pau', 'riu', 'scu', 'sdu', 'tnu', 'txu', 'us ', 'utu', 'vau', 'vtu', 'wau', 'wiu', 'wvu', 'wyu'].includes(countryCode)) {\n return 'xxu';\n }\n return countryCode;\n }\n\n}\n"],"mappings":";;;;;;AAAA;AACA;AACA;AACA;AACA;;AAEA;AACA;;AAGe,SAAAA,SAAA,EAAY;EAEzB,OAAO;IACLC,WAAW,EAAE,8BAA8B;IAC3CC,QAAQ;IAAEC;EACZ,CAAC;EAED,SAASA,GAAGA,CAACC,MAAM,EAAE;IACnB;IACA,MAAMC,GAAG,GAAG;MAACC,OAAO,EAAE,EAAE;MAAEH,GAAG,EAAE,EAAE;MAAEI,KAAK,EAAE;IAAI,CAAC;IAE/C,MAAM,CAACC,QAAQ,CAAC,GAAGJ,MAAM,CAACK,GAAG,CAAC,KAAK,CAAC;IACpC,IAAI,CAACD,QAAQ,EAAE;MACb,OAAOH,GAAG;IACZ;IAEAK,cAAc,CAACF,QAAQ,CAAC;IAExB,OAAOH,GAAG;EACZ;EAEA,SAASH,QAAQA,CAACE,MAAM,EAAE;IACxB,MAAMC,GAAG,GAAG;MAACC,OAAO,EAAE,EAAE;MAAEC,KAAK,EAAE;IAAI,CAAC;IAEtC,MAAM,CAACC,QAAQ,CAAC,GAAGJ,MAAM,CAACK,GAAG,CAAC,KAAK,CA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Y,OAAO,KAAK;IACd;IACA,OAAOA,WAAW;EACpB;AAEF"}
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+
3
+ var _chai = require("chai");
4
+ var _marcRecord = require("@natlibfi/marc-record");
5
+ var _fixCountryCodes = _interopRequireDefault(require("./fix-country-codes"));
6
+ var _fixura = require("@natlibfi/fixura");
7
+ var _fixugen = _interopRequireDefault(require("@natlibfi/fixugen"));
8
+ var _debug = _interopRequireDefault(require("debug"));
9
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
+ (0, _fixugen.default)({
11
+ callback,
12
+ path: [__dirname, '..', 'test-fixtures', 'fix-country-codes'],
13
+ useMetadataFile: true,
14
+ recurse: false,
15
+ fixura: {
16
+ reader: _fixura.READERS.JSON
17
+ },
18
+ mocha: {
19
+ before: () => testValidatorFactory()
20
+ }
21
+ });
22
+ const debug = (0, _debug.default)('@natlibfi/marc-record-validators-melinda/fix-country-codestest');
23
+ async function testValidatorFactory() {
24
+ const validator = await (0, _fixCountryCodes.default)();
25
+ (0, _chai.expect)(validator).to.be.an('object').that.has.any.keys('description', 'validate');
26
+ (0, _chai.expect)(validator.description).to.be.a('string');
27
+ (0, _chai.expect)(validator.validate).to.be.a('function');
28
+ }
29
+ async function callback({
30
+ getFixture,
31
+ enabled = true,
32
+ fix = false
33
+ }) {
34
+ if (enabled === false) {
35
+ debug('TEST SKIPPED!');
36
+ return;
37
+ }
38
+ const validator = await (0, _fixCountryCodes.default)();
39
+ const record = new _marcRecord.MarcRecord(getFixture('record.json'));
40
+ const expectedResult = getFixture('expectedResult.json');
41
+ // console.log(expectedResult); // eslint-disable-line
42
+
43
+ if (!fix) {
44
+ const result = await validator.validate(record);
45
+ (0, _chai.expect)(result).to.eql(expectedResult);
46
+ return;
47
+ }
48
+ await validator.fix(record);
49
+ (0, _chai.expect)(record).to.eql(expectedResult);
50
+ }
51
+ //# sourceMappingURL=fix-country-codes.spec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fix-country-codes.spec.js","names":["_chai","require","_marcRecord","_fixCountryCodes","_interopRequireDefault","_fixura","_fixugen","_debug","obj","__esModule","default","generateTests","callback","path","__dirname","useMetadataFile","recurse","fixura","reader","READERS","JSON","mocha","before","testValidatorFactory","debug","createDebugLogger","validator","validatorFactory","expect","to","be","an","that","has","any","keys","description","a","validate","getFixture","enabled","fix","record","MarcRecord","expectedResult","result","eql"],"sources":["../src/fix-country-codes.spec.js"],"sourcesContent":["import {expect} from 'chai';\nimport {MarcRecord} from '@natlibfi/marc-record';\nimport validatorFactory from './fix-country-codes';\nimport {READERS} from '@natlibfi/fixura';\nimport generateTests from '@natlibfi/fixugen';\nimport createDebugLogger from 'debug';\n\ngenerateTests({\n callback,\n path: [__dirname, '..', 'test-fixtures', 'fix-country-codes'],\n useMetadataFile: true,\n recurse: false,\n fixura: {\n reader: READERS.JSON\n },\n mocha: {\n before: () => testValidatorFactory()\n }\n});\nconst debug = createDebugLogger('@natlibfi/marc-record-validators-melinda/fix-country-codestest');\n\nasync function testValidatorFactory() {\n const validator = await validatorFactory();\n\n expect(validator)\n .to.be.an('object')\n .that.has.any.keys('description', 'validate');\n\n expect(validator.description).to.be.a('string');\n expect(validator.validate).to.be.a('function');\n}\n\nasync function callback({getFixture, enabled = true, fix = false}) {\n if (enabled === false) {\n debug('TEST SKIPPED!');\n return;\n }\n\n const validator = await validatorFactory();\n const record = new MarcRecord(getFixture('record.json'));\n const expectedResult = getFixture('expectedResult.json');\n // console.log(expectedResult); // eslint-disable-line\n\n if (!fix) {\n const result = await validator.validate(record);\n expect(result).to.eql(expectedResult);\n return;\n }\n\n await validator.fix(record);\n expect(record).to.eql(expectedResult);\n}\n"],"mappings":";;AAAA,IAAAA,KAAA,GAAAC,OAAA;AACA,IAAAC,WAAA,GAAAD,OAAA;AACA,IAAAE,gBAAA,GAAAC,sBAAA,CAAAH,OAAA;AACA,IAAAI,OAAA,GAAAJ,OAAA;AACA,IAAAK,QAAA,GAAAF,sBAA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GAAG,MAAM,IAAAC,wBAAgB,EAAC,CAAC;EAC1C,MAAMe,MAAM,GAAG,IAAIC,sBAAU,CAACJ,UAAU,CAAC,aAAa,CAAC,CAAC;EACxD,MAAMK,cAAc,GAAGL,UAAU,CAAC,qBAAqB,CAAC;EACxD;;EAEA,IAAI,CAACE,GAAG,EAAE;IACR,MAAMI,MAAM,GAAG,MAAMnB,SAAS,CAACY,QAAQ,CAACI,MAAM,CAAC;IAC/C,IAAAd,YAAM,EAACiB,MAAM,CAAC,CAAChB,EAAE,CAACiB,GAAG,CAACF,cAAc,CAAC;IACrC;EACF;EAEA,MAAMlB,SAAS,CAACe,GAAG,CAACC,MAAM,CAAC;EAC3B,IAAAd,YAAM,EAACc,MAAM,CAAC,CAACb,EAAE,CAACiB,GAAG,CAACF,cAAc,CAAC;AACvC"}
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "url": "git@github.com:natlibfi/marc-record-validators-melinda.git"
15
15
  },
16
16
  "license": "MIT",
17
- "version": "10.15.6-alpha.1",
17
+ "version": "10.16.0-alpha.1",
18
18
  "main": "./dist/index.js",
19
19
  "publishConfig": {
20
20
  "access": "public"
@@ -0,0 +1,93 @@
1
+ // Fix country codes in 008/15-17
2
+ //
3
+ // This is proto. Extend later...
4
+ //
5
+ // Author(s): Nicholas Volk
6
+
7
+ //import createDebugLogger from 'debug';
8
+ //import {fieldToString, nvdebug} from './utils';
9
+
10
+
11
+ export default function () {
12
+
13
+ return {
14
+ description: 'Fix deprecated country codes',
15
+ validate, fix
16
+ };
17
+
18
+ function fix(record) {
19
+ //nvdebug(`FIX ME`);
20
+ const res = {message: [], fix: [], valid: true};
21
+
22
+ const [field008] = record.get('008');
23
+ if (!field008) {
24
+ return res;
25
+ }
26
+
27
+ fixCountryCode(field008);
28
+
29
+ return res;
30
+ }
31
+
32
+ function validate(record) {
33
+ const res = {message: [], valid: true};
34
+
35
+ const [field008] = record.get('008');
36
+
37
+ if (!field008) {
38
+ return res;
39
+ }
40
+
41
+ const originalCountryCode = getCountryCodeFromField008(field008);
42
+ const modifiedCountryCode = deprecatedCountryCodeToCurrentCountryCode(originalCountryCode);
43
+ if (originalCountryCode === modifiedCountryCode) {
44
+ return res;
45
+ }
46
+
47
+ res.message.push(`Modify 008/15-17: '${originalCountryCode}' => '${modifiedCountryCode}'`); // eslint-disable-line functional/immutable-data
48
+
49
+ res.valid = false; // eslint-disable-line functional/immutable-data
50
+ return res;
51
+ }
52
+
53
+ function fixCountryCode(field008) {
54
+ const originalCountryCode = getCountryCodeFromField008(field008);
55
+ const modifiedCountryCode = deprecatedCountryCodeToCurrentCountryCode(originalCountryCode);
56
+ if (originalCountryCode !== modifiedCountryCode && modifiedCountryCode.length === 3) {
57
+ field008.value = `${field008.value.substring(0, 15)}${modifiedCountryCode}${field008.value.substring(18)}`; // eslint-disable-line functional/immutable-data
58
+ return;
59
+ }
60
+ }
61
+
62
+ function getCountryCodeFromField008(field) {
63
+ return field.value.substring(15, 18); // return 008/15-17
64
+ }
65
+
66
+ function deprecatedCountryCodeToCurrentCountryCode(countryCode) {
67
+ if (countryCode === 'air') { // Armenia
68
+ return 'ai ';
69
+ }
70
+ if (countryCode === 'ajr') { // Azerbaidzan
71
+ return 'aj ';
72
+ }
73
+ // Australia
74
+ if (['aca', 'qua', 'tma', 'vra', 'wea', 'xga', 'xna', 'xoa', 'xra'].includes(countryCode)) {
75
+ return 'at ';
76
+ }
77
+
78
+ // Canada
79
+ if (['abc', 'bcc', 'cn ', 'mbc', 'nfc', 'nkc', 'nsc', 'ntc', 'nuc', 'onc', 'pic', 'quc', 'snc', 'ykc'].includes(countryCode)) {
80
+ return 'xxc';
81
+ }
82
+ // Great Britain:
83
+ if (['enk', 'nik', 'stk', 'uik', 'uk ', 'wlk'].includes(countryCode)) {
84
+ return 'xxk';
85
+ }
86
+ // United States:
87
+ if (['aku', 'alu', 'aru', 'azu', 'cau', 'cou', 'ctu', 'dcu', 'deu', 'flu', 'gau', 'hiu', 'iau', 'idu', 'ilu', 'inu', 'kgr', 'ksu', 'kyu', 'lau', 'mau', 'mdu', 'meu', 'miu', 'mnu', 'mou', 'msu', 'mtu', 'nbu', 'ncu', 'nhu', 'nju', 'nmu', 'nvu', 'nyu', 'ohu', 'oku', 'oru', 'pau', 'riu', 'scu', 'sdu', 'tnu', 'txu', 'us ', 'utu', 'vau', 'vtu', 'wau', 'wiu', 'wvu', 'wyu'].includes(countryCode)) {
88
+ return 'xxu';
89
+ }
90
+ return countryCode;
91
+ }
92
+
93
+ }
@@ -0,0 +1,52 @@
1
+ import {expect} from 'chai';
2
+ import {MarcRecord} from '@natlibfi/marc-record';
3
+ import validatorFactory from './fix-country-codes';
4
+ import {READERS} from '@natlibfi/fixura';
5
+ import generateTests from '@natlibfi/fixugen';
6
+ import createDebugLogger from 'debug';
7
+
8
+ generateTests({
9
+ callback,
10
+ path: [__dirname, '..', 'test-fixtures', 'fix-country-codes'],
11
+ useMetadataFile: true,
12
+ recurse: false,
13
+ fixura: {
14
+ reader: READERS.JSON
15
+ },
16
+ mocha: {
17
+ before: () => testValidatorFactory()
18
+ }
19
+ });
20
+ const debug = createDebugLogger('@natlibfi/marc-record-validators-melinda/fix-country-codestest');
21
+
22
+ async function testValidatorFactory() {
23
+ const validator = await validatorFactory();
24
+
25
+ expect(validator)
26
+ .to.be.an('object')
27
+ .that.has.any.keys('description', 'validate');
28
+
29
+ expect(validator.description).to.be.a('string');
30
+ expect(validator.validate).to.be.a('function');
31
+ }
32
+
33
+ async function callback({getFixture, enabled = true, fix = false}) {
34
+ if (enabled === false) {
35
+ debug('TEST SKIPPED!');
36
+ return;
37
+ }
38
+
39
+ const validator = await validatorFactory();
40
+ const record = new MarcRecord(getFixture('record.json'));
41
+ const expectedResult = getFixture('expectedResult.json');
42
+ // console.log(expectedResult); // eslint-disable-line
43
+
44
+ if (!fix) {
45
+ const result = await validator.validate(record);
46
+ expect(result).to.eql(expectedResult);
47
+ return;
48
+ }
49
+
50
+ await validator.fix(record);
51
+ expect(record).to.eql(expectedResult);
52
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "message" : [
3
+ ],
4
+ "valid": true
5
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "description": "Validate 008/15-17: nothing to do",
3
+ "enabled": true,
4
+ "fix": false,
5
+ "only": false
6
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "fields" : [
3
+ { "tag": "005", "value": "20220202020202.0" },
4
+ { "tag": "008", "value": "860616s1985 fi |||||||||||||||||swe||"}
5
+ ]
6
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "message": [
3
+ "Modify 008/15-17: 'azu' => 'xxu'"
4
+ ],
5
+ "valid": false
6
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "description": "Validate 008/15-17: azu -> xxu",
3
+ "enabled": true,
4
+ "fix": false,
5
+ "only": false
6
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "fields" : [
3
+ { "tag": "005", "value": "20220202020202.0" },
4
+ { "tag": "008", "value": "860616s1985 azu|||||||||||||||||swe||"}
5
+ ]
6
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "_validationOptions": {},
3
+ "leader": "",
4
+ "fields" : [
5
+ { "tag": "005", "value": "20220202020202.0" },
6
+ { "tag": "008", "value": "860616s1985 fi |||||||||||||||||swe||"}
7
+ ]
8
+
9
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "description": "Fix 008/15-17: nothing to do",
3
+ "enabled": true,
4
+ "fix": true,
5
+ "only": false
6
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "fields" : [
3
+ { "tag": "005", "value": "20220202020202.0" },
4
+ { "tag": "008", "value": "860616s1985 fi |||||||||||||||||swe||"}
5
+ ]
6
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "_validationOptions": {},
3
+ "leader": "",
4
+ "fields" : [
5
+ { "tag": "005", "value": "20220202020202.0" },
6
+ { "tag": "008", "value": "860616s1985 at |||||||||||||||||swe||"}
7
+ ]
8
+
9
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "description": "Fix 008/15-17: 'tma' => 'at#'",
3
+ "enabled": true,
4
+ "fix": true,
5
+ "only": false
6
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "fields" : [
3
+ { "tag": "005", "value": "20220202020202.0" },
4
+ { "tag": "008", "value": "860616s1985 tma|||||||||||||||||swe||"}
5
+ ]
6
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "_validationOptions": {},
3
+ "leader": "",
4
+ "fields" : [
5
+ { "tag": "005", "value": "20220202020202.0" }
6
+ ]
7
+
8
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "description": "Fix 008/15-17: sanity check against missing 008 field",
3
+ "enabled": true,
4
+ "fix": true,
5
+ "only": false
6
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "fields" : [
3
+ { "tag": "005", "value": "20220202020202.0" }
4
+ ]
5
+ }