Skip to content

Conversation

Copy link

Copilot AI commented Jan 2, 2026

  • Understand the codebase and existing literal handling mechanism
  • Update nodeToJsonldRepresentation.ts to convert xsd:dateTime and xsd:date literals to JavaScript Date objects
  • Update type definitions to include Date in LiteralLike and RawValue types
  • Update getNodeFromRaw.ts to handle Date objects when writing back to the dataset
    • Format xsd:date as date-only (YYYY-MM-DD)
    • Use full ISO format for xsd:dateTime
    • Prevent Date objects from being used as NamedNode identifiers
    • Restrict Date coercion to only xsd:date and xsd:dateTime datatypes
  • Update getValueForKey.ts to include Date in the return type
  • Add tests for Date conversion in nodeToJsonRepresentation.test.ts
  • Update existing tests to expect Date objects for date fields
  • Add test for setting a Date value to a date field
  • Add timezone edge case test for xsd:date serialization
  • Fix spelling errors in test description
  • Run tests to verify the changes (all 230 tests pass)
  • Run CodeQL security scan (no issues found)
Original prompt

This section details on the original issue you should resolve

<issue_title>feat: improved literal handling</issue_title>
<issue_description>Rather than all literals being converted to strings it would be nice to have numeric types go to numbers, dateTimes go to Dates and so forth.

You should be able to largely use https://www.npmjs.com/package/rdf-literal or re-use https://github.com/LDflex/LDflex/blob/3379cb7059dccdfb41de804a2e0db338e974cc3a/src/valueUtils.js to achieve this.

Edit I've noticed that you have this in some cases, added support for dates would be excellent</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits January 2, 2026 21:23
…e to JavaScript Date objects

Co-authored-by: jeswr <63333554+jeswr@users.noreply.github.com>
Co-authored-by: jeswr <63333554+jeswr@users.noreply.github.com>
Copilot AI changed the title [WIP] Improve handling of numeric and date literals feat: convert xsd:date and xsd:dateTime literals to JavaScript Date objects Jan 2, 2026
Copilot AI requested a review from jeswr January 2, 2026 21:29
@elf-pavlik
Copy link
Contributor

Eventually it might be better to use https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Instant

There are some existing polyfills, maybe it's better to use them rather than making a breaking change once Temporal becomes more broadly available? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal#see_also

@jeswr jeswr marked this pull request as ready for review January 3, 2026 13:16
Copilot AI review requested due to automatic review settings January 3, 2026 13:16
@jeswr
Copy link
Collaborator

jeswr commented Jan 3, 2026

I'd be inclined to stick with Date until Temporal.Instant is more widely adopted.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR enhances literal handling by adding automatic conversion of xsd:date and xsd:dateTime RDF literals to JavaScript Date objects, similar to existing numeric and boolean conversions.

Key Changes

  • Reading: xsd:date and xsd:dateTime literals are automatically parsed into Date objects instead of remaining as strings
  • Writing: Date objects can be set on date fields and are serialized appropriately (date-only format for xsd:date, full ISO 8601 for xsd:dateTime)
  • Types: Updated LiteralLike and RawValue type definitions to include Date, enabling proper TypeScript type checking

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
packages/jsonld-dataset-proxy/src/util/nodeToJsonldRepresentation.ts Adds Date object creation for xsd:date and xsd:dateTime literals during reading
packages/jsonld-dataset-proxy/src/util/getNodeFromRaw.ts Implements Date serialization logic with format selection based on datatype (date-only vs full ISO)
packages/jsonld-dataset-proxy/src/types.ts Extends LiteralLike type to include Date
packages/jsonld-dataset-proxy/src/util/RawObject.ts Extends RawValue type to include Date
packages/jsonld-dataset-proxy/src/subjectProxy/getValueForKey.ts Updates return type signature to include Date
packages/jsonld-dataset-proxy/test/patientExampleData.ts Updates PatientShape type to accept Date for birthdate field
packages/jsonld-dataset-proxy/test/nodeToJsonRepresentation.test.ts Adds comprehensive tests for date/dateTime parsing including timezone handling
packages/jsonld-dataset-proxy/test/jsonldDatasetProxy.test.ts Updates test expectations to validate Date objects and fixes date format in test data

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 42 to 44
} else {
// Use full ISO format for xsd:dateTime and other date-related types
return literal(value.toISOString(), datatype);
Copy link

Copilot AI Jan 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The else branch at line 42-44 converts any Date object to ISO string format when the datatype is not xsd:date, even for non-date-related datatypes. This could lead to unexpected behavior if a Date object is accidentally passed for a field with a different datatype (e.g., xsd:string). Consider restricting Date object handling to only xsd:date and xsd:dateTime datatypes, and either throw an error or return undefined for other datatypes.

Suggested change
} else {
// Use full ISO format for xsd:dateTime and other date-related types
return literal(value.toISOString(), datatype);
} else if (datatype === "http://www.w3.org/2001/XMLSchema#dateTime") {
// Use full ISO format for xsd:dateTime
return literal(value.toISOString(), datatype);
} else {
// For non-date-related datatypes, do not coerce Date values
return undefined;

Copilot uses AI. Check for mistakes.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied this suggestion in 587aa3e. Date objects are now only accepted for xsd:date and xsd:dateTime datatypes, returning undefined for other datatypes.

Comment on lines 26 to +27
case "http://www.w3.org/2001/XMLSchema#dateTime":
return new Date(literal.value);
Copy link

Copilot AI Jan 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Date constructor may create an invalid Date object if the literal value is malformed. Consider adding validation to check if the date is valid using isNaN(date.getTime()) and returning the original string value if parsing fails, maintaining backward compatibility with invalid date strings.

Suggested change
case "http://www.w3.org/2001/XMLSchema#dateTime":
return new Date(literal.value);
case "http://www.w3.org/2001/XMLSchema#dateTime": {
const date = new Date(literal.value);
return isNaN(date.getTime()) ? literal.value : date;
}

Copilot uses AI. Check for mistakes.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied this suggestion in 587aa3e. Invalid dates now return the original string value.

).toBe("P1Y2M3D");
});

it("throws an error when it encoutners a quad that is not a Liter, NamedNode, or BlankNode", () => {
Copy link

Copilot AI Jan 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spelling error: "encoutners" should be "encounters" and "Liter" should be "Literal".

Suggested change
it("throws an error when it encoutners a quad that is not a Liter, NamedNode, or BlankNode", () => {
it("throws an error when it encounters a quad that is not a Literal, NamedNode, or BlankNode", () => {

Copilot uses AI. Check for mistakes.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 587aa3e.

@jeswr
Copy link
Collaborator

jeswr commented Jan 3, 2026

@copilot apply changes based on the comments in this thread

- Validate Date parsing and return original string for invalid dates
- Restrict Date coercion to only xsd:date and xsd:dateTime datatypes
- Add tests for invalid date strings
- Add timezone edge case test for xsd:date serialization
- Fix spelling errors in test description

Co-authored-by: jeswr <63333554+jeswr@users.noreply.github.com>
Copy link
Author

Copilot AI commented Jan 3, 2026

@copilot apply changes based on the comments in this thread

Applied all the requested changes in commit 587aa3e:

  • Added validation for invalid Date parsing (returns original string if invalid)
  • Restricted Date coercion to only xsd:date and xsd:dateTime datatypes
  • Added tests for invalid date strings
  • Added timezone edge case test
  • Fixed spelling errors in test description

@jeswr
Copy link
Collaborator

jeswr commented Jan 3, 2026

@copilot please revert the support for invalid dates

Per reviewer request, reverted the invalid date validation that returned
the original string for malformed dates. Invalid dates will now return
an Invalid Date object, consistent with standard JavaScript Date behavior.

Co-authored-by: jeswr <63333554+jeswr@users.noreply.github.com>
Copy link
Author

Copilot AI commented Jan 3, 2026

@copilot please revert the support for invalid dates

Reverted in 91e6d86. Invalid dates will now return an Invalid Date object consistent with standard JavaScript Date behavior.

@jeswr jeswr requested a review from jaxoncreed January 3, 2026 14:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: improved literal handling

3 participants