Commit 26b783ff authored by Leandro Melo's avatar Leandro Melo
Browse files

Introduce JSON validation

Support basic validation for JSON files according to the
draft http://tools.ietf.org/html/draft-zyp-json-schema-03

.

This is not a complete implementation yet, but it should
already be useful, since "type" verification along with
many of the attributes is done.

Change-Id: I364bc98dd92937c5e2ea9cba7e15ed8e03eb9beb
Reviewed-by: default avatarErik Verbruggen <erik.verbruggen@nokia.com>
parent 454e2c39
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (info@qt.nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at info@qt.nokia.com.
**
**************************************************************************/
#include "jsoncheck.h"
#include <parser/qmljsast_p.h>
#include <utils/qtcassert.h>
#include <QtCore/QDebug>
#include <QtCore/QLatin1String>
#include <QtCore/QRegExp>
#include <QtCore/QMap>
#include <cmath>
using namespace QmlJS;
using namespace QmlJS::AST;
using namespace QmlJS::StaticAnalysis;
using namespace Utils;
JsonCheck::JsonCheck(Document::Ptr doc)
: m_doc(doc)
, m_schema(0)
{
QTC_CHECK(m_doc->ast());
}
JsonCheck::~JsonCheck()
{}
QList<Message> JsonCheck::operator ()(JsonSchema *schema)
{
QTC_ASSERT(schema, return QList<Message>());
m_schema = schema;
m_analysis.push(AnalysisData());
processSchema(m_doc->ast());
const AnalysisData &analysis = m_analysis.pop();
return analysis.m_messages;
}
bool JsonCheck::preVisit(Node *ast)
{
if (!m_firstLoc.isValid()) {
if (ExpressionNode *expr = ast->expressionCast())
m_firstLoc = expr->firstSourceLocation();
}
m_analysis.push(AnalysisData());
return true;
}
void JsonCheck::postVisit(Node *)
{
const AnalysisData &previous = m_analysis.pop();
if (previous.m_messages.isEmpty())
analysis()->m_hasMatch = true;
else
analysis()->m_messages.append(previous.m_messages);
analysis()->m_ranking += previous.m_ranking;
}
bool JsonCheck::visit(ObjectLiteral *ast)
{
if (!proceedCheck(JsonValue::Object, ast->lbraceToken))
return false;
analysis()->boostRanking();
const QStringList &properties = m_schema->properties();
if (properties.isEmpty())
return false;
QSet<QString> propertiesFound;
for (PropertyNameAndValueList *it = ast->properties; it; it = it->next) {
StringLiteralPropertyName *literalName = cast<StringLiteralPropertyName *>(it->name);
if (literalName) {
const QString &propertyName = literalName->id.toString();
if (m_schema->hasPropertySchema(propertyName)) {
analysis()->boostRanking();
propertiesFound.insert(propertyName);
// Sec. 5.2: "... each property definition's value MUST be a schema..."
m_schema->enterNestedPropertySchema(propertyName);
processSchema(it->value);
m_schema->leaveNestedSchema();
} else {
analysis()->m_messages.append(Message(ErrInvalidPropertyName,
literalName->firstSourceLocation(),
propertyName, QString(),
false));
}
} else {
analysis()->m_messages.append(Message(ErrStringValueExpected,
it->name->firstSourceLocation(),
QString(), QString(),
false));
}
}
QStringList missing;
foreach (const QString &property, properties) {
if (!propertiesFound.contains(property)) {
m_schema->enterNestedPropertySchema(property);
if (m_schema->required())
missing.append(property);
m_schema->leaveNestedSchema();
}
}
if (!missing.isEmpty()) {
analysis()->m_messages.append(Message(ErrMissingRequiredProperty,
ast->firstSourceLocation(),
missing.join(QLatin1String(", ")),
QString(),
false));
} else {
analysis()->boostRanking();
}
return false;
}
bool JsonCheck::visit(ArrayLiteral *ast)
{
if (!proceedCheck(JsonValue::Array, ast->firstSourceLocation()))
return false;
analysis()->boostRanking();
if (m_schema->hasItemSchema()) {
// Sec. 5.5: "When this attribute value is a schema... all the items in the array MUST
// be valid according to the schema."
m_schema->enterNestedItemSchema();
for (ElementList *element = ast->elements; element; element = element->next)
processSchema(element->expression);
m_schema->leaveNestedSchema();
} else if (m_schema->hasItemArraySchema()) {
// Sec. 5.5: "When this attribute value is an array of schemas... each position in the
// instance array MUST conform to the schema in the corresponding position for this array."
int current = 0;
const int arraySize = m_schema->itemArraySchemaSize();
for (ElementList *element = ast->elements; element; element = element->next, ++current) {
if (current < arraySize) {
if (m_schema->maybeEnterNestedArraySchema(current)) {
processSchema(element->expression);
m_schema->leaveNestedSchema();
} else {
Node::accept(element->expression, this);
}
} else {
// TODO: Handle additionalItems.
}
}
if (current < arraySize
|| (current > arraySize && !m_schema->hasAdditionalItems())) {
analysis()->m_messages.append(Message(ErrInvalidArrayValueLength,
ast->firstSourceLocation(),
QString::number(arraySize), QString(), false));
}
}
return false;
}
bool JsonCheck::visit(NullExpression *ast)
{
if (proceedCheck(JsonValue::Null, ast->firstSourceLocation()))
return false;
analysis()->boostRanking();
return false;
}
bool JsonCheck::visit(TrueLiteral *ast)
{
if (!proceedCheck(JsonValue::Boolean, ast->firstSourceLocation()))
return false;
analysis()->boostRanking();
return false;
}
bool JsonCheck::visit(FalseLiteral *ast)
{
if (!proceedCheck(JsonValue::Boolean, ast->firstSourceLocation()))
return false;
analysis()->boostRanking();
return false;
}
bool JsonCheck::visit(NumericLiteral *ast)
{
double dummy;
if (std::abs(std::modf(ast->value, &dummy)) > 0.000000001) {
if (!proceedCheck(JsonValue::Double, ast->firstSourceLocation()))
return false;
} else if (!proceedCheck(JsonValue::Int, ast->firstSourceLocation())) {
return false;
}
analysis()->boostRanking();
if (m_schema->hasMinimum()) {
double minValue = m_schema->minimum();
if (ast->value < minValue) {
analysis()->m_messages.append(Message(ErrLargerNumberValueExpected,
ast->firstSourceLocation(),
QString::number(minValue), QString(), false));
} else if (m_schema->hasExclusiveMinimum()
&& std::abs(ast->value - minValue) > 0.000000001) {
analysis()->m_messages.append(Message(ErrMinimumNumberValueIsExclusive,
ast->firstSourceLocation(),
QString(), QString(), false));
} else {
analysis()->boostRanking();
}
}
if (m_schema->hasMaximum()) {
double maxValue = m_schema->maximum();
if (ast->value > maxValue) {
analysis()->m_messages.append(Message(ErrSmallerNumberValueExpected,
ast->firstSourceLocation(),
QString::number(maxValue), QString(), false));
} else if (m_schema->hasExclusiveMaximum()) {
analysis()->m_messages.append(Message(ErrMaximumNumberValueIsExclusive,
ast->firstSourceLocation(),
QString(), QString(), false));
} else {
analysis()->boostRanking();
}
}
return false;
}
bool JsonCheck::visit(StringLiteral *ast)
{
if (!proceedCheck(JsonValue::String, ast->firstSourceLocation()))
return false;
analysis()->boostRanking();
const QString &literal = ast->value.toString();
const QString &pattern = m_schema->pattern();
if (!pattern.isEmpty()) {
QRegExp regExp(pattern);
if (regExp.indexIn(literal) == -1) {
analysis()->m_messages.append(Message(ErrInvalidStringValuePattern,
ast->firstSourceLocation(),
QString(), QString(), false));
return false;
}
analysis()->boostRanking(3); // Treat string patterns with higher weight.
}
int expectedLength = m_schema->minimumLength();
if (expectedLength != -1) {
if (literal.length() < expectedLength) {
analysis()->m_messages.append(Message(ErrLongerStringValueExpected,
ast->firstSourceLocation(),
QString::number(expectedLength), QString(),
false));
} else {
analysis()->boostRanking();
}
}
expectedLength = m_schema->maximumLength();
if (expectedLength != -1) {
if (literal.length() > expectedLength) {
analysis()->m_messages.append(Message(ErrShorterStringValueExpected,
ast->firstSourceLocation(),
QString::number(expectedLength), QString(),
false));
} else {
analysis()->boostRanking();
}
}
return false;
}
static QString formatExpectedTypes(QStringList all)
{
all.removeDuplicates();
return all.join(QLatin1String(", or "));
}
void JsonCheck::processSchema(Node *ast)
{
if (m_schema->hasTypeSchema()) {
m_schema->enterNestedTypeSchema();
processSchema(ast);
m_schema->leaveNestedSchema();
} else if (m_schema->hasUnionSchema()) {
// Sec. 5.1: "... value is valid if it is of the same type as one of the simple
// type definitions, or valid by one of the schemas, in the array."
int bestRank = 0;
QList<StaticAnalysis::Message> bestErrorGuess;
int current = 0;
const int unionSize = m_schema->unionSchemaSize();
m_analysis.push(AnalysisData());
for (; current < unionSize; ++current) {
if (m_schema->maybeEnterNestedUnionSchema(current)) {
processSchema(ast);
m_schema->leaveNestedSchema();
} else {
Node::accept(ast, this);
}
if (analysis()->m_hasMatch)
break;
if (analysis()->m_ranking >= bestRank) {
bestRank = analysis()->m_ranking;
bestErrorGuess = analysis()->m_messages;
}
analysis()->m_ranking = 0;
analysis()->m_messages.clear();
}
m_analysis.pop();
if (current == unionSize) {
// When we don't have a match for a union typed schema, we try to "guess" which
// particular item from the union the user tried to represent. The one with the best
// ranking wins.
if (bestRank > 0) {
analysis()->m_messages.append(bestErrorGuess);
} else {
analysis()->m_messages.append(Message(ErrDifferentValueExpected,
ast->firstSourceLocation(),
formatExpectedTypes(m_schema->validTypes()),
QString(),
false));
}
}
} else {
Node::accept(ast, this);
}
}
bool JsonCheck::proceedCheck(JsonValue::Kind kind, const SourceLocation &location)
{
if (!m_firstLoc.isValid())
return false;
if (!m_schema->isTypeConstrained())
return false;
if (!m_schema->acceptsType(JsonValue::kindToString(kind))) {
analysis()->m_messages.append(Message(ErrDifferentValueExpected,
location,
formatExpectedTypes(m_schema->validTypes()),
QString(),
false));
return false;
}
return true;
}
JsonCheck::AnalysisData *JsonCheck::analysis()
{
return &m_analysis.top();
}
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (info@qt.nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at info@qt.nokia.com.
**
**************************************************************************/
#ifndef JSONCHECK_H
#define JSONCHECK_H
#include "qmljs_global.h"
#include <qmljs/qmljsdocument.h>
#include <qmljs/parser/qmljsast_p.h>
#include <qmljs/parser/qmljsastvisitor_p.h>
#include <qmljs/qmljsstaticanalysismessage.h>
#include <utils/json.h>
#include <QtCore/QString>
#include <QtCore/QSet>
#include <QtCore/QStack>
#include <QtCore/QList>
namespace QmlJS {
class QMLJS_EXPORT JsonCheck : public AST::Visitor
{
public:
JsonCheck(Document::Ptr doc);
virtual ~JsonCheck();
QList<StaticAnalysis::Message> operator()(Utils::JsonSchema *schema);
private:
virtual bool preVisit(AST::Node *);
virtual void postVisit(AST::Node *);
virtual bool visit(AST::ObjectLiteral *ast);
virtual bool visit(AST::ArrayLiteral *ast);
virtual bool visit(AST::NullExpression *ast);
virtual bool visit(AST::TrueLiteral *ast);
virtual bool visit(AST::FalseLiteral *ast);
virtual bool visit(AST::NumericLiteral *ast);
virtual bool visit(AST::StringLiteral *ast);
struct AnalysisData
{
AnalysisData() : m_ranking(0), m_hasMatch(false), m_evaluatinType(false) {}
void boostRanking(int unit = 1) { m_ranking += unit; }
int m_ranking;
bool m_hasMatch;
bool m_evaluatinType;
QList<StaticAnalysis::Message> m_messages;
};
void processSchema(AST::Node *ast);
bool proceedCheck(Utils::JsonValue::Kind kind, const AST::SourceLocation &location);
AnalysisData *analysis();
Document::Ptr m_doc;
AST::SourceLocation m_firstLoc;
Utils::JsonSchema *m_schema;
QStack<AnalysisData> m_analysis;
};
} // QmlJs
#endif // JSONCHECK_H
......@@ -32,7 +32,8 @@ HEADERS += \
$$PWD/qmljscontext.h \
$$PWD/qmljsscopechain.h \
$$PWD/qmljsutils.h \
$$PWD/qmljsstaticanalysismessage.h
$$PWD/qmljsstaticanalysismessage.h \
$$PWD/jsoncheck.h
SOURCES += \
$$PWD/qmljsbind.cpp \
......@@ -56,7 +57,8 @@ SOURCES += \
$$PWD/qmljscontext.cpp \
$$PWD/qmljsscopechain.cpp \
$$PWD/qmljsutils.cpp \
$$PWD/qmljsstaticanalysismessage.cpp
$$PWD/qmljsstaticanalysismessage.cpp \
$$PWD/jsoncheck.cpp
RESOURCES += \
$$PWD/qmljs.qrc
......
......@@ -193,6 +193,30 @@ StaticAnalysisMessages::StaticAnalysisMessages()
tr("unintentional empty block, use ({}) for empty object literal"));
newMsg(HintPreferNonVarPropertyType, Hint,
tr("use %1 instead of 'var' or 'variant' to improve performance"), 1);
newMsg(ErrMissingRequiredProperty, Error,
tr("missing property '%1'"), 1);
newMsg(ErrObjectValueExpected, Error,
tr("object value expected"));
newMsg(ErrArrayValueExpected, Error,
tr("array value expected"));
newMsg(ErrDifferentValueExpected, Error,
tr("%1 value expected"), 1);
newMsg(ErrSmallerNumberValueExpected, Error,
tr("maximum number value is %1"), 1);
newMsg(ErrLargerNumberValueExpected, Error,
tr("minimum number value is %1"), 1);
newMsg(ErrMaximumNumberValueIsExclusive, Error,
tr("maximum number value is exclusive"));
newMsg(ErrMinimumNumberValueIsExclusive, Error,
tr("minimum number value is exclusive"));
newMsg(ErrInvalidStringValuePattern, Error,
tr("string value does not match required pattern"));
newMsg(ErrLongerStringValueExpected, Error,
tr("minimum string value length is %1"), 1);
newMsg(ErrShorterStringValueExpected, Error,
tr("maximum string value length is %1"), 1);
newMsg(ErrInvalidArrayValueLength, Error,
tr("%1 elements expected in array value"), 1);
}
} // anonymous namespace
......@@ -208,7 +232,11 @@ Message::Message()
: type(UnknownType), severity(Hint)
{}
Message::Message(Type type, AST::SourceLocation location, const QString &arg1, const QString &arg2)
Message::Message(Type type,
AST::SourceLocation location,
const QString &arg1,
const QString &arg2,
bool appendTypeId)
: location(location), type(type)
{
QTC_ASSERT(messages()->messages.contains(type), return);
......@@ -227,7 +255,8 @@ Message::Message(Type type, AST::SourceLocation location, const QString &arg1, c
qWarning() << "StaticAnalysis message" << type << "expects exactly two arguments";
message = message.arg(arg1, arg2);
}
message.append(QString(" (M%1)").arg(QString::number(prototype.type)));
if (appendTypeId)
message.append(QString(" (M%1)").arg(QString::number(prototype.type)));
}
bool Message::isValid() const
......
......@@ -114,7 +114,19 @@ enum Type
WarnNumberConstructor = 308,
HintBinaryOperatorSpacing = 309,
WarnUnintentinalEmptyBlock = 310,
HintPreferNonVarPropertyType = 311
HintPreferNonVarPropertyType = 311,
ErrMissingRequiredProperty = 312,
ErrObjectValueExpected = 313,
ErrArrayValueExpected = 314,
ErrDifferentValueExpected = 315,
ErrSmallerNumberValueExpected = 316,
ErrLargerNumberValueExpected = 317,
ErrMaximumNumberValueIsExclusive = 318,
ErrMinimumNumberValueIsExclusive = 319,
ErrInvalidStringValuePattern = 320,
ErrLongerStringValueExpected = 321,
ErrShorterStringValueExpected = 322,
ErrInvalidArrayValueLength = 323
};
class QMLJS_EXPORT Message
......@@ -123,7 +135,8 @@ public:
Message();
Message(Type type, AST::SourceLocation location,
const QString &arg1 = QString(),
const QString &arg2 = QString());
const QString &arg2 = QString(),
bool appendTypeId = true);
static QList<Type> allMessageTypes();
......
This diff is collapsed.