Skip to content

Commit ffbd85f

Browse files
committed
fix: 🐛 CR comments
1 parent a5cbe7e commit ffbd85f

4 files changed

Lines changed: 28 additions & 76 deletions

File tree

packages/plugin-validation/README.md

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,10 @@ registerCustomElement({
192192
Full TypeScript support is included:
193193

194194
```typescript
195-
import { validationPlugin, type ValidationPluginOptions } from '@merkur/plugin-validation';
196195
import { s, type Infer } from '@esmj/schema';
196+
import { defineWidget } from '@merkur/core';
197+
import { componentPlugin } from '@merkur/plugin-component';
198+
import { validationPlugin, type ValidationPluginOptions } from '@merkur/plugin-validation';
197199

198200
const propsSchema = s.object({
199201
userId: s.string(),
@@ -212,47 +214,4 @@ export default defineWidget({
212214
validationPlugin(options),
213215
],
214216
});
215-
```
216-
217-
## Custom Element Integration
218-
219-
When using with `@merkur/integration-custom-element`, the validation plugin provides a cleaner approach for parsing HTML attributes to widget props. Instead of defining individual `attributesParser` functions, you can define a schema with coercion that handles both validation and type conversion:
220-
221-
```javascript
222-
import { registerCustomElement } from '@merkur/integration-custom-element';
223-
import { componentPlugin } from '@merkur/plugin-component';
224-
import { validationPlugin } from '@merkur/plugin-validation';
225-
import { s } from '@esmj/schema';
226-
227-
// Schema with coercion - automatically converts string attributes to correct types
228-
const propsSchema = s.object({
229-
userId: s.string(),
230-
count: s.cast.number(), // Casts "42" → 42
231-
enabled: s.cast.boolean(), // Casts "true" → true
232-
config: s.cast.json(s.object({ apiUrl: s.string() })), // Parses JSON strings automatically
233-
});
234-
235-
const widgetDefinition = {
236-
name: 'my-widget',
237-
version: '1.0.0',
238-
$plugins: [
239-
componentPlugin,
240-
validationPlugin({ props: propsSchema }),
241-
],
242-
// ...
243-
};
244-
245-
registerCustomElement({
246-
widgetDefinition,
247-
observedAttributes: ['user-id', 'count', 'enabled', 'config'],
248-
});
249-
```
250-
251-
```html
252-
<my-widget
253-
user-id="abc123"
254-
count="42"
255-
enabled="true"
256-
config='{"apiUrl": "https://api.example.com"}'
257-
></my-widget>
258-
```
217+
```

packages/plugin-validation/src/__tests__/indexSpec.js

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ describe('validationPlugin', () => {
2323
});
2424

2525
const widget = await createMerkurWidget({
26-
$plugins: [
27-
componentPlugin,
28-
() => validationPlugin({ props: propsSchema }),
29-
],
26+
$plugins: [componentPlugin, validationPlugin({ props: propsSchema })],
3027
name: 'test-widget',
3128
version: '1.0.0',
3229
props: { name: 'test' },
@@ -43,10 +40,7 @@ describe('validationPlugin', () => {
4340
});
4441

4542
const widget = await createMerkurWidget({
46-
$plugins: [
47-
componentPlugin,
48-
() => validationPlugin({ props: propsSchema }),
49-
],
43+
$plugins: [componentPlugin, validationPlugin({ props: propsSchema })],
5044
name: 'test-widget',
5145
version: '1.0.0',
5246
props: {},
@@ -111,7 +105,7 @@ describe('validationPlugin', () => {
111105
const throwWidget = await createMerkurWidget({
112106
$plugins: [
113107
componentPlugin,
114-
() => validationPlugin({ props: propsSchema, onError: null }),
108+
validationPlugin({ props: propsSchema, onError: null }),
115109
],
116110
name: 'throw-widget',
117111
version: '1.0.0',
@@ -138,8 +132,7 @@ describe('validationPlugin', () => {
138132
const customWidget = await createMerkurWidget({
139133
$plugins: [
140134
componentPlugin,
141-
() =>
142-
validationPlugin({ props: propsSchema, onError: customHandler }),
135+
validationPlugin({ props: propsSchema, onError: customHandler }),
143136
],
144137
name: 'custom-widget',
145138
version: '1.0.0',
@@ -179,7 +172,7 @@ describe('validationPlugin', () => {
179172
const widget = await createMerkurWidget({
180173
$plugins: [
181174
componentPlugin,
182-
() => validationPlugin({ props: propsSchema, onError: null }),
175+
validationPlugin({ props: propsSchema, onError: null }),
183176
],
184177
name: 'test-widget',
185178
version: '1.0.0',
@@ -196,7 +189,7 @@ describe('validationPlugin', () => {
196189
const widget = await createMerkurWidget({
197190
$plugins: [
198191
componentPlugin,
199-
() => validationPlugin({ props: propsSchema, onError: null }),
192+
validationPlugin({ props: propsSchema, onError: null }),
200193
],
201194
name: 'test-widget',
202195
version: '1.0.0',
@@ -213,7 +206,7 @@ describe('validationPlugin', () => {
213206
const widget = await createMerkurWidget({
214207
$plugins: [
215208
componentPlugin,
216-
() => validationPlugin({ props: propsSchema, onError: null }),
209+
validationPlugin({ props: propsSchema, onError: null }),
217210
],
218211
name: 'test-widget',
219212
version: '1.0.0',
@@ -223,7 +216,7 @@ describe('validationPlugin', () => {
223216
},
224217
});
225218

226-
await expect(widget.mount()).resolves.not.toThrow();
219+
await expect(widget.mount()).rejects.toThrow();
227220
});
228221
});
229222

@@ -235,10 +228,7 @@ describe('validationPlugin', () => {
235228
});
236229

237230
const widget = await createMerkurWidget({
238-
$plugins: [
239-
componentPlugin,
240-
() => validationPlugin({ props: propsSchema }),
241-
],
231+
$plugins: [componentPlugin, validationPlugin({ props: propsSchema })],
242232
name: 'test-widget',
243233
version: '1.0.0',
244234
props: { name: 'test', count: 42 },
@@ -270,7 +260,7 @@ describe('validationPlugin', () => {
270260
const widget = await createMerkurWidget({
271261
$plugins: [
272262
componentPlugin,
273-
() => validationPlugin({ props: propsSchema, onError: null }),
263+
validationPlugin({ props: propsSchema, onError: null }),
274264
],
275265
name: 'test-widget',
276266
version: '1.0.0',
@@ -305,7 +295,7 @@ describe('validationPlugin', () => {
305295
const widget = await createMerkurWidget({
306296
$plugins: [
307297
componentPlugin,
308-
() => validationPlugin({ props: propsSchema, onError: null }),
298+
validationPlugin({ props: propsSchema, onError: null }),
309299
],
310300
name: 'test-widget',
311301
version: '1.0.0',

packages/plugin-validation/src/index.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export function validationPlugin(options = {}) {
5555
);
5656
}
5757

58-
return {
58+
return () => ({
5959
async setup(widget) {
6060
widget.$in.validation = {
6161
props,
@@ -81,7 +81,7 @@ export function validationPlugin(options = {}) {
8181

8282
return widget;
8383
},
84-
};
84+
});
8585
}
8686

8787
/**
@@ -156,6 +156,8 @@ async function setPropsHook(widget, originalSetProps, propsSetter) {
156156
if (!result.success) {
157157
handleValidationError(widget, result);
158158
} else {
159+
widget.props = result.data; // Update widget.props with validated/transformed data
160+
159161
// Call original setProps with result.data which is the validated and potentially transformed props
160162
return originalSetProps(result.data);
161163
}
@@ -170,14 +172,13 @@ async function setPropsHook(widget, originalSetProps, propsSetter) {
170172
*/
171173
async function mountHook(widget, originalMount, ...rest) {
172174
// Validate initial props
173-
if (widget.props && Object.keys(widget.props).length > 0) {
174-
const result = validateProps(widget, widget.props);
175-
176-
if (!result.success) {
177-
handleValidationError(widget, result);
178-
} else {
179-
widget.props = result.data;
180-
}
175+
const propsToValidate = widget.props ?? {};
176+
const result = validateProps(widget, propsToValidate);
177+
178+
if (!result.success) {
179+
handleValidationError(widget, result);
180+
} else {
181+
widget.props = result.data;
181182
}
182183

183184
return originalMount(...rest);

website/docs/validation-plugin.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ registerCustomElement({
198198
Full TypeScript support is included:
199199

200200
```typescript
201+
import { defineWidget } from '@merkur/core';
202+
import { componentPlugin } from '@merkur/plugin-component';
201203
import { validationPlugin, type ValidationPluginOptions } from '@merkur/plugin-validation';
202204
import { s, type Infer } from '@esmj/schema';
203205

0 commit comments

Comments
 (0)