Skip to content

Commit

Permalink
Validate input to Calendar.prototype.fields
Browse files Browse the repository at this point in the history
As discussed in #1610, we restrict values returned by the iterator to be no more than the ten known values, prohibiting duplicates. The check for String values has been removed as all of those values are Strings, so the value restriction is plenty.

This does not address the question of return value validation.
  • Loading branch information
cjtenny authored and Ms2ger committed Sep 9, 2021
1 parent 4e63f25 commit 7ebc700
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,22 @@ export class Calendar implements Temporal.Calendar {
fields(fields) {
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
const fieldsArray = [];
const allowed = new Set([
'year',
'month',
'monthCode',
'day',
'hour',
'minute',
'second',
'millisecond',
'microsecond',
'nanosecond'
]);
for (const name of fields) {
if (ES.Type(name) !== 'String') throw new TypeError('invalid fields');
if (!allowed.has(name)) throw new RangeError(`invalid field name ${name}`);
allowed.delete(name);
ArrayPrototypePush.call(fieldsArray, name);
}
return impl[GetSlot(this, CALENDAR_ID)].fields(fieldsArray);
Expand Down

0 comments on commit 7ebc700

Please sign in to comment.