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

feat: add default method for Strings and Numbers #846

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions src/NopeNumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ export class NopeNumber extends NopePrimitive<number> {
return this;
}

public default(value: number) {
const rule: Rule<any> = (entry) => {
if (this.isEmpty(entry)) {
return value;
}

return entry;
};

return this.test(rule);
}

public validate(entry?: any, context?: Record<string | number, unknown>): string | undefined {
const value = !!entry ? Number(entry) : entry;

Expand Down
12 changes: 12 additions & 0 deletions src/NopeString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,16 @@ export class NopeString extends NopePrimitive<string> {

return this.test(rule);
}

public default(value: string) {
const rule: Rule<string> = (entry) => {
if (this.isEmpty(entry)) {
return value;
}

return entry as string;
};

return this.test(rule);
}
}
29 changes: 29 additions & 0 deletions src/__tests__/NopeNumber.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,33 @@ describe('#NopeNumber', () => {
await validateSyncAndAsync(Nope.number().integer('integerErrorMessage'), '12', undefined);
});
});

describe('#default', () => {
it('should return a default value when entry is empty', async () => {
for (const { schema, value, expected } of [
{
schema: Nope.number().default(5),
value: 0.1,
expected: 0.1,
},
{
schema: Nope.number().default(10),
value: undefined,
expected: 10,
},
{
schema: Nope.number().default(15),
value: null,
expected: 15,
},
{
schema: Nope.number().default(101010),
value: 20,
expected: 20,
},
]) {
await validateSyncAndAsync(schema, value, expected);
}
});
});
});
31 changes: 31 additions & 0 deletions src/__tests__/NopeString.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,4 +324,35 @@ describe('#NopeString', () => {
}
});
});

describe('#default', () => {
it('should return a default value when entry is empty', async () => {
const ERR_MSG = 'error-message';

for (const { schema, value, expected } of [
{
schema: Nope.string().default('default-value').email(ERR_MSG),
value: '',
expected: 'default-value',
},
{
schema: Nope.string().default('default-value-trim').trim().email(ERR_MSG),
value: ' ',
expected: 'default-value-trim',
},
{
schema: Nope.string().default('_ftonato ').email(ERR_MSG),
value: undefined,
expected: '_ftonato ',
},
{
schema: Nope.string().trim().default('will-not-assume-this-value').email(ERR_MSG),
value: ' aaa ',
expected: 'aaa',
},
]) {
await validateSyncAndAsync(schema, value, expected);
}
});
});
});