Code coverage report for .readme/includes/Schema/SchemaArrayExample.js

Statements: 100% (15 / 15)      Branches: 100% (0 / 0)      Functions: 100% (3 / 3)      Lines: 100% (15 / 15)      Ignored: none     

All files » .readme/includes/Schema/ » SchemaArrayExample.js
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  1       1             1 1       1 1       1   1               1   1                       1               1 1         1   1            
 
var Schema = Divhide.Schema;
 
 
/// Array schema ( no repeatable)
var schema = Schema
    .array([ Schema.string().default("value"), Schema.number(), Schema.string() ])
    .optional()
    .compile();
 
 
/// because its optional it returns null
var value = schema.value();
expect(value).toBe(null);
 
 
/// The value is right!
value = schema.value([ '1', 2, '3' ]);
expect(value).equals([ '1', 2, '3']);
 
 
/// array have more items than it should
expect(
    function(){
        schema.value([ '1', 2, '3', 4, 5, 6 ])
    })
    .toThrow(
        new Error("Expected list with 3 items but found 6.")
    );
 
 
/// Wrong type!
expect(
    function(){
        schema.value(10);
    })
    .toThrow(
        new Error("'array' was expected but found 'number' instead.")
    );
 
 
///
/// Just another way to write the rule!
///
 
 
var schema = Schema
    .array([ Schema.string(), Schema.number() ])
    .repeatable()
    .optional()
    .compile();
 
 
// Get the value
var value = schema.value(["1", 2, "3", 4]);
expect(value).equals(["1", 2, "3", 4]);
 
 
// because the number of item on the array must be multiple of 2
// an error is thrown
expect(
    function(){
        schema.value(["1", 2, "3"])
    })
    .toThrow(
        new Error("Expected list length to be multiple of 2 but found length of 3.")
    );