Skip to content

Commit

Permalink
fix: add jsdoc and test for parsing object
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalics committed Sep 1, 2024
1 parent 259a6a5 commit 8794653
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1298,8 +1298,12 @@ class ObjectSchemaBuilder<
/**
* Disallow additional properties for object schema `additionalProperties=false`
*
* If you would like to define additional properties type - use `additionalProeprties`
* If you would like to define additional properties type - use `additionalProperties`
* @see {@link ObjectSchemaBuilder.additionalProperties additionalProperties}
*
* If you would like to mark properties required - use `required` or `requiredFor`
* @see {@link ObjectSchemaBuilder.required required}
* @see {@link ObjectSchemaBuilder.requiredFor requiredFor}
*/
strict() {
this.schema.additionalProperties = false;
Expand All @@ -1326,7 +1330,8 @@ class ObjectSchemaBuilder<
/**
* Make **ALL** properties in your object required.
*
* If you need to make 1 property required - use {@link ObjectSchemaBuilder.requiredFor}
* If you need to make few required properties (1 or more, not everything fields) - use `requiredFor`
* @see {@link ObjectSchemaBuilder.requiredFor requiredFor}
*/
required(): ObjectSchemaBuilder<Definition, Required<T>> {
const allProperties = Object.keys(this.schema.properties!);
Expand Down
20 changes: 19 additions & 1 deletion tests/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,22 @@ test('#61 optional nullable object schema should parsed successfully', () => {
expect(optionalNullableObj.parse({})).toStrictEqual({});
expect(optionalNullableObj.parse({ sex: 'male' })).toStrictEqual({ sex: 'male' });
expect(optionalNullableObj.parse({ sex: null })).toStrictEqual({ sex: null });
})
})

test('#61 should parse big schema successfully', () => {
const schema2 = s.object({
name: s.string().minLength(2).maxLength(20),
age: s.number().integer().min(0).max(100),

// default optional - string | null | undefined
email: s.string().format('email').nullable().optional().default(null),

//optional - string | null | undefined
phone: s.string().nullable().optional(),

// required - string | null
surname: s.string().nullable(),
}).strict();

expect(schema2.parse({ name: 'Alex', age: 10, })).toStrictEqual({ name: 'Alex', age: 10, email: null });
})

0 comments on commit 8794653

Please sign in to comment.