Skip to content

Commit

Permalink
feat: move handle validation to product updation
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed May 13, 2024
1 parent a958e4c commit b4307c2
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { normalizeHandle } from "../normalize-handle"
import { toHandle } from "../to-handle"

describe("normalizeHandle", function () {
it("should generate URL friendly handles", function () {
Expand Down Expand Up @@ -38,7 +38,7 @@ describe("normalizeHandle", function () {
]

expectations.forEach((expectation) => {
expect(normalizeHandle(expectation.input)).toEqual(expectation.output)
expect(toHandle(expectation.input)).toEqual(expectation.output)
})
})
})
2 changes: 1 addition & 1 deletion packages/core/utils/src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ export * from "./transaction"
export * from "./trim-zeros"
export * from "./upper-case-first"
export * from "./wrap-handler"
export * from "./normalize-handle"
export * from "./to-handle"
export * from "./validate-handle"
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { kebabCase } from "./to-kebab-case"

/**
* Helper method to normalize entity "handle" to be URL
* friendly.
* Helper method to create a to be URL friendly "handle" from
* a string value.
*
* - Works by converting the value to lowercase
* - Splits and remove accents from characters
* - Removes all unallowed characters like a '"%$ and so on.
*/
export const normalizeHandle = (value: string): string => {
export const toHandle = (value: string): string => {
return kebabCase(
value
.toLowerCase()
Expand Down
5 changes: 2 additions & 3 deletions packages/medusa/src/api-v2/admin/collections/validators.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { HandleValidator } from "../../utils/common-validators"
import {
createFindParams,
createOperatorMap,
Expand Down Expand Up @@ -30,13 +29,13 @@ export const AdminGetCollectionsParams = createFindParams({
export type AdminCreateCollectionType = z.infer<typeof AdminCreateCollection>
export const AdminCreateCollection = z.object({
title: z.string(),
handle: HandleValidator,
handle: z.string().optional(),
metadata: z.record(z.unknown()).optional(),
})

export type AdminUpdateCollectionType = z.infer<typeof AdminUpdateCollection>
export const AdminUpdateCollection = z.object({
title: z.string().optional(),
handle: HandleValidator,
handle: z.string().optional(),
metadata: z.record(z.unknown()).optional(),
})
7 changes: 2 additions & 5 deletions packages/medusa/src/api-v2/admin/products/validators.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { ProductStatus } from "@medusajs/utils"
import { z } from "zod"
import {
GetProductsParams,
HandleValidator,
} from "../../utils/common-validators"
import { GetProductsParams } from "../../utils/common-validators"
import {
createFindParams,
createOperatorMap,
Expand Down Expand Up @@ -182,7 +179,7 @@ export const AdminCreateProduct = z
discountable: z.boolean().optional().default(true),
images: z.array(z.object({ url: z.string() })).optional(),
thumbnail: z.string().optional(),
handle: HandleValidator,
handle: z.string().optional(),
status: statusEnum.optional().default(ProductStatus.DRAFT),
type_id: z.string().nullable().optional(),
collection_id: z.string().nullable().optional(),
Expand Down
8 changes: 0 additions & 8 deletions packages/medusa/src/api-v2/utils/common-validators/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,3 @@ export const OptionalBooleanValidator = z.preprocess(
(val: any) => optionalBooleanMapper.get(val?.toLowerCase()),
z.boolean().optional()
)

/**
* Validates entity handle to have URL-safe characters
*/
export const HandleValidator = z
.string()
.regex(/^[a-z0-9]+(?:-[a-z0-9]+)*$/)
.optional()
4 changes: 2 additions & 2 deletions packages/modules/product/src/models/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import {
createPsqlIndexStatementHelper,
DALUtils,
generateEntityId,
kebabCase,
ProductUtils,
Searchable,
toHandle,
} from "@medusajs/utils"
import ProductCategory from "./product-category"
import ProductCollection from "./product-collection"
Expand Down Expand Up @@ -216,7 +216,7 @@ class Product {
this.collection_id ??= this.collection?.id ?? null

if (!this.handle && this.title) {
this.handle = kebabCase(this.title)
this.handle = toHandle(this.title)
}
}
}
Expand Down
18 changes: 15 additions & 3 deletions packages/modules/product/src/services/product-module-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
ProductStatus,
promiseAll,
removeUndefined,
isValidHandle,
} from "@medusajs/utils"
import {
ProductCategoryEventData,
Expand Down Expand Up @@ -1315,9 +1316,13 @@ export default class ProductModuleService<
sharedContext
)) as ProductTypes.CreateProductDTO

if (!productData.handle && productData.title) {
productData.handle = kebabCase(productData.title)
}
/**
* We are already computing handle from title in model. Do we
* need here again?
*/
// if (!productData.handle && productData.title) {
// productData.handle = kebabCase(productData.title)
// }

if (!productData.status) {
productData.status = ProductStatus.DRAFT
Expand All @@ -1339,6 +1344,13 @@ export default class ProductModuleService<
productData.discountable = false
}

if (productData.handle && !isValidHandle(productData.handle)) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"Invalid product handle. It must contain URL safe characters"
)
}

if (productData.tags?.length && productData.tags.some((t) => !t.id)) {
const dbTags = await this.productTagService_.list(
{
Expand Down

0 comments on commit b4307c2

Please sign in to comment.