Skip to content

Commit

Permalink
Require explicit handling of optional and required fields (WICG#1339)
Browse files Browse the repository at this point in the history
  • Loading branch information
apasel422 authored Jun 20, 2024
1 parent cd0bb31 commit 78322dd
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 219 deletions.
15 changes: 11 additions & 4 deletions ts/src/header-validator/validate-eligible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ export type Eligible = {
trigger: boolean
}

function presence(v: Item | InnerList, ctx: Context): Maybe<boolean> {
function presence(
v: Item | InnerList | undefined,
ctx: Context
): Maybe<boolean> {
if (v === undefined) {
return Maybe.some(false)
}

if (v[0] !== true) {
ctx.warning('ignoring dictionary value')
}
Expand All @@ -35,9 +42,9 @@ export function validateEligible(
): [ValidationResult, Maybe<Eligible>] {
return validateDictionary(str, new Context(), (d, ctx) =>
struct(d, ctx, {
navigationSource: field(navigationSourceKey, presence, false),
eventSource: field(eventSourceKey, presence, false),
trigger: field(triggerKey, presence, false),
navigationSource: field(navigationSourceKey, presence),
eventSource: field(eventSourceKey, presence),
trigger: field(triggerKey, presence),
}).filter((v) => {
if (v.navigationSource && (v.eventSource || v.trigger)) {
ctx.error(
Expand Down
23 changes: 14 additions & 9 deletions ts/src/header-validator/validate-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ export type Info = {
}

function preferredPlatform(
v: Item | InnerList,
v: Item | InnerList | undefined,
ctx: Context
): Maybe<PreferredPlatform> {
): Maybe<PreferredPlatform | null> {
if (v === undefined) {
return Maybe.some(null)
}
if (!(v[0] instanceof Token)) {
ctx.error('must be a token')
return Maybe.None
Expand All @@ -37,7 +40,13 @@ function preferredPlatform(
})
}

function reportHeaderErrors(v: Item | InnerList, ctx: Context): Maybe<boolean> {
function reportHeaderErrors(
v: Item | InnerList | undefined,
ctx: Context
): Maybe<boolean> {
if (v === undefined) {
return Maybe.some(false)
}
if (typeof v[0] !== 'boolean') {
ctx.error('must be a boolean')
return Maybe.None
Expand All @@ -51,12 +60,8 @@ function reportHeaderErrors(v: Item | InnerList, ctx: Context): Maybe<boolean> {
export function validateInfo(str: string): [ValidationResult, Maybe<Info>] {
return validateDictionary(str, new Context(), (d, ctx) =>
struct(d, ctx, {
preferredPlatform: field('preferred-platform', preferredPlatform, null),
reportHeaderErrors: field(
'report-header-errors',
reportHeaderErrors,
false
),
preferredPlatform: field('preferred-platform', preferredPlatform),
reportHeaderErrors: field('report-header-errors', reportHeaderErrors),
})
)
}
Expand Down
Loading

0 comments on commit 78322dd

Please sign in to comment.