Skip to content

Commit 5d845ee

Browse files
committed
Validate vin numbers
1 parent 2b8586f commit 5d845ee

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

client/pages/VinNumbers/AddVinForm.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,8 @@ import Button from '../../components/Button';
66
import {createFormSchemaValidator} from '/client/validation/form';
77
import {VinNumberFields} from '/imports/api/vinNumbers/collection';
88
import {addVin} from '/imports/api/vinNumbers/methods';
9+
import validateVin from './validateVin';
910

10-
// @todo #3:15min Add VIN validation function tests
11-
12-
// eslint-disable-next-line no-unused-vars
13-
function validateVin(vin) {
14-
const re = new RegExp('^[A-HJ-NPR-Z\\d]{8}[\\dX][A-HJ-NPR-Z\\d]{2}\\d{6}$');
15-
return vin.match(re);
16-
}
17-
18-
// @todo #3:10min Add VIN number validation
19-
// with the above validation function
2011
class AddVinForm extends React.Component {
2112
onCreate(vin) {
2213
const {reset} = this.props;
@@ -34,7 +25,7 @@ class AddVinForm extends React.Component {
3425
const {handleSubmit, submitting} = this.props;
3526
return (
3627
<Form onSubmit={handleSubmit(this.onCreate.bind(this))}>
37-
<Input name="value" label="VIN number" />
28+
<Input name="value" label="VIN number" validate={validateVin} />
3829
<Input name="notes" label="Notes" />
3930
<Button text="Save" type="submit" loading={submitting} />
4031
</Form>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const VIN_REGEX = new RegExp('^[A-HJ-NPR-Z\\d]{8}[\\dX][A-HJ-NPR-Z\\d]{2}\\d{6}$');
2+
3+
const validateVin = vin => (vin && vin.match(VIN_REGEX) ? undefined : 'Please enter a valid 17-digit VIN number');
4+
5+
export default validateVin;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import validateVin from './validateVin';
2+
import {expect} from 'chai';
3+
4+
describe('validateVin', () => {
5+
it('should validate standard VIN numbers', () => {
6+
expect(validateVin('5GZCZ43D13S812715')).to.equal(undefined);
7+
});
8+
it('should not validate invalid VIN numbers', () => {
9+
validateVin('5GZCZ43D13S81271').should.equal('Please enter a valid 17-digit VIN number');
10+
});
11+
it('should not validate `undefined`', () => {
12+
validateVin(undefined).should.equal('Please enter a valid 17-digit VIN number');
13+
});
14+
});

0 commit comments

Comments
 (0)