Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

let/const consistency in README: let preference #2227

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 28 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ let userSchema = object({
});

// parse and assert validity
const user = await userSchema.validate(await fetchUser());
let user = await userSchema.validate(await fetchUser());

type User = InferType<typeof userSchema>;
/* {
Expand All @@ -46,7 +46,7 @@ transform that value into more concrete and specific values, without making furt

```ts
// Attempts to coerce values to the correct type
const parsedUser = userSchema.cast({
let parsedUser = userSchema.cast({
name: 'jimmy',
age: '24',
createdOn: '2014-09-23T19:25:25Z',
Expand All @@ -59,7 +59,7 @@ of running parsing logic.

```ts
// ❌ ValidationError "age is not a number"
const parsedUser = await userSchema.validate(
let parsedUser = await userSchema.validate(
{
name: 'jimmy',
age: '24',
Expand Down Expand Up @@ -190,9 +190,9 @@ Each built-in type implements basic type parsing, which comes in handy when pars
Additionally types implement type specific transforms that can be enabled.

```ts
const num = number().cast('1'); // 1
let num = number().cast('1'); // 1

const obj = object({
let obj = object({
firstName: string().lowercase().trim(),
})
.json()
Expand All @@ -203,7 +203,7 @@ const obj = object({
Custom transforms can be added

```ts
const reversedString = string()
let reversedString = string()
.transform((currentValue) => currentValue.split('').reverse().join(''))
.cast('dlrow olleh'); // "hello world"
```
Expand All @@ -230,7 +230,7 @@ string()
As with transforms, tests can be customized on the fly

```ts
const jamesSchema = string().test(
let jamesSchema = string().test(
'is-james',
(d) => `${d.path} is not James`,
(value) => value == null || value === 'James',
Expand Down Expand Up @@ -258,7 +258,7 @@ in the case of a nested validation.
Error messages can also be constructed on the fly to customize how the schema fails.

```ts
const order = object({
let order = object({
no: number().required(),
sku: string().test({
name: 'is-sku',
Expand Down Expand Up @@ -287,11 +287,11 @@ Schema are immutable, each method call returns a new schema object. Reuse and pa
fear of mutating another instance.

```ts
const optionalString = string().optional();
let optionalString = string().optional();

const definedString = optionalString.defined();
let definedString = optionalString.defined();

const value = undefined;
let value = undefined;
optionalString.isValid(value); // true
definedString.isValid(value); // false
```
Expand All @@ -303,7 +303,7 @@ Yup schema produce static TypeScript interfaces. Use `InferType` to extract that
```ts
import * as yup from 'yup';

const personSchema = yup.object({
let personSchema = yup.object({
firstName: yup.string().defined(),
nickName: yup.string().default('').nullable(),
sex: yup
Expand All @@ -327,11 +327,11 @@ setting a default affects the output type of the schema, essentially marking it
```ts
import { string } from 'yup';

const value: string = string().default('hi').validate(undefined);
let value: string = string().default('hi').validate(undefined);

// vs

const value: string | undefined = string().validate(undefined);
let value: string | undefined = string().validate(undefined);
```

### Ensuring a schema matches an existing type
Expand All @@ -349,15 +349,15 @@ interface Person {
}

// will raise a compile-time type error if the schema does not produce a valid Person
const schema: ObjectSchema<Person> = object({
let schema: ObjectSchema<Person> = object({
name: string().defined(),
age: number().optional(),
sex: string<'male' | 'female' | 'other'>().nullable().defined(),
});

// ❌ errors:
// "Type 'number | undefined' is not assignable to type 'string'."
const badSchema: ObjectSchema<Person> = object({
let badSchema: ObjectSchema<Person> = object({
name: number(),
});

Expand Down Expand Up @@ -664,11 +664,11 @@ Collects schema details (like meta, labels, and active tests) into a serializabl
description object.

```ts
const schema = object({
let schema = object({
name: string().required(),
});

const description = schema.describe();
let description = schema.describe();
```

For schema with dynamic components (references, lazy, or conditions), describe requires
Expand Down Expand Up @@ -954,7 +954,7 @@ Indicates that `null` is a valid value for the schema. Without `nullable()`
`null` is treated as a different type and will fail `Schema.isType()` checks.

```ts
const schema = number().nullable();
let schema = number().nullable();

schema.cast(null); // null

Expand All @@ -967,7 +967,7 @@ The opposite of `nullable`, removes `null` from valid type values for the schema
**Schema are non nullable by default**.

```ts
const schema = number().nonNullable();
let schema = number().nonNullable();

schema.cast(null); // TypeError

Expand All @@ -979,7 +979,7 @@ InferType<typeof schema>; // number
Require a value for the schema. All field values apart from `undefined` meet this requirement.

```ts
const schema = string().defined();
let schema = string().defined();

schema.cast(undefined); // TypeError

Expand All @@ -991,7 +991,7 @@ InferType<typeof schema>; // string
The opposite of `defined()` allows `undefined` values for the given type.

```ts
const schema = string().optional();
let schema = string().optional();

schema.cast(undefined); // undefined

Expand Down Expand Up @@ -1610,7 +1610,7 @@ Object schema come with a default value already set, which "builds" out the obje
sets any defaults for fields:

```js
const schema = object({
let schema = object({
name: string().default(''),
});

Expand All @@ -1623,7 +1623,7 @@ one gotcha! though. For nested object schema that are optional but include non o
may fail in unexpected ways:

```js
const schema = object({
let schema = object({
id: string().required(),
names: object({
first: string().required(),
Expand Down Expand Up @@ -1685,13 +1685,13 @@ fields.
Create a new schema from a subset of the original's fields.

```js
const person = object({
let person = object({
age: number().default(30).required(),
name: string().default('pat').required(),
color: string().default('red').required(),
});

const nameAndAge = person.pick(['name', 'age']);
let nameAndAge = person.pick(['name', 'age']);
nameAndAge.getDefault(); // => { age: 30, name: 'pat'}
```

Expand All @@ -1700,13 +1700,13 @@ nameAndAge.getDefault(); // => { age: 30, name: 'pat'}
Create a new schema with fields omitted.

```js
const person = object({
let person = object({
age: number().default(30).required(),
name: string().default('pat').required(),
color: string().default('red').required(),
});

const nameAndAge = person.omit(['color']);
let nameAndAge = person.omit(['color']);
nameAndAge.getDefault(); // => { age: 30, name: 'pat'}
```

Expand Down
Loading