Skip to content

Commit c160c1c

Browse files
authored
Feature update validation (#35)
* Update record validation to follow rules in https://www.loc.gov/marc/specifications/specrecstruc.html , including: - Validate that leader is 24 characters long (if configured with validationOption `leader: true`) - Validate that leader contains only printable/graphic ASCII characters (if configured with validationOption `characters: true`) - Validate that field tags are three characters long - Validate that field tags consist only of ASCII numeric characters (decimal integers 0-9) and/or ASCII alphabetic characters (uppercase or lowercase, but not both). (if configured with validationOption `characters: true`) - Validate that controlField tags start with '00' or ASCII alphabetic characters (if configured with validationOption `characters: true`) - Validate that dataField tags do not start with '00' (if configured with validationOption `characters: true`) - Validate that controlFields do not have ind1, ind2 or subfields - Validate that controlField values contain only printable/graphic ASCII characters (if configured with validationOption `characters: true`) - Validate that controlField value does not exceed maximum length for a field - Validate that dataFields do not have value - Validate that indicators are one character long - Validate that indicators consist only of blanks or ASCII numeric or lowercase alphabetic characters (if configured with validationOption `characters: true`) - Validate that subfield codes are one character long - Validate that subfield codes consist only of printable/graphic ASCII characters excluding blank (if configured with validationOption `characters: true`) - Validate that subfield values do not contain ASCII control characters (if configured with validationOption `noControlCharacters: true`) - Validate that subfield values do not exceed maximum length for a field - Validate that fields or subfields do not have additional properties (if configured with validationOption `noAdditionalProperties: true`) * Add new validationOption `strict: true` that causes all other validationOptions to be handled as `true` * Add new validationOptions and update default validationOptions: ``` strict: false // If true, all validationOptions below are handled as if true fields: true, // Do not allow record without fields subfields: true, // Do not allow empty subfields subfieldValues: true, // Do not allow subfields without value controlFieldValues: true // Do not allow controlFields without value leader: false, // Do not allow record without leader, with empty leader or with leader with length != 24 characters: false // Do not allow erronous characters in tags, indicators and subfield codes noControlCharacters: false, // Do not allow ASCII control characters in field/subfield values noAdditionalProperties: false // Do not allow additional properties in fields ``` * Update deps * Update node-tests: v19.x -> v20.x * Fix and add tests * Add some debug * Update and fix README * Add commentary to schema * 7.3.0-alpha.7
1 parent eb1e888 commit c160c1c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+866
-513
lines changed

.github/workflows/melinda-node-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111

1212
strategy:
1313
matrix:
14-
node-version: [16.x, 18.x, 19.x]
14+
node-version: [16.x, 18.x, 20.x]
1515
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
1616

1717
steps:

README.md

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ const record = new MarcRecord();
1717
```js
1818
const record = new MarcRecord(
1919
{
20-
leader: 'foo',
20+
leader: '02848ccm a22005894i 4500',
21+
',
2122
fields: [
2223
{tag: '001', value: 'foo'},
2324
{tag: '002', value: 'bar'},
@@ -35,17 +36,31 @@ const recordB = MarcRecord.clone(recordA)
3536
3637
When constructing or modifying the record, validation checks are run. You may need to alter these checks to work with incomplete intermediate records.
3738
39+
**strict**
40+
41+
validationOption `strict: true` sets all the other validationOptions as true regardless of if they are defined
42+
validationOption `strict: false` sets other validationOptions as they are defined or as default
43+
3844
**Global validation options:**
3945
4046
```js
4147
MarcRecord.getValidationOptions();
4248
4349
// Default settings
50+
// These default validationOptions are (mostly) backwards compatible with marc-record-js < 7.3.0
51+
4452
MarcRecord.setValidationOptions(
4553
{
46-
fields: true, // Do not allow record without fields
47-
subfields: true, // Do not allow empty subfields
48-
subfieldsValues: true, // Do not allow subfields without value
54+
fields: true, // Do not allow record without fields
55+
subfields: true, // Do not allow empty subfields
56+
subfieldValues: true, // Do not allow subfields without value
57+
controlFieldValues: true, // Do not allow controlFields without value
58+
leader: false, // Do not allow record without leader, with empty leader or with leader with length != 24
59+
characters: false, // Do not allow erronous characters in tags, indicators and subfield codes
60+
noControlCharacters: false, // Do not allow ASCII control characters in field/subfield values
61+
noAdditionalProperties: false, // Do not allow additional properties in fields
62+
63+
strict: false // If true, set all validationOptions to true
4964
}
5065
);
5166
```
@@ -57,12 +72,20 @@ You can reset global validation options to default with empty object:
5772
MarcRecord.setValidationOptions({});
5873
```
5974
75+
You can set all global validation options to true with validationOption strict: true:
76+
77+
```js
78+
// Set all validationOptions to true with strict: true
79+
MarcRecord.setValidationOptions({strict: true});
80+
```
81+
82+
6083
**Record specific validation options** can be given when constructing:
6184
6285
```js
6386
const record = new MarcRecord(
6487
{
65-
leader: 'foo',
88+
leader: '02848ccm a22005894i 4500',
6689
fields: []
6790
},
6891
{fields: false} // Allow empty fields
@@ -77,27 +100,27 @@ The following examples demonstrate the invalid records, when default validation
77100
// Error: fields[] is empty. Validation option: fields
78101
new MarcRecord(
79102
{
80-
leader: 'foo',
103+
leader: '02848ccm a22005894i 4500',
81104
fields: []
82105
}
83106
);
84107
85108
// Error: subfields[] is empty. Validation option: subfields
86109
new MarcRecord(
87110
{
88-
leader: 'foo',
111+
leader: '02848ccm a22005894i 4500',
89112
fields: [
90-
{tag: "021", subfields: []}
113+
{tag: "509", , ind1: " ", ind2: " ", subfields: []}
91114
]
92115
}
93116
);
94117
95118
// Error: subfield has no value. Validation option: subfieldValues
96119
new MarcRecord(
97120
{
98-
leader: 'foo',
121+
leader: '02848ccm a22005894i 4500',
99122
fields: [
100-
{tag: "021", subfields: [{code: "a", value: ""}]}
123+
{tag: "509", ind1: " ", ind2: " ", subfields: [{code: "a", value: ""}]}
101124
]
102125
}
103126
);
@@ -229,7 +252,7 @@ Failing examples:
229252
// Example record
230253
const record = new MarcRecord(
231254
{
232-
leader: "foo",
255+
leader: "02848ccm a22005894i 4500",
233256
fields: [
234257
{tag: "001", value: "bar"}
235258
]
@@ -304,6 +327,6 @@ To serialize and unserialize MARC records, see [marc-record-serializers](https:/
304327
305328
Copyright (c) 2014-2017 **Pasi Tuominen <[email protected]>**
306329
307-
Copyright (c) 2018 **University Of Helsinki (The National Library Of Finland)**
330+
Copyright (c) 2018-2023 **University Of Helsinki (The National Library Of Finland)**
308331
309332
This project's source code is licensed under the terms of **MIT License** or any later version.

0 commit comments

Comments
 (0)