File tree Expand file tree Collapse file tree 3 files changed +21
-11
lines changed
Expand file tree Collapse file tree 3 files changed +21
-11
lines changed Original file line number Diff line number Diff line change @@ -6,17 +6,8 @@ import Button from '../../components/Button';
66import { createFormSchemaValidator } from '/client/validation/form' ;
77import { VinNumberFields } from '/imports/api/vinNumbers/collection' ;
88import { 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
2011class 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 >
Original file line number Diff line number Diff line change 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 ;
Original file line number Diff line number Diff line change 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+ } ) ;
You can’t perform that action at this time.
0 commit comments