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

fix: fix judgment null error #69

Merged
merged 2 commits into from
Apr 19, 2023
Merged
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
4 changes: 3 additions & 1 deletion src/utils/formatErrorMessage.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import isEmpty from './isEmpty';

/**
* formatErrorMessage('${name} is a required field', {name: 'email'});
* output: 'email is a required field'
*/
export default function formatErrorMessage<E>(errorMessage?: string | E, params?: any) {
if (typeof errorMessage === 'string') {
return errorMessage.replace(/\$\{\s*(\w+)\s*\}/g, (_, key) => {
return params?.[key] || `[${key}]`;
return isEmpty(params?.[key]) ? `[${key}]` : params?.[key];
});
}

Expand Down
14 changes: 9 additions & 5 deletions test/NumberTypeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,18 @@ describe('#NumberType', () => {
});

it('Should be within the range of optional values', () => {
let schemaData = { data: NumberType().range(10, 20) };
let schemaData = { data: NumberType().range(0, 20) };
let schema = new Schema(schemaData);
schema.checkForField('data', { data: 10 }).hasError.should.equal(false);
schema.checkForField('data', { data: 0 }).hasError.should.equal(false);
schema.checkForField('data', { data: 20 }).hasError.should.equal(false);
schema.checkForField('data', { data: 9 }).hasError.should.equal(true);
schema.checkForField('data', { data: -1 }).hasError.should.equal(true);
schema.checkForField('data', { data: 21 }).hasError.should.equal(true);
schema
.checkForField('data', { data: 9 })
.errorMessage.should.equal('data field must be between 10 and 20');
.checkForField('data', { data: -1 })
.errorMessage.should.equal('data field must be between 0 and 20');
schema
.checkForField('data', { data: 21 })
.errorMessage.should.equal('data field must be between 0 and 20');
});

it('Should be within the following value range: 1,2,3,4', () => {
Expand Down