Skip to content

Commit

Permalink
Fix lexicon validation, do not allow undefined when required (bluesky…
Browse files Browse the repository at this point in the history
…-social#457)

* Fix lexicon validation, do not allow undefined when required

* Make term optional for actor search/typeahead
  • Loading branch information
devinivy authored Jan 5, 2023
1 parent 7d6449a commit e9f5f23
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 12 deletions.
1 change: 0 additions & 1 deletion lexicons/app/bsky/actor/search.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"description": "Find users matching search criteria.",
"parameters": {
"type": "params",
"required": ["term"],
"properties": {
"term": {"type": "string"},
"limit": {"type": "integer", "minimum": 1, "maximum": 100, "default": 50},
Expand Down
1 change: 0 additions & 1 deletion lexicons/app/bsky/actor/searchTypeahead.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"description": "Find user suggestions for a search term.",
"parameters": {
"type": "params",
"required": ["term"],
"properties": {
"term": {"type": "string"},
"limit": {"type": "integer", "minimum": 1, "maximum": 100, "default": 50}
Expand Down
2 changes: 0 additions & 2 deletions packages/api/src/client/lexicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1241,7 +1241,6 @@ export const schemaDict = {
description: 'Find users matching search criteria.',
parameters: {
type: 'params',
required: ['term'],
properties: {
term: {
type: 'string',
Expand Down Expand Up @@ -1317,7 +1316,6 @@ export const schemaDict = {
description: 'Find user suggestions for a search term.',
parameters: {
type: 'params',
required: ['term'],
properties: {
term: {
type: 'string',
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/client/types/app/bsky/actor/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { lexicons } from '../../../../lexicons'
import * as AppBskySystemDeclRef from '../system/declRef'

export interface QueryParams {
term: string
term?: string
limit?: number
before?: string
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { lexicons } from '../../../../lexicons'
import * as AppBskySystemDeclRef from '../system/declRef'

export interface QueryParams {
term: string
term?: string
limit?: number
}

Expand Down
2 changes: 1 addition & 1 deletion packages/lexicon/src/validators/complex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export function object(
// required
if (Array.isArray(def.required)) {
for (const key of def.required) {
if (!(key in value)) {
if (typeof value[key] === 'undefined') {
return {
success: false,
error: new ValidationError(`${path} must have the property "${key}"`),
Expand Down
2 changes: 1 addition & 1 deletion packages/lexicon/src/validators/xrpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function params(
// required
if (Array.isArray(def.required)) {
for (const key of def.required) {
if (!(key in (value as Record<string, unknown>))) {
if (typeof (value as Record<string, unknown>)[key] === 'undefined') {
return {
success: false,
error: new ValidationError(`${path} must have the property "${key}"`),
Expand Down
19 changes: 19 additions & 0 deletions packages/lexicon/tests/general.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ describe('Record validation', () => {
datetime: new Date().toISOString(),
}),
).toThrow('Record must have the property "object"')
expect(() =>
lex.assertValidRecord('com.example.kitchenSink', {
$type: 'com.example.kitchenSink',
object: undefined,
array: ['one', 'two'],
boolean: true,
number: 123.45,
integer: 123,
string: 'string',
datetime: new Date().toISOString(),
}),
).toThrow('Record must have the property "object"')
})

it('Fails incorrect types', () => {
Expand Down Expand Up @@ -569,6 +581,13 @@ describe('XRPC parameter validation', () => {
number: 123.45,
}),
).toThrow('Params must have the property "integer"')
expect(() =>
lex.assertValidXrpcParams('com.example.query', {
boolean: true,
number: 123.45,
integer: undefined,
}),
).toThrow('Params must have the property "integer"')
})

it('Validates parameter types', () => {
Expand Down
2 changes: 0 additions & 2 deletions packages/pds/src/lexicon/lexicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1241,7 +1241,6 @@ export const schemaDict = {
description: 'Find users matching search criteria.',
parameters: {
type: 'params',
required: ['term'],
properties: {
term: {
type: 'string',
Expand Down Expand Up @@ -1317,7 +1316,6 @@ export const schemaDict = {
description: 'Find user suggestions for a search term.',
parameters: {
type: 'params',
required: ['term'],
properties: {
term: {
type: 'string',
Expand Down
2 changes: 1 addition & 1 deletion packages/pds/src/lexicon/types/app/bsky/actor/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { HandlerAuth } from '@atproto/xrpc-server'
import * as AppBskySystemDeclRef from '../system/declRef'

export interface QueryParams {
term: string
term?: string
limit?: number
before?: string
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { HandlerAuth } from '@atproto/xrpc-server'
import * as AppBskySystemDeclRef from '../system/declRef'

export interface QueryParams {
term: string
term?: string
limit?: number
}

Expand Down

0 comments on commit e9f5f23

Please sign in to comment.