summaryrefslogtreecommitdiffstats
path: root/src/qtjsonschema/checkpoints_p.h
blob: 9ca8a1d338a25639d52013ea42708ac24bc57236 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** $QT_BEGIN_LICENSE:LGPL$
** 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.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** 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.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef CHECKPOINTS_H
#define CHECKPOINTS_H

#include <QtCore/qhash.h>
#include <QtCore/qlist.h>
#include <QtCore/qregexp.h>

#include <QStringList>
#include <QDebug>
#include <QDate>
#include <QUrl>
#include <QColor>

#include <math.h>

QT_BEGIN_HEADER

namespace SchemaValidation {

/**
  \internal
  This template is used for hash computation for static latin1 strings.
 */
template<ushort C1 = 0, ushort C2 = 0, ushort C3 = 0, ushort C4 = 0, ushort C5 = 0,
         ushort C6 = 0, ushort C7 = 0, ushort C8 = 0, ushort C9 = 0, ushort C10 = 0,
         ushort C11 = 0, ushort C12 = 0, ushort C13 = 0, ushort C14 = 0, ushort C15 = 0,
         ushort C16 = 0, ushort C17 = 0, ushort C18 = 0>
struct QStaticStringHash
{
    typedef QStaticStringHash<C2, C3, C4, C5, C6, C7, C8, C9, C10, C11, C12, C13, C14, C15, C16, C17, C18> Suffix;

    const static int Hash = (C1 ^ Suffix::Hash) + 8;
    //(C1 ^ ( (C2 ^ (...)) +8 )) +8
};

template<>
struct QStaticStringHash<>
{
    typedef QStaticStringHash<> Suffix;
    const static int Hash = 0;

    /**
      \internal
      This function has to be align with qStringHash::Hash value
    */
    inline static int hash(const QString &string)
    {
        const ushort *str = reinterpret_cast<const ushort*>(string.constData());
        return hash(str, 0, string.length());
    }
private:
    inline static int hash(const ushort *str, const int index, const int length)
    {
        return index != length ? (str[index] ^ hash(str, index + 1, length)) + 8
                     : 0;
    }
};

template<class T>
class SchemaPrivate<T>::NullCheck : public Check {
public:
    NullCheck(SchemaPrivate *schema, QSharedPointer<CheckSharedData> &data)
        : Check(schema, data, "") // TODO
    {}
protected:
    virtual bool doCheck(const Value&) { return true; }
};

// 5.1
template<class T>
class SchemaPrivate<T>::CheckType : public Check {
    enum Type {StringType = 0x0001,
               NumberType = 0x0002,
               IntegerType = 0x0004,
               BooleanType = 0x0008,
               ObjectType = 0x0010,
               ArrayType = 0x0020,
               NullType = 0x0040,
               AnyType = 0x0080,
               UnknownType = 0};
public:
    CheckType(SchemaPrivate *schema, QSharedPointer<CheckSharedData> &data, const Value& type)
        : Check(schema, data, "Type check failed for %1")
        , m_type(UnknownType)
    {
        bool ok;
        QStringList typesName;

        QString typeName = type.toString(&ok);
        if (!ok) {
            ValueList typesList = type.toList(&ok);
            Q_ASSERT_X(ok, Q_FUNC_INFO, "Type is neither a string nor an array");
            typename ValueList::const_iterator i;
            for (i = typesList.constBegin(); i != typesList.constEnd(); ++i) {
                typeName = Value(*i).toString(&ok);
                if (ok) {
                    typesName << typeName;
                }
            }
        } else {
            typesName << typeName;
        }
        foreach (const QString &name, typesName) {
            const int hash = QStaticStringHash<>::hash(name.toLower());

            // FIXME there are chances of conflicts, do we care? What is chance for new types in JSON?
            // FIXME we need to check only 2 chars. That would be faster.
            // FIXME probably we want to support schemas here too.
            switch (hash) {
            case QStaticStringHash<'s','t','r','i','n','g'>::Hash:
                m_type |= StringType;
                break;
            case QStaticStringHash<'n','u','m','b','e','r'>::Hash:
                m_type |= NumberType;
                m_type |= IntegerType; // an integer is also a number
                break;
            case QStaticStringHash<'i','n','t','e','g','e','r'>::Hash:
                m_type |= IntegerType;
                break;
            case QStaticStringHash<'b','o','o','l','e','a','n'>::Hash:
                m_type |= BooleanType;
                break;
            case QStaticStringHash<'o','b','j','e','c','t'>::Hash:
                m_type |= ObjectType;
                break;
            case QStaticStringHash<'a','r','r','a','y'>::Hash:
                m_type |= ArrayType;
                break;
            case QStaticStringHash<'a','n','y'>::Hash:
                m_type |= AnyType;
                break;
            case QStaticStringHash<'n','u','l','l'>::Hash:
                m_type |= NullType;
                break;
            default:
                m_type |= UnknownType;
            }
        }

//        qDebug() << Q_FUNC_INFO << m_type << type.toString(&ok);
    }

    virtual bool doCheck(const Value &value)
    {
        if (m_type == UnknownType)
            return true;

        bool result = findType(value) & m_type;
//        bool ok;
//        qDebug() << Q_FUNC_INFO << findType(value)  << m_type << value.toString(&ok) << result;
        return result;
    }

private:
    inline Type findType(const Value &value) const
    {
        bool ok;
        // Lets assume that the value is valid.
        switch (m_type) {
        case StringType:
            value.toString(&ok);
            if (ok)
                return StringType;
            break;
        case NumberType:
            value.toDouble(&ok);
            if (ok)
                return NumberType;
            break;
        case IntegerType:
            value.toInt(&ok);
            if (ok)
                return IntegerType;
            break;
        case BooleanType:
            value.toBoolean(&ok);
            if (ok)
                return BooleanType;
            break;
        case ObjectType:
            value.toObject(&ok);
            if (ok)
                return ObjectType;
            break;
        case NullType:
            value.toNull(&ok);
            if (ok)
                return NullType;
            break;
        case AnyType:
            return AnyType;
        case UnknownType:
            break;
        default:
            break;
        };

        //TODO FIXME it can be better
        value.toInt(&ok);
        if (ok)
            return IntegerType;
        value.toDouble(&ok);
        if (ok)
            return NumberType;
        value.toObject(&ok);
        if (ok)
            return ObjectType;
        value.toString(&ok);
        if (ok)
            return StringType;
        value.toBoolean(&ok);
        if (ok)
            return BooleanType;
        value.toList(&ok);
        if (ok)
            return ArrayType;
        return AnyType;
    }

    uint m_type;
};

// 5.2
template<class T>
class SchemaPrivate<T>::CheckProperties : public Check {
public:
    CheckProperties(SchemaPrivate *schema, QSharedPointer<CheckSharedData> &data, const Value &properties)
        : Check(schema, data, "Properties check failed for %1")
    {
        bool ok;
        const Object obj = properties.toObject(&ok);
        Q_ASSERT(ok);

        QList<Key> propertyNames = obj.propertyNames();
//        qDebug() << "    propertyNames: " << propertyNames <<this;

        m_checks.reserve(propertyNames.count());
        foreach (const Key &propertyName, propertyNames) {
            QVarLengthArray<Check *, 4> checks;
            const Object propertyChecks = obj.property(propertyName).toObject(&ok);
//            qDebug() << "    propertyChecks:" << propertyChecks;

            // create object to share data between attribures of a property
            QSharedPointer<CheckSharedData> data(new CheckSharedData());

            Q_ASSERT(ok);
            foreach (const Key &key, propertyChecks.propertyNames()) {
//                bool ok;
//                qDebug() << "        key:" << key << this << propertyChecks.property(key).toString(&ok)<< propertyChecks.property(key).toInt(&ok);
                checks.append(schema->createCheckPoint(key, propertyChecks.property(key), data));
            }
            m_checks.insert(propertyName, checks);
        }
    }

    ~CheckProperties()
    {
        typename QHash<const Key, QVarLengthArray<Check *, 4> >::const_iterator i;
        for (i = m_checks.constBegin(); i != m_checks.constEnd(); ++i) {
            typename QVarLengthArray<Check *, 4>::const_iterator j;
            for (j = i.value().constBegin(); j != i.value().constEnd(); ++j){
                delete *j;
            }
        }
    }

    virtual bool doCheck(const Value &value)
    {
        bool ok;
        Object object = value.toObject(&ok);
        if (!ok)
            return false;

        //qDebug() << Q_FUNC_INFO;
        foreach (const Key &key, object.propertyNames()) {
            QVarLengthArray<Check *, 4> empty;
            QVarLengthArray<Check *, 4> checks = m_checks.value(key, empty);
            Value property = object.property(key);
            foreach (Check *check, checks) {
                //qDebug()  <<"CHECKING:" << check;
                if (!check->check(property)) {
                    return false;
                }
            }
        }
        return true;
    }


    void checkDefault(Value& value, Object &_object) const
    {
        bool ok;
        Object object = value.toObject(&ok);
        if (!ok)
            return;

        //qDebug() << Q_FUNC_INFO;

        // create missing properties list
        QList<Key> strs;
        QHashIterator<const Key, QVarLengthArray<Check *, 4> > it(m_checks);
        while (it.hasNext()) {
             it.next();
             strs << it.key();
         }

        foreach (const Key &key, object.propertyNames()) {
            QVarLengthArray<Check *, 4> empty;
            QVarLengthArray<Check *, 4> checks = m_checks.value(key, empty);
            Value property = object.property(key);

            // remove from missing properties list
            strs.removeOne(key);

            if (_object[key].isObject()) {
                foreach (Check *check, checks) {
                    Object oo(_object[key].toObject());
                    check->checkDefault(property, oo);
                    _object[key] = oo;
                }
            }
        }

        // add defaults for missing properties
        foreach (const Key &key, strs) {
            QVarLengthArray<Check *, 4> empty;
            QVarLengthArray<Check *, 4> checks = m_checks.value(key, empty);
            Value property = object.property(key);

            if (checks.first()->getDefault()) { // basic type
                _object.insert(key, checks.first()->getDefault()->value());
            }
            else { // looks like object or array
                Object object;
                foreach (Check *check, checks) {
                    Value value("", Object());
                    check->checkDefault(value, object);
                }

                if (!object.isEmpty()) {
                    _object.insert(key, object);
                }
            }
        }
    }

private:
    QHash<const Key, QVarLengthArray<Check *, 4> > m_checks;
};

// 5.5
template<class T>
class SchemaPrivate<T>::CheckItems : public Check {
public:
    CheckItems(SchemaPrivate *schemap, QSharedPointer<CheckSharedData> &data, const Value &schema)
        : Check(schemap, data, "Items check failed for %1")
    {
        // qDebug()  << Q_FUNC_INFO << this;
        bool ok;
        Object obj = schema.toObject(&ok);
        Q_ASSERT(ok);
        m_schema = Schema<T>(obj, schemap->m_callbacks);
    }

    virtual bool doCheck(const Value& value)
    {
        //qDebug() << Q_FUNC_INFO << this;
        bool ok;
        ValueList array = value.toList(&ok);
        if (!ok)
            return false;

        typename ValueList::const_iterator i;
        for (i = array.constBegin(); i != array.constEnd(); ++i) {
            if (!m_schema.check(*i, Check::m_schema->m_callbacks)) {
                return false;
            }
        }
        return true;
    }
private:
    Schema<T> m_schema;
};

// 5.7
template<class T>
class SchemaPrivate<T>::CheckRequired : public Check {
public:
    CheckRequired(SchemaPrivate *schema, QSharedPointer<CheckSharedData> &data, const Value &required)
        : Check(schema, data, "Check required field")  // TODO what to do about Required ?
    {
        bool ok;
        m_req = required.toBoolean(&ok);
        if (!ok) {
            // maybe someone used string instead of bool
            QString value = required.toString(&ok).toLower();
            if (value == QString::fromLatin1("false"))
                m_req = false;
            else if (value == QString::fromLatin1("true"))
                m_req = true;
            else
                Q_ASSERT(false);

            qWarning() << QString::fromLatin1("Wrong 'required' syntax found, instead of boolean type a string was used");
        }
        Q_ASSERT(ok);
        if (m_req)
            Check::m_schema->m_maxRequired++;
    }

    virtual bool doCheck(const Value&)
    {
        //qDebug() << Q_FUNC_INFO << m_schema << this;
        if (m_req)
            Check::m_schema->m_requiredCount++;
        return true;
    }
private:
    bool m_req;
};

// 5.9
template<class T>
class SchemaPrivate<T>::CheckMinimum : public Check {
public:
    CheckMinimum(SchemaPrivate *schema, QSharedPointer<CheckSharedData> &data, const Value &minimum)
        : Check(schema, data, "Minimum check failed for %1")
    {
        bool ok;
        m_min = minimum.toDouble(&ok);
        Q_ASSERT(ok);
    }

    virtual bool doCheck(const Value &value)
    {
        bool ok;
        double d = value.toDouble(&ok);
        return ok && (Check::m_data->m_flags.testFlag(CheckSharedData::ExclusiveMinimum) ? d > m_min : d >= m_min);
    }
private:
    double m_min;
};

// 5.10
template<class T>
class SchemaPrivate<T>::CheckMaximum : public Check {
public:
    CheckMaximum(SchemaPrivate *schema, QSharedPointer<CheckSharedData> &data, const Value &maximum)
        : Check(schema, data, "Maximum check failed for %1")
    {
        // qDebug()  << Q_FUNC_INFO << this;
        bool ok;
        m_max = maximum.toDouble(&ok);
        Q_ASSERT(ok);
    }

    virtual bool doCheck(const Value &value)
    {
        //qDebug() << Q_FUNC_INFO << value << m_max << this;
        bool ok;
        double d = value.toDouble(&ok);
        return ok && (Check::m_data->m_flags.testFlag(CheckSharedData::ExclusiveMaximum) ? d < m_max : d <= m_max);
    }
private:
    double m_max;
};


// 5.11
template<class T>
class SchemaPrivate<T>::CheckExclusiveMinimum : public Check {
public:
    CheckExclusiveMinimum(SchemaPrivate *schema, QSharedPointer<CheckSharedData> &data, const Value &_value)
        : Check(schema, data, "Exclusive minimum check failed for %1")
    {
        bool ok;
        bool bExclusive = _value.toBoolean(&ok);
        if (!ok) {
            // maybe someone used string instead of bool
            QString value = _value.toString(&ok).toLower();
            if (value == QString::fromLatin1("false"))
                bExclusive = false;
            else if (value == QString::fromLatin1("true"))
                bExclusive = true;
            else
                Q_ASSERT(false);

            if (!value.isEmpty())
                qWarning() << QString::fromLatin1("Wrong 'exclusiveMinimum' syntax found, instead of boolean type a string was used");
        }
        Q_ASSERT(ok);

        if (bExclusive) {
            Check::m_data->m_flags |= CheckSharedData::ExclusiveMinimum;
        }
    }

    virtual bool doCheck(const Value &)
    {
        // check will be done in minimum
        return true;
    }
private:
};

// 5.12
template<class T>
class SchemaPrivate<T>::CheckExclusiveMaximum : public Check {
public:
    CheckExclusiveMaximum(SchemaPrivate *schema, QSharedPointer<CheckSharedData> &data, const Value &_value)
        : Check(schema, data, "Exclusive minimum check failed for %1")
    {
        bool ok;
        bool bExclusive = _value.toBoolean(&ok);
        if (!ok) {
            // maybe someone used string instead of bool
            QString value = _value.toString(&ok).toLower();
            if (value == QString::fromLatin1("false"))
                bExclusive = false;
            else if (value == QString::fromLatin1("true"))
                bExclusive = true;
            else
                Q_ASSERT(false);

            if (!value.isEmpty())
                qWarning() << QString::fromLatin1("Wrong 'exclusiveMaximum' syntax found, instead of boolean type a string was used");
        }
        Q_ASSERT(ok);

        if (bExclusive) {
            Check::m_data->m_flags |= CheckSharedData::ExclusiveMaximum;
        }
    }

    virtual bool doCheck(const Value &)
    {
        // check will be done in maximum
        return true;
    }
private:
};

// 5.13
template<class T>
class SchemaPrivate<T>::CheckMinItems : public Check {
public:
    CheckMinItems(SchemaPrivate *schema, QSharedPointer<CheckSharedData> &data, const Value& minimum)
        : Check(schema, data, "Minimum item count check failed for %1")
    {
        bool ok;
        m_min = minimum.toInt(&ok);
        Q_ASSERT(ok);
    }

    virtual bool doCheck(const Value &value)
    {
        bool ok;
        int count = value.toList(&ok).count();
        return count >= m_min && ok;
    }
private:
    int m_min;
};

// 5.14
template<class T>
class SchemaPrivate<T>::CheckMaxItems : public Check {
public:
    CheckMaxItems(SchemaPrivate *schema, QSharedPointer<CheckSharedData> &data, const Value& maximum)
        : Check(schema, data, "Maximum item count check failed for %1")
    {
        bool ok;
        m_max = maximum.toInt(&ok);
        Q_ASSERT(ok);
    }

    virtual bool doCheck(const Value &value)
    {
        bool ok;
        int count = value.toList(&ok).count();
        return count <= m_max && ok;
    }

private:
    int m_max;
};

// 5.16
template<class T>
class SchemaPrivate<T>::CheckPattern : public Check {
public:
    CheckPattern(SchemaPrivate *schema, QSharedPointer<CheckSharedData> &data, const Value& patternValue)
        : Check(schema, data, "Pattern check failed for %1")
    {
        bool ok;
        QString patternString = patternValue.toString(&ok);
        m_regexp.setPattern(patternString);
        Q_ASSERT(ok && m_regexp.isValid());
    }

    virtual bool doCheck(const Value &value)
    {
        bool ok;
        QString str = value.toString(&ok);
        if (!ok) {
            // According to spec (5.15) we should check the value only when it exist and it is a string.
            // It is a bit strange, but I think we have to return true here.
            return true;
        }
        return m_regexp.exactMatch(str);
    }
private:
    QRegExp m_regexp;
};

// 5.17
template<class T>
class SchemaPrivate<T>::CheckMinLength : public Check {
public:
    CheckMinLength(SchemaPrivate *schema, QSharedPointer<CheckSharedData> &data, const Value& min)
        : Check(schema, data, "Minimal string length check failed for %1")
    {
        bool ok;
        m_min = min.toInt(&ok);
        Q_ASSERT(ok);
    }

    virtual bool doCheck(const Value &value)
    {
        bool ok;
        QString str = value.toString(&ok);
//        qDebug() << Q_FUNC_INFO << str << ok;
        if (!ok) {
            // According to spec (5.16) we should check the value only when it exist and it is a string.
            // It is a bit strange, but I think we have to return true here.
            return true;
        }
        return str.length() >= m_min;
    }
private:
    int m_min;
};

// 5.18
template<class T>
class SchemaPrivate<T>::CheckMaxLength : public Check {
public:
    CheckMaxLength(SchemaPrivate *schema, QSharedPointer<CheckSharedData> &data, const Value& max)
        : Check(schema, data, "Maximal string length check failed for %1")
    {
        bool ok;
        m_max = max.toInt(&ok);
        Q_ASSERT(ok);
    }

    virtual bool doCheck(const Value &value)
    {
        bool ok;
        QString str = value.toString(&ok);
//        qDebug() << Q_FUNC_INFO << str << ok;
        if (!ok) {
            // According to spec (5.16) we should check the value only when it exist and it is a string.
            // It is a bit strange, but I think we have to return true here.
            return true;
        }
        return str.length() <= m_max;
    }
private:
    int m_max;
};

// 5.19
template<class T>
class SchemaPrivate<T>::CheckEnum : public Check {
public:
    CheckEnum(SchemaPrivate *schema, QSharedPointer<CheckSharedData> &data, const Value& value)
        : Check(schema, data, "Enum check failed for %1")
    {
        bool ok;
        m_enum = value.toList(&ok);
        Q_ASSERT(ok);
    }

    virtual bool doCheck(const Value &value)
    {
        typename ValueList::const_iterator i;
        for (i = m_enum.constBegin(); i != m_enum.constEnd(); ++i) {
            if (value.compare(*i)) {
                return true;
            }
        }
        return false;
    }

private:
    ValueList m_enum;
};

// 5.20
template<class T>
class SchemaPrivate<T>::CheckDefault : public Check {
public:
    CheckDefault(SchemaPrivate *schema, QSharedPointer<CheckSharedData> &data, const Value& value)
        : Check(schema, data, "Default check failed for %1")
    {
        // used shared data to store
        Check::m_data->m_default = QSharedPointer<Value>(new Value(value));
    }

    virtual bool doCheck(const Value &value)
    {
        return true;
    }
private:
};

// 5.23
template<class T>
class SchemaPrivate<T>::CheckFormat : public Check {
public:
    CheckFormat(SchemaPrivate *schema, QSharedPointer<CheckSharedData> &data, const Value& value)
        : Check(schema, data, "Enum format failed for %1")
    {
        bool ok;
        m_format = value.toString(&ok).toLower();
        Q_ASSERT(ok && !m_format.isEmpty());
    }

    virtual bool doCheck(const Value &value)
    {
        bool ok = true;
        int hash = QStaticStringHash<>::hash(m_format);
        switch (hash) {
        case QStaticStringHash<'d','a','t','e','-','t','i','m','e'>::Hash:
            if (QString::fromLatin1("date-time") == m_format) {
                QString str;
                if (!(str = value.toString(&ok)).isEmpty() && ok) {
                    return QDateTime::fromString(str, "yyyy-MM-ddThh:mm:ssZ").isValid();
                }
            }
            break;
        case QStaticStringHash<'d','a','t','e'>::Hash:
            if (QString::fromLatin1("date") == m_format) {
                QString str;
                if (!(str = value.toString(&ok)).isEmpty() && ok) {
                    return QDate::fromString(str, "yyyy-MM-dd").isValid();
                }
            }
            break;
        case QStaticStringHash<'t','i','m','e'>::Hash:
            if (QString::fromLatin1("time") == m_format) {
                QString str;
                if (!(str = value.toString(&ok)).isEmpty() && ok) {
                    return QTime::fromString(str, "hh:mm:ss").isValid();
                }
            }
            break;
        case QStaticStringHash<'r','e','g','e','x','p'>::Hash:
            if (QString::fromLatin1("regexp") == m_format) {
            }
            break;
        case QStaticStringHash<'c','o','l','o','r'>::Hash:
            if (QString::fromLatin1("color") == m_format) {
                QString str;
                if (!(str = value.toString(&ok)).isEmpty() && ok) {
                    return QColor(str).isValid();
                }
            }
            break;
        case QStaticStringHash<'u','r','i'>::Hash:
            if (QString::fromLatin1("uri") == m_format) {
                QString str;
                if (!(str = value.toString(&ok)).isEmpty() && ok) {
                    return str.contains(':') || QUrl(str, QUrl::StrictMode).isValid(); // URN or URL
                }
            }
            break;
        case QStaticStringHash<'u','r','l'>::Hash:
            if (QString::fromLatin1("url") == m_format) {
                QString str;
                if (!(str = value.toString(&ok)).isEmpty() && ok) {
                    return QUrl(str, QUrl::StrictMode).isValid();
                }
            }
            break;
        case QStaticStringHash<'p','h','o','n','e'>::Hash:
            if (QString::fromLatin1("phone") == m_format) {
                //TODO
            }
            break;
        case QStaticStringHash<'e','m','a','i','l'>::Hash:
            if (QString::fromLatin1("email") == m_format) {
                //TODO
            }
            break;
        case QStaticStringHash<'i','p','-','a','d','d','r','e','s','s'>::Hash:
            if (QString::fromLatin1("ip-address") == m_format) {
                //TODO
            }
            break;
        case QStaticStringHash<'i','p','v','6'>::Hash:
            if (QString::fromLatin1("ipv6") == m_format) {
                //TODO
            }
            break;
        case QStaticStringHash<'h','o','s','t','-','n','a','m','e'>::Hash:
            if (QString::fromLatin1("host-name") == m_format) {
                //TODO
            }
            break;
        case QStaticStringHash<'n','o','n','n','e','g','a','t','i','v','e','i','n','t','e','g','e','r'>::Hash: // NonNegativeInteger
            if (QString::fromLatin1("nonnegativeinteger") == m_format) {
                return (value.toInt(&ok) >= 0 && ok);
            }
            break;
        }
        return ok;
    }

private:
    QString m_format;
};

// 5.24
template<class T>
class SchemaPrivate<T>::CheckDivisibleBy : public Check {
public:
    CheckDivisibleBy(SchemaPrivate *schema, QSharedPointer<CheckSharedData> &data, const Value &value)
        : Check(schema, data, "DivisibleBy check failed for %1")
    {
        // qDebug()  << Q_FUNC_INFO << this;
        bool ok;
        m_div = value.toDouble(&ok);
        Q_ASSERT(ok && m_div != 0);
    }

    virtual bool doCheck(const Value &value)
    {
        //qDebug() << Q_FUNC_INFO << value << m_div << this;
        bool ok;
        double d = value.toDouble(&ok);
        return m_div != 0 && ok && fmod(d, m_div) == 0;
    }
private:
    double m_div;
};

// 5.26
template<class T>
class SchemaPrivate<T>::CheckExtends : public Check {
public:
    CheckExtends(SchemaPrivate *schema, QSharedPointer<CheckSharedData> &data, const Value &value)
        : Check(schema, data, "Extends check failed for %1")
    {
        // FIXME
        // Keep in mind that there is a bug in spec. (internet draft 3).
        // We should search for a schema not for a string here.
        // Tests are using "string" syntax, so we need to support it for a while
        bool ok;
        Object obj = value.toObject(&ok);
        if (!ok) {
            QString schemaName = value.toString(&ok);
            if (!ok || schemaName.length()) {
                ValueList array = value.toList(&ok);
                Q_ASSERT(ok);
                typename ValueList::const_iterator i;
                for (i = array.constBegin(); i != array.constEnd(); ++i) {
                    obj = Value(*i).toObject(&ok);
                    Q_ASSERT(ok);
                    m_extendedSchema.append(Schema<T>(obj, schema->m_callbacks));
                }
            } else {
                qWarning() << QString::fromLatin1("Wrong 'extends' syntax found, instead of \"%1\" should be \"%2\"")
                              .arg(schemaName, QString::fromLatin1("{\"$ref\":\"%1\"}").arg(schemaName));
                m_extendedSchema.append(schema->m_callbacks->loadSchema(schemaName));
            }
        } else {
            m_extendedSchema.append(Schema<T>(obj, schema->m_callbacks));
        }
    }

    virtual bool doCheck(const Value &value)
    {
        for (int i = 0; i < m_extendedSchema.count(); ++i) {
            if (!m_extendedSchema[i].check(value, Check::m_schema->m_callbacks))
                return false;
        }
        return true;
    }

    void checkDefault(Value& value, Object &_object) const
    {
        for (int i = 0; i < m_extendedSchema.count(); ++i) {
            m_extendedSchema[i].checkDefault(value, _object);
        }
    }

private:
    QVarLengthArray<Schema<T>, 4> m_extendedSchema;
};

// 5.28
template<class T>
class SchemaPrivate<T>::CheckRef : public Check {
public:
    CheckRef(SchemaPrivate *schema, QSharedPointer<CheckSharedData> &data, const Value &value)
        : Check(schema, data, "$Ref check failed for %1")
    {
        // TODO according to spec we should replace existing check by this one
        // I'm not sure what does it mean. Should we remove other checks?
        // What if we have two $ref? Can it happen? For now, lets use magic of
        // undefined bahaviour (without crashing of course).
        bool ok;
        QString schemaName = value.toString(&ok);
        Q_ASSERT(ok);

        m_newSchema = schema->m_callbacks->loadSchema(schemaName);
        if (!m_newSchema.isValid()) {
            // FIXME should we have current schema name?
            const QString msg =  QString::fromLatin1("Schema extends %1 but it is unknown.")
                    .arg(schemaName);
            qWarning() << msg;
            schema->m_callbacks->setError(msg);
        }
    }
    virtual bool doCheck(const Value &value)
    {
        bool result = m_newSchema.check(value, Check::m_schema->m_callbacks);
//        qDebug() << Q_FUNC_INFO << result;
        return result;
    }

    void checkDefault(Value& value, Object &_object) const
    {
        m_newSchema.checkDefault(value, _object);

    }

private:
    Schema<T> m_newSchema;
};

template<class T>
class SchemaPrivate<T>::CheckDescription : public NullCheck {
public:
    CheckDescription(SchemaPrivate *schema, QSharedPointer<CheckSharedData> &data)
        : NullCheck(schema, data)
    {}
};

template<class T>
class SchemaPrivate<T>::CheckTitle : public NullCheck {
public:
    CheckTitle(SchemaPrivate *schema, QSharedPointer<CheckSharedData> &data)
        : NullCheck(schema, data)
    {}
};

template<class T>
typename SchemaPrivate<T>::Check *SchemaPrivate<T>::createCheckPoint(const Key &key, const Value &value, QSharedPointer<CheckSharedData> &data)
{
    QString keyName = key;
    keyName = keyName.toLower();

    // This is a perfect hash. BUT spec, in future, can be enriched by new values, that we should just ignore.
    // As we do not know about them we can't be sure that our perfect hash will be still perfect, therefore
    // we have to do additional string comparison that confirm result hash function result.
    int hash = QStaticStringHash<>::hash(keyName);
    switch (hash) {
    case QStaticStringHash<'r','e','q','u','i','r','e','d'>::Hash:
        if (QString::fromLatin1("required") == keyName)
            return new CheckRequired(this, data, value);
        break;
    case QStaticStringHash<'m','a','x','i','m','u','m'>::Hash:
        if (QString::fromLatin1("maximum") == keyName)
            return new CheckMaximum(this, data, value);
        break;
    case QStaticStringHash<'e','x','c','l','u','s','i','v','e','m','a','x','i','m','u','m'>::Hash:
        if (QString::fromLatin1("exclusivemaximum") == keyName)
            return new CheckExclusiveMaximum(this, data, value);
        break;
    case QStaticStringHash<'m','i','n','i','m','u','m'>::Hash:
        if (QString::fromLatin1("minimum") == keyName)
            return new CheckMinimum(this, data, value);
        break;
    case QStaticStringHash<'e','x','c','l','u','s','i','v','e','m','i','n','i','m','u','m'>::Hash:
        if (QString::fromLatin1("exclusiveminimum") == keyName)
            return new CheckExclusiveMinimum(this, data, value);
        break;
    case QStaticStringHash<'p','r','o','p','e','r','t','i','e','s'>::Hash:
        if (QString::fromLatin1("properties") == keyName)
            return new CheckProperties(this, data, value);
        break;
    case QStaticStringHash<'d','e','s','c','r','i','p','t','i','o','n'>::Hash:
        if (QString::fromLatin1("description") == keyName)
            return new CheckDescription(this, data);
        break;
    case QStaticStringHash<'t','i','t','l','e'>::Hash:
        if (QString::fromLatin1("title") == keyName)
            return new CheckTitle(this, data);
        break;
    case QStaticStringHash<'m','a','x','i','t','e','m','s'>::Hash:
        if (QString::fromLatin1("maxitems") == keyName)
            return new CheckMaxItems(this, data, value);
        break;
    case QStaticStringHash<'m','i','n','i','t','e','m','s'>::Hash:
        if (QString::fromLatin1("minitems") == keyName)
            return new CheckMinItems(this, data, value);
        break;
    case QStaticStringHash<'i','t','e','m','s'>::Hash:
        if (QString::fromLatin1("items") == keyName)
            return new CheckItems(this, data, value);
        break;
    case QStaticStringHash<'e','x','t','e','n','d','s'>::Hash:
        if (QString::fromLatin1("extends") == keyName)
            return new CheckExtends(this, data, value);
        break;
    case QStaticStringHash<'p','a','t','t','e','r','n'>::Hash:
        if (QString::fromLatin1("pattern") == keyName)
            return new CheckPattern(this, data, value);
        break;
    case QStaticStringHash<'m','i','n','l','e','n','g','t','h'>::Hash:
        if (QString::fromLatin1("minlength") == keyName)
            return new CheckMinLength(this, data, value);
        break;
    case QStaticStringHash<'m','a','x','l','e','n','g','t','h'>::Hash:
        if (QString::fromLatin1("maxlength") == keyName)
            return new CheckMaxLength(this, data, value);
        break;
    case QStaticStringHash<'e','n','u','m'>::Hash:
        if (QString::fromLatin1("enum") == keyName)
            return new CheckEnum(this, data, value);
        break;
    case QStaticStringHash<'f','o','r','m','a','t'>::Hash:
        if (QString::fromLatin1("format") == keyName)
            return new CheckFormat(this, data, value);
        break;
    case QStaticStringHash<'d','e','f','a','u','l','t'>::Hash:
        if (QString::fromLatin1("default") == keyName)
            return new CheckDefault(this, data, value);
        break;
    case QStaticStringHash<'d','i','v','i','s','i','b','l','e','b','y'>::Hash:
        if (QString::fromLatin1("divisibleby") == keyName)
            return new CheckDivisibleBy(this, data, value);
        break;
    case QStaticStringHash<'$','r','e','f'>::Hash:
        if (QString::fromLatin1("$ref") == keyName)
            return new CheckRef(this, data, value);
        break;
    case QStaticStringHash<'t','y','p','e'>::Hash:
        if (QString::fromLatin1("type") == keyName)
            return new CheckType(this, data, value);
        break;
    default:
//        qDebug() << "NOT FOUND"  << keyName;
        return new NullCheck(this, data);
    }

//    qDebug() << "FALLBACK"  << keyName;
//    bool  ok;
//    qCritical() << keyName << value.toString(&ok);
    return new NullCheck(this, data);
}

template<class T>
bool Schema<T>::check(const Value &value, Service *callbackToUseForCheck) const
{
    return d_ptr->check(value, callbackToUseForCheck);
}

template<class T>
bool SchemaPrivate<T>::check(const Value &value, Service *callbackToUseForCheck) const
{
    //qDebug() << Q_FUNC_INFO << m_checks.count() << this;
    Q_ASSERT(callbackToUseForCheck);
    Q_ASSERT(!m_callbacks);

    m_callbacks = callbackToUseForCheck;
    bool result = check(value);
    m_callbacks = 0;
    return result;
}

template<class T>
bool SchemaPrivate<T>::check(const Value &value) const
{
    Q_ASSERT(m_callbacks);

    m_requiredCount = 0;
    foreach (Check *check, m_checks) {
        if (!check->check(value)) {
            return false;
        }
    }
    if (m_requiredCount != m_maxRequired) {
        m_callbacks->setError(QString::fromLatin1("Schema validation error: Required field is missing"));
        return false;
    }
    return true;
}

template<class T>
void SchemaPrivate<T>::checkDefault(Value &value, Object &object) const
{
    foreach (Check *check, m_checks) {
        check->checkDefault(value, object);
    }
}

} // namespace SchemaValidation

QT_END_HEADER

#endif // CHECKPOINTS_H