Skip to content

Commit

Permalink
feat: add support for label method
Browse files Browse the repository at this point in the history
  • Loading branch information
simonguo committed Apr 9, 2024
1 parent e315aec commit caddc4d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Schema for data modeling & validation
- [`when(condition: (schemaSpec: SchemaDeclaration<DataType, ErrorMsgType>) => Type)`](#whencondition-schemaspec-schemadeclarationdatatype-errormsgtype--type)
- [`check(value: ValueType, data?: DataType):CheckResult`](#checkvalue-valuetype-data-datatypecheckresult)
- [`checkAsync(value: ValueType, data?: DataType):Promise<CheckResult>`](#checkasyncvalue-valuetype-data-datatypepromisecheckresult)
- [`label(label: string)`](#labellabel-string)
- [StringType(errorMessage?: string)](#stringtypeerrormessage-string)
- [`isEmail(errorMessage?: string)`](#isemailerrormessage-string)
- [`isURL(errorMessage?: string)`](#isurlerrormessage-string)
Expand Down Expand Up @@ -515,6 +516,23 @@ type.checkAsync(1).then(checkResult => {
});
```

#### `label(label: string)`

Overrides the key name in error messages.

```js
MixedType().label('Username');
```

Eg:

```js
SchemaModel({
first_name: StringType().label('First name'),
age: NumberType().label('Age')
});
```

### StringType(errorMessage?: string)

Define a string type. Supports all the same methods as [MixedType](#mixedtype).
Expand Down
17 changes: 15 additions & 2 deletions src/MixedType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class MixedType<ValueType = any, DataType = any, E = ErrorMessageType, L
protected emptyAllowed = false;
protected rules: RuleType<ValueType, DataType, E | string>[] = [];
protected priorityRules: RuleType<ValueType, DataType, E | string>[] = [];
protected fieldLabel?: string;

schemaSpec: SchemaDeclaration<DataType, E>;
value: any;
Expand All @@ -43,7 +44,9 @@ export class MixedType<ValueType = any, DataType = any, E = ErrorMessageType, L
if (this.required && !checkRequired(value, this.trim, this.emptyAllowed)) {
return {
hasError: true,
errorMessage: formatErrorMessage(this.requiredMessage, { name: fieldName })
errorMessage: formatErrorMessage(this.requiredMessage, {
name: this.fieldLabel || fieldName
})
};
}

Expand All @@ -70,7 +73,9 @@ export class MixedType<ValueType = any, DataType = any, E = ErrorMessageType, L
if (this.required && !checkRequired(value, this.trim, this.emptyAllowed)) {
return Promise.resolve({
hasError: true,
errorMessage: formatErrorMessage(this.requiredMessage, { name: fieldName })
errorMessage: formatErrorMessage(this.requiredMessage, {
name: this.fieldLabel || fieldName
})
});
}

Expand Down Expand Up @@ -160,6 +165,14 @@ export class MixedType<ValueType = any, DataType = any, E = ErrorMessageType, L
);
return this;
}

/**
* Overrides the key name in error messages.
*/
label(label: string) {
this.fieldLabel = label;
return this;
}
}

export default function getMixedType<DataType = any, E = ErrorMessageType>() {
Expand Down
18 changes: 15 additions & 3 deletions test/MixedTypeSpec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const chai = require('chai');
const schema = require('../src');
import chai, { expect } from 'chai';
import * as schema from '../src';

chai.should();
const { StringType, SchemaModel, NumberType, ArrayType, MixedType } = schema;

Expand Down Expand Up @@ -460,4 +460,16 @@ describe('#MixedType', () => {
}
}, 100);
});

it('Should use label to override the field name in the error message', () => {
const schema = SchemaModel({
first_name: StringType().label('First Name').isRequired(),
age: NumberType().label('Age').isRequired()
});

expect(schema.check({})).to.deep.equal({
first_name: { hasError: true, errorMessage: 'First Name is a required field' },
age: { hasError: true, errorMessage: 'Age is a required field' }
});
});
});

0 comments on commit caddc4d

Please sign in to comment.