Skip to content

Commit

Permalink
Use xml schema datetime validator.
Browse files Browse the repository at this point in the history
  • Loading branch information
aljones15 committed Dec 1, 2023
1 parent e76ef4f commit 80d30d7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Added
- **BREAKING**: Default issuance now uses VC 2.0 context.
- **BREAKING**: DateTime validator is now an xml schema DateTime validator.
- Support for VC 2.0 credentials issuance and verification.
- Support for VC 2.0 `validUntil` and `validFrom`.
- Support for VP 2.0 presentations issuance and verification.
Expand Down
16 changes: 9 additions & 7 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ export const {
CONTEXT_URL: CREDENTIALS_CONTEXT_V2_URL
}
} = credentialsContextV2;
// Z and T can be lowercase
// RFC3339 regex
export const dateRegex = new RegExp('^(\\d{4})-(0[1-9]|1[0-2])-' +
'(0[1-9]|[12][0-9]|3[01])T([01][0-9]|2[0-3]):' +
'([0-5][0-9]):([0-5][0-9]|60)' +
'(\\.[0-9]+)?(Z|(\\+|-)([01][0-9]|2[0-3]):' +
'([0-5][0-9]))$', 'i');
// Z and T must be uppercase
// xml schema date time RegExp
// @see https://www.w3.org/TR/xmlschema11-2/#dateTime
export const dateRegex = new RegExp(
'-?([1-9][0-9]{3,}|0[0-9]{3})' +
'-(0[1-9]|1[0-2])' +
'-(0[1-9]|[12][0-9]|3[01])' +
'T(([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](\.[0-9]+)?|(24:00:00(\.0+)?))' +
'(Z|(\\+|-)((0[0-9]|1[0-3]):[0-5][0-9]|14:00))?');

// entries should be in ascending version order
// so v1 is entry 0
Expand Down
15 changes: 8 additions & 7 deletions test/20-dateRegex.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@ chai.should();

import * as vc from '../lib/index.js';

const assertDateTime = (date, bool) =>
vc.dateRegex.test(date).should.equal(bool);

describe('verifies RFC3339 Dates', function() {
it('verify a valid date', function() {
const latest = new Date().toISOString();
vc.dateRegex.test(latest).should.be.true;
assertDateTime(latest, true);
});
it('verify a valid date with lowercase t', function() {
it('does not verify a valid date with lowercase t', function() {
const latest = new Date().toISOString().toLowerCase();
vc.dateRegex.test(latest).should.be.true;
assertDateTime(latest, false);
});

it('should not verify an invalid date', function() {
const invalid = '2017/09/27';
vc.dateRegex.test(invalid).should.be.false;
assertDateTime('2017/09/27', false);
});
it('should not verify 2 digit years', function() {
const invalid = '17-09-27T22:07:22.563z';
vc.dateRegex.test(invalid).should.be.false;
assertDateTime('17-09-27T22:07:22.563Z', false);
});
});

0 comments on commit 80d30d7

Please sign in to comment.