@@ -9,13 +9,13 @@ function validateTitle({ title, InvalidPropertyError }) {
99//validate title length
1010
1111//validate and normalise product description
12- function validateDescription ( { descripton , InvalidPropertyError } ) {
13- if ( descripton . length < 50 ) {
12+ function validateDescription ( { description , InvalidPropertyError } ) {
13+ if ( description . length < 50 ) {
1414 throw new InvalidPropertyError (
1515 `A product's title must be at least 20 characters long.`
1616 )
1717 }
18- return descripton . charAt ( 0 ) . toUpperCase ( ) + descripton . slice ( 1 ) ;
18+ return description . charAt ( 0 ) . toUpperCase ( ) + description . slice ( 1 ) ;
1919}
2020
2121/**
@@ -153,46 +153,21 @@ const CalculateTotalReviews = (totalRatings) => {
153153}
154154
155155//calculate average rating
156- const calculateAverageRating = ( totalRatings ) => {
156+ const calculateAverageRating = ( totalRatings , resultingProductData ) => {
157157 if ( ! totalRatings || ! totalRatings . length ) return 0
158158 return totalRatings . reduce ( ( acc , curr , idx ) => acc + curr * ( idx + 1 ) , 0 ) / resultingProductData . totalReviews ;
159159}
160160
161- // validate uuid as ObjectId
162- const validateUUID = ( id , InvalidPropertyError ) => {
163- // const regex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
164- // console.log(id);
165- const regex = / ^ [ a - f A - F 0 - 9 ] { 24 } $ / ; // ObjectId validation regex
166- if ( ! regex . test ( id ) ) {
167- throw new InvalidPropertyError ( `Invalid product ObjectId.` )
161+ // validate uuid as ObjectId
162+ const validateObjectId = ( id , InvalidPropertyError ) => {
163+ if ( ! / ^ [ 0 - 9 a - f A - F ] { 24 } $ / . test ( id ) ) {
164+ throw new InvalidPropertyError ( "Invalid product id." ) ;
168165 }
169166 return id ;
170- }
171-
172- // validate rating model
173- const validateRatingModel = ( ratingModel , InvalidPropertyError ) => {
174- const { productId, userId, ratingValue } = ratingModel ;
175-
176- // validate IDs
177- const productUUID = validateUUID ( productId , InvalidPropertyError ) ;
178- const userUUID = validateUUID ( userId , InvalidPropertyError ) ;
179-
180- // validate rating value
181- const validRatingValues = [ 1 , 2 , 3 , 4 , 5 ] ;
182- if ( ! ratingValue || ! validRatingValues . includes ( ratingValue ) ) {
183- throw new InvalidPropertyError (
184- `Invalid rating value.`
185- )
186- }
187167
188- return {
189- productId : productUUID ,
190- userId : userUUID ,
191- ratingValue,
192- date : new Date ( ) . toISOString ( ) ,
193- } ;
194168}
195169
170+
196171//basic product validation
197172const basicProductValidation = ( { productData, errorHandlers } ) => {
198173
@@ -207,7 +182,7 @@ const basicProductValidation = ({ productData, errorHandlers }) => {
207182
208183 if ( ! productData . descripton ) {
209184 errors . push ( `Product descripton is required` ) ;
210- } else resultingProductData . descripton = validateDescription ( { descripton : productData . descripton , InvalidPropertyError } ) ;
185+ } else resultingProductData . description = validateDescription ( { description : productData . description , InvalidPropertyError } ) ;
211186
212187 if ( ! productData . price ) {
213188 errors . push ( `Product price is required` ) ;
@@ -283,13 +258,13 @@ const basicProductValidation = ({ productData, errorHandlers }) => {
283258 resultingProductData . latestRating = null ;
284259 resultingProductData . rateAverage = 0 ;
285260 resultingProductData . lastModified = new Date ( ) . toISOString ( ) ;
286- resultingProductData . instock = Boolean ( resultingProductData . inventory && resultingProductData . inventory > 0 )
261+ resultingProductData . instock = Boolean ( resultingProductData . inventory )
287262 resultingProductData . brands = productData . brands || [ ] ;
288263
289264 if ( errors . length ) {
290265 throw new RequiredParameterError ( errors . join ( ', ' ) ) ;
291266 }
292- console . log ( "end of validations : " ) ;
267+ console . log ( "successfully validated product : " ) ;
293268 return resultingProductData ;
294269}
295270
@@ -298,7 +273,11 @@ module.exports = () => {
298273 basicProductValidation,
299274 CalculateTotalReviews,
300275 calculateAverageRating,
301- validateUUID,
302- validateRatingModel
276+ validateObjectId,
277+ validateTitle,
278+ validateNumber,
279+ validateCategory,
280+ validateColors,
281+ validateDescription
303282 } )
304283}
0 commit comments