<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def check(self):
 """Check for validity. :raises ValueError: - if not all lines are as long as the :attr:`number of needles <AYABInterface.machines.Machine.number_of_needles>` - if the contents of the rows are not :attr:`needle positions <AYABInterface.machines.Machine.needle_positions>` """

        # TODO: This violates the law of demeter.
        #       The architecture should be changed that this check is either
        #       performed by the machine or by the unity of machine and
        #       carriage.
        expected_positions = self._machine.needle_positions
        expected_row_length = self._machine.number_of_needles
        for row_index, row in enumerate(self._rows):
            if len(row) != expected_row_length:
                message = _ROW_LENGTH_ERROR_MESSAGE.format(
                    row_index, len(row), expected_row_length)
                raise ValueError(message)
            for needle_index, needle_position in enumerate(row):
                if needle_position not in expected_positions:
                    message = _NEEDLE_POSITION_ERROR_MESSAGE.format(
                        row_index, needle_index, repr(needle_position),
                        ", ".join(map(repr, expected_positions)))
                    raise ValueError(message)