Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions assets/scss/custom.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
--alert-best-practice-background-color-highlight: rgba(0, 164, 0, 0.15);
--alert-best-practice-foreground-color: var(--ifm-color-success-contrast-foreground);
--alert-best-practice-border-color: #a3148c;

--alert-added-version-background-color: #ff7400;
--alert-added-version-foreground-color: #190700;
--alert-added-version-border-color: #ff4d00;
}

/* For readability concerns, you should choose a lighter palette in dark mode. */
Expand All @@ -37,6 +41,10 @@
--alert-best-practice-background-color-highlight: rgba(0, 164, 0, 0.15);
--alert-best-practice-foreground-color: var(--ifm-color-success-contrast-foreground);
--alert-best-practice-border-color: #e617c3;

--alert-added-version-background-color: #ff7400;
--alert-added-version-foreground-color: #190700;
--alert-added-version-border-color: #ff4d00;
}

[data-theme='dark'],
Expand Down
9 changes: 9 additions & 0 deletions blog/2025-07-03-simple-normalizer-verifier.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: "Simple Normalizer: JSON Types Verifier"
authors: [jannik]
tags: [simple-normalizer]
---

[Simple Normalizer now verifies][1] that all of your simple object normalizers are returning the correct types.

[1]: /docs/php/symfony/simple-normalizer/#returning-invalid-values
33 changes: 33 additions & 0 deletions docs/php/symfony/simple-normalizer/index.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {LinkList} from "../../../../src/components/Link/LinkList";
import {AddedBadge} from "../../../../src/components/AddedBadge";

# Simple Normalizer

Expand Down Expand Up @@ -155,3 +156,35 @@ public function normalize (object $value, array $context, SimpleNormalizer $norm
// ...
}
```


## Returning Invalid Values

<AddedBadge package="21torr/simple-normalizer" version="1.4.0" />

All values in the generated JSON must be of valid JSON types, that includes: scalars, arrays and empty objects.

:::info
Empty objects are allowed, as JSON encoding an empty array[^1] will always encode to `[]`, but `json_encode(new stdClass()) === "{}"`.
:::

However, due to performance reasons, the normalizer does not check for valid values. If you return an invalid value, it will be silently ignored (and in the end passed to `json_encode()`):

```php
class MyNormalizer implements SimpleObjectNormalizerInterface
{
public function normalize (object $value, array $context, SimpleNormalizer $normalizer) : mixed
{
assert($value instanceof MyObject);

// this will be silently ignored
return new SomeObject();
}
}
```

This is never intended behavior and is now validated. This check is only enabled in `debug` mode, so you can still use the normalizer in production without performance overhead.



[^1]: PHP doesn't distinguish between empty arrays and empty objects, so `json_encode` can only encode to one of them. You can configure it with the constant `JSON_FORCE_OBJECT`, but that is a global setting, and most of the time you want to have different behavior in the same normalization process.
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
"write-translations": "docusaurus write-translations"
},
"dependencies": {
"@docusaurus/core": "^3.8.0",
"@docusaurus/faster": "^3.8.0",
"@docusaurus/preset-classic": "^3.8.0",
"@docusaurus/theme-classic": "^3.8.0",
"@docusaurus/core": "^3.8.1",
"@docusaurus/faster": "^3.8.1",
"@docusaurus/preset-classic": "^3.8.1",
"@docusaurus/theme-classic": "^3.8.1",
"@iconify/react": "^6.0.0",
"@mdx-js/react": "^3.1.0",
"clsx": "^2.1.1",
"prism-react-renderer": "^2.4.1",
Expand All @@ -23,8 +24,8 @@
"react-dom": "^19.1.0"
},
"devDependencies": {
"@docusaurus/module-type-aliases": "3.8.0",
"@docusaurus/types": "^3.8.0",
"@docusaurus/module-type-aliases": "3.8.1",
"@docusaurus/types": "^3.8.1",
"@tsconfig/docusaurus": "^2.0.3",
"@types/node": "^22.15.29",
"@types/react": "^19.1.6",
Expand Down
Loading