The CSS Validator is a tool for validating CSS stylesheets, built on top of the official W3C CSS Validator (CSS Validator). It allows you to check if your CSS adheres to defined standards (such as CSS3), identifying errors and warnings efficiently. This project provides a simple API that can be run locally or in a CI/CD environment, supporting CSS validation via HTTP requests.
The project is packaged as a Docker image, making it easy to deploy in various environments. It also integrates with Semantic Release for automated versioning and publishing to Docker Hub.
- CSS validation based on W3C standards.
- Support for different validation profiles (e.g.,
css3svg
). - JSON responses for easy integration with other tools.
- Automated testing with PHPUnit.
- Automatic publishing of versioned Docker images to Docker Hub.
- Docker: Ensure Docker is installed on your system. You can install it by following the instructions at docker.com.
- curl (optional): For testing the API via the command line.
-
Run the container:
docker run --rm -p 8080:8080 crisnao2/css-validator:latest
This starts the container and maps port
8080
on the container to port8080
on the host. -
Test the API:
The API will be available at
http://localhost:8080
. See the usage examples below.
The API accepts the following parameters via POST
requests:
Parameter | Description | Possible Values | Default |
---|---|---|---|
css |
The CSS code to validate. | String (e.g., body { color: blue; } ) |
Required |
profile |
The CSS validation profile to use. | css1 , css2 , css21 , css3 , css3svg , etc. |
css3svg |
lang |
The language for error messages. | en , pt-BR , de , etc. |
en |
You can build a custom version of the Docker image by adjusting the build behavior with the following arguments:
Argument | Description | Possible Values | Default |
---|---|---|---|
RUN_TESTS |
Determines if tests should be run during the build (useful for local builds). | true , false |
true |
REMOVE_TEST_FILES |
Determines if test-related files (e.g., composer , vendor/bin/phpunit ) should be removed the image. |
true , false |
false |
-
Clone the Repository:
git clone https://github.com/crisnao2/css_validator.git cd css_validator
-
Build the Custom Image:
-
To build the image without running tests and keeping test files:
docker build --build-arg RUN_TESTS=false --build-arg REMOVE_TEST_FILES=false -t css-validator:custom .
-
To build an optimized image (without test files):
docker build --build-arg RUN_TESTS=false --build-arg REMOVE_TEST_FILES=true -t css-validator:custom .
-
-
Run the Image:
docker run -d -p 8080:8080 css-validator:custom
The API accepts POST
requests with the parameters described above. Below are examples of validating invalid and valid CSS using curl
.
curl -X POST \
-d "css='body { color: blue; } p { margin: ; }'" \
-d "profile=css3svg" \
-d "lang=en" \
http://localhost:8080
{
"cssvalidation": {
"uri": "file:/tmp/css_xxx.css",
"checkedby": "http://jigsaw.w3.org/css-validator/",
"csslevel": "css3",
"date": "2025-04-06T12:35:13Z",
"validity": false,
"errors": [
{
"source": "file:/tmp/css_xxx.css",
"line": 1,
"context": "p",
"type": "generator.unrecognize",
"errortype": "parse-error",
"errorsubtype": "unrecognized",
"message": "Value Error : margin (nullbox.html#propdef-margin)\n\nParse Error"
}
],
"warnings": [],
"result": {
"errorcount": 1,
"warningcount": 0
}
}
}
curl -X POST \
-d "css='body { color: blue; } p { margin: 10px; }'" \
-d "profile=css3svg" \
-d "lang=en" \
http://localhost:8080
{
"cssvalidation": {
"uri": "file:/tmp/css_xxx.css",
"checkedby": "http://jigsaw.w3.org/css-validator/",
"csslevel": "css3",
"date": "2025-04-06T12:35:13Z",
"validity": true,
"errors": [],
"warnings": [],
"result": {
"errorcount": 0,
"warningcount": 0
}
}
}
The project includes automated tests using PHPUnit. To run the tests locally:
-
Build the image with test files:
docker build --build-arg RUN_TESTS=true --build-arg REMOVE_TEST_FILES=true -t css-validator:test .
-
Run the tests:
docker run --rm css-validator:test sh -c "composer install --no-interaction && ./vendor/bin/phpunit --configuration phpunit.xml"
Tests are automatically executed in the CI pipeline (GitHub Actions) for every push or pull request to the main
branch.
Contributions are welcome! Follow the steps below to contribute:
-
Fork the repository.
-
Create a branch for your feature or bug fix:
git checkout -b feature/new-feature
-
Make your changes and commit them:
git commit -m "feat: add new feature"
-
Push your changes to the remote repository:
git push origin feature/new-feature
-
Open a Pull Request on GitHub.
The project uses Semantic Release for automated versioning. Follow the Angular commit conventions to ensure proper versioning:
feat:
for new features (increments the minor version).fix:
for bug fixes (increments the patch version).BREAKING CHANGE:
for breaking changes (increments the major version).
This project is licensed under the GPL-3.0 License.
Cristiano Soares
- Website: comerciobr.com
If you encounter any problems or have any questions, please open an issue on the GitHub repository.