Skip to content

Latest commit

 

History

History
 
 

validator

AMP HTML ⚡ Validator

A validator for the AMP HTML format.

If you just want to validate a page, please see our documentation over at amp.dev.

Chrome Extension

Please see js/chromeextension/README.md.

Visual Studio Code Extension

An extension for Visual Studio Code VSCode marketplace

Command Line Tool and Node.js API

Please see js/nodejs/README.md.

Web UI

Please see js/webui/README.md.

JSON

The validator rules are exported in the JSON format and hosted on: https://cdn.ampproject.org/v0/validator.json

The JSON rules are provided on best-effort basis and it's not recommended to rely on them in a production environment.

Building a Custom Validator

This is only useful for development - e.g. when making changes to js/engine/validator.js or when authoring an AMP extension, and it's rough around the edges. Below are instructions for Linux Ubuntu 14.

Installation

Linux

Install these packages using apt-get:

  • npm
  • openjdk-7-jre
  • protobuf-compiler
  • python3
  • python3-pip

Then pip3 install protobuf.

In addition, install Node.js v4.4.2. E.g., by downloading or by using a package manager or by using NVM.

OSX

Dependencies:

  • npm

  • homebrew

  • python 3 (e.g. these instructions)

    • protobuf

      pip3 install --user protobuf
    • openjdk-7-jre

      Install openjdk, then symlink the system Java wrappers to find it:

      brew install openjdk
      sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk

Usage

Then, run python build.py. This creates dist/validator_minified.js, which is equivalent to the validator deployed at cdn.ampproject.org. You may now use the --validator_js command line flag to amphtml-validator to use this validator.

For use for testing with extensions, you can simply run python build.py to run all of the validator tests in the amphtml repo. To create/update validator-*.out files that are used in the test, run python build.py --update_tests.

$ amphtml-validator --validator_js dist/validator_minified.js testdata/feature_tests/several_errors.html
testdata/feature_tests/several_errors.html:23:2 The attribute 'charset' may not appear in tag 'meta name= and content='.
testdata/feature_tests/several_errors.html:26:2 The tag 'script' is disallowed except in specific forms.
testdata/feature_tests/several_errors.html:32:2 The mandatory attribute 'height' is missing in tag 'amp-img'. (see https://amp.dev/documentation/components/amp-img)
testdata/feature_tests/several_errors.html:34:2 The attribute 'width' in tag 'amp-ad' is set to the invalid value '100%'. (see https://amp.dev/documentation/components/amp-ad)
...

Building on MacOS

Note: This is for building the validator from source. If you are simply running validator tests for extensions, see the Installation steps instead.

  • Download protobuf with brew install protobuf via homebrew.
  • Use pip to pip install google and pip install protobuf. If you don't have pip, you can get it either via brew install python or get-pip.py.
  • If your npm is out of date, run npm i -g npm to update it.

To verify that you have the necessary prerequisites, run and verify:

$ protoc --version
libprotoc 3.5.1

and

$ python
>>> import google.protobuf
>>>

Now cd amphtml/validator and run python build.py.