1+ 'use strict' ;
2+
3+ var _extends = Object . assign || function ( target ) { for ( var i = 1 ; i < arguments . length ; i ++ ) { var source = arguments [ i ] ; for ( var key in source ) { if ( Object . prototype . hasOwnProperty . call ( source , key ) ) { target [ key ] = source [ key ] ; } } } return target ; } ;
4+
5+ var MixedSchema = require ( './mixed' ) ;
6+ var Promise = require ( 'promise/lib/es6-extensions' ) ;
7+
8+ var _require = require ( './locale.js' ) ;
9+
10+ var mixed = _require . mixed ;
11+ var locale = _require . array ;
12+
13+ var _require2 = require ( './util/_' ) ;
14+
15+ var inherits = _require2 . inherits ;
16+ var collectErrors = _require2 . collectErrors ;
17+
18+ var scopeError = function scopeError ( value ) {
19+ return function ( err ) {
20+ err . value = value ;
21+ throw err ;
22+ } ;
23+ } ;
24+
25+ module . exports = ArraySchema ;
26+
27+ function ArraySchema ( ) {
28+ if ( ! ( this instanceof ArraySchema ) ) return new ArraySchema ( ) ;
29+
30+ MixedSchema . call ( this , { type : 'array' } ) ;
31+
32+ this . transforms . push ( function ( values ) {
33+ if ( typeof values === 'string' ) try {
34+ values = JSON . parse ( values ) ;
35+ } catch ( err ) {
36+ values = null ;
37+ }
38+
39+ if ( Array . isArray ( values ) ) return this . _subType ? values . map ( this . _subType . cast , this . _subType ) : values ;
40+
41+ return this . isType ( values ) ? values : null ;
42+ } ) ;
43+ }
44+
45+ inherits ( ArraySchema , MixedSchema , {
46+
47+ _typeCheck : function _typeCheck ( v ) {
48+ return Array . isArray ( v ) ;
49+ } ,
50+
51+ _validate : function _validate ( _value , _opts , _state ) {
52+ var errors = [ ] ,
53+ context ,
54+ subType ,
55+ schema ,
56+ endEarly ,
57+ recursive ;
58+
59+ _state = _state || { } ;
60+ context = _state . parent || ( _opts || { } ) . context ;
61+ schema = this . _resolve ( context ) ;
62+ subType = schema . _subType ;
63+ endEarly = schema . _option ( 'abortEarly' , _opts ) ;
64+ recursive = schema . _option ( 'recursive' , _opts ) ;
65+
66+ return MixedSchema . prototype . _validate . call ( this , _value , _opts , _state ) [ 'catch' ] ( endEarly ? null : function ( err ) {
67+ errors = err ;
68+ return err . value ;
69+ } ) . then ( function ( value ) {
70+ if ( ! recursive || ! subType || ! schema . _typeCheck ( value ) ) {
71+ if ( errors . length ) throw errors [ 0 ] ;
72+ return value ;
73+ }
74+
75+ var result = value . map ( function ( item , key ) {
76+ var path = ( _state . path || '' ) + '[' + key + ']' ,
77+ state = _extends ( { } , _state , { path : path , key : key , parent : value } ) ;
78+
79+ return subType . _validate ( item , _opts , state ) ;
80+ } ) ;
81+
82+ result = endEarly ? Promise . all ( result ) [ 'catch' ] ( scopeError ( value ) ) : collectErrors ( result , value , _state . path , errors ) ;
83+
84+ return result . then ( function ( ) {
85+ return value ;
86+ } ) ;
87+ } ) ;
88+ } ,
89+
90+ of : function of ( schema ) {
91+ var next = this . clone ( ) ;
92+ next . _subType = schema ;
93+ return next ;
94+ } ,
95+
96+ required : function required ( msg ) {
97+ var next = MixedSchema . prototype . required . call ( this , msg || mixed . required ) ;
98+
99+ return next . min ( 1 , msg || mixed . required ) ;
100+ } ,
101+
102+ min : function min ( _min , message ) {
103+ message = message || locale . min ;
104+
105+ return this . test ( {
106+ message : message ,
107+ name : 'min' ,
108+ exclusive : true ,
109+ params : { min : _min } ,
110+ test : function test ( value ) {
111+ return value && value . length >= _min ;
112+ }
113+ } ) ;
114+ } ,
115+
116+ max : function max ( _max , message ) {
117+ message = message || locale . max ;
118+ return this . test ( {
119+ message : message ,
120+ name : 'max' ,
121+ exclusive : true ,
122+ params : { max : _max } ,
123+ test : function test ( value ) {
124+ return value && value . length <= _max ;
125+ }
126+ } ) ;
127+ } ,
128+
129+ compact : function compact ( rejector ) {
130+ var reject = ! rejector ? function ( v ) {
131+ return ! ! v ;
132+ } : function ( v , i , a ) {
133+ return ! rejector ( v , i , a ) ;
134+ } ;
135+
136+ return this . transform ( function ( values ) {
137+ return values != null ? values . filter ( reject ) : values ;
138+ } ) ;
139+ }
140+ } ) ;
0 commit comments