@@ -65,301 +65,7 @@ errors; // {
6565
6666## API
6767
68- ### Schemator
69-
70- #### Schemator()
71- ``` js
72- var schemator = new Schemator ();
73- ```
74-
75- #### Schemator#availableDataTypes()
76- ``` js
77- schemator .availableDataTypes (); // ['boolean', 'string', 'etc.']
78- ```
79-
80- #### Schemator#availableRules()
81- ``` js
82- schemator .availableRules (); // ['type', 'minLength', 'etc.']
83- ```
84-
85- #### Schemator#availableSchemata()
86- ``` js
87- schemator .defineSchema (' PersonSchema' , { ... });
88- schemator .availableSchemata (); // ['PersonSchema']
89- ```
90-
91- #### Schemator#getDataType(name)
92- ``` js
93- schemator .getDataType (' myDataType' );
94- ```
95-
96- #### Schemator#getRule(name)
97- ``` js
98- schemator .getRule (' myRule' );
99- ```
100-
101- #### Schemator#getSchema(name)
102- ``` js
103- schemator .getSchema (' PersonSchema' );
104- ```
105-
106- #### Schemator#removeDataType(name)
107- ``` js
108- schemator .removeDataType (' myDataType' );
109- ```
110-
111- #### Schemator#removeRule(name)
112- ``` js
113- schemator .removeRule (' myRule' );
114- ```
115-
116- #### Schemator#removeSchema(name)
117- ``` js
118- schemator .removeSchema (' PersonSchema' );
119- ```
120-
121- #### Schemator#defineDataType(name, typeDefinition)
122- ``` js
123- schemator .defineDataType (' NaN' , function (x ) {
124- if (isNaN (x)) {
125- return null ;
126- } else {
127- return {
128- rule: ' type' ,
129- actual: typeof x,
130- expected: ' NaN'
131- };
132- }
133- });
134- ```
135-
136- #### Schemator#defineRule(name, ruleFunc[ , async] )
137- ``` js
138- schemator .defineRule (' divisibleBy' , function (x , divisor ) {
139- if (typeof x === ' number' && typeof divisor === ' number' && x % divisor !== 0 ) {
140- return {
141- rule: ' divisibleBy' ,
142- actual: ' ' + x + ' % ' + divisor + ' === ' + (x % divisor),
143- expected: ' ' + x + ' % ' + divisor + ' === 0'
144- };
145- }
146- return null ;
147- });
148-
149- schemator .defineSchema (' mySchema' , {
150- seats: {
151- divisibleBy: 4
152- }
153- });
154-
155- var errors = schemator .getSchema (' mySchema' ).validateSync ({
156- seats: 16
157- });
158-
159- errors; // null
160-
161- errors = schemator .getSchema (' mySchema' ).validateSync ({
162- seats: 17
163- });
164-
165- errors; // {
166- // seats: {
167- // errors: [ {
168- // rule: 'divisibleBy',
169- // actual: '17 % 4 === 1',
170- // expected: '17 % 4 === 0'
171- // } ]
172- // }
173- // }
174- ```
175-
176- Asynchronous rule:
177- ``` js
178- schemator .defineRule (' divisibleBy' , function (x , divisor , cb ) {
179-
180- // asynchronity here is fake, but you could do something async, like make an http request
181- setTimeout (function () {
182- if (typeof x === ' number' && typeof divisor === ' number' && x % divisor !== 0 ) {
183- cb ({
184- rule: ' divisibleBy' ,
185- actual: ' ' + x + ' % ' + divisor + ' === ' + (x % divisor),
186- expected: ' ' + x + ' % ' + divisor + ' === 0'
187- });
188- }
189- cb (null );
190- }, 1 );
191- }, true ); // pass true as the third argument
192-
193- schemator .defineSchema (' mySchema' , {
194- seats: {
195- divisibleBy: 4
196- }
197- });
198-
199- var errors = schemator .getSchema (' mySchema' ).validate ({
200- seats: 16
201- }, function (err ) {
202- errors; // null
203-
204- errors = schemator .getSchema (' mySchema' ).validate ({
205- seats: 17
206- }, function (err ) {
207- errors; // {
208- // seats: {
209- // errors: [ {
210- // rule: 'divisibleBy',
211- // actual: '17 % 4 === 1',
212- // expected: '17 % 4 === 0'
213- // } ]
214- // }
215- // }
216- });
217- });
218- ```
219-
220- #### Schemator#defineSchema(name, definition)
221- ``` js
222- schemator .defineSchema (' PersonSchema' , {
223- name: {
224- first: {
225- type: ' string' ,
226- maxLength: 255
227- },
228- last: {
229- type: ' string' ,
230- maxLength: 255
231- }
232- },
233- age: {
234- type: ' number' ,
235- max: 150 ,
236- min: 0
237- }
238- });
239- ```
240-
241- #### Schemator#validate(schemaName, attrs[ , options] , cb)
242- See ` Schema#validate(attrs[, options], cb) `
243-
244- #### Schemator#validateSync(schemaName, attrs[ , options] )
245- See ` Schema#validateSync(attrs[, options]) `
246-
247- #### Schemator#setDefaults(schemaName, attrs)
248- See ` Schema#setDefaults(attrs) `
249-
250- #### Schemator#getDefaults()
251- See ` Schema#getDefaults() `
252-
253- #### Schemator#addDefaultsToTarget(schemaName, target[ , overwrite] )
254- See ` Schema#addDefaultsToTarget(target) `
255-
256- #### Schemator#stripNonSchemaAttrs(schemaName, target)
257- See ` Schema#stripNonSchemaAttrs(target) `
258-
259- ### Schema
260-
261- #### Schema#validate(attrs[ , options] , cb)
262- ``` js
263- PersonSchema .validate ({
264- name: ' John Anderson'
265- }, function (err ) {
266- err; // null
267- });
268-
269- PersonSchema .validate ({
270- name: 5
271- }, function (err ) {
272- err; // {
273- // name: {
274- // errors: [{
275- // rule: 'type',
276- // actual: 'number',
277- // expected: 'string'
278- // }]
279- // }
280- // }
281- });
282- ```
283-
284- #### Schema#validateSync(attrs[ , options] )
285- ``` js
286- var errors = PersonSchema .validate ({
287- name: ' John Anderson'
288- });
289-
290- errors; // null
291-
292- errors = mySchema .validate ({
293- name: 5
294- });
295- errors; // {
296- // name: {
297- // errors: [{
298- // rule: 'type',
299- // actual: 'number',
300- // expected: 'string'
301- // }]
302- // }
303- // }
304- ```
305-
306- #### Schema#setDefaults(attrs)
307- ``` js
308- PersonSchema .setDefaults ({
309- first: ' ' ,
310- last: ' ' ,
311- plan: ' free'
312- });
313- ```
314-
315- #### Schema#getDefaults()
316- ``` js
317- PersonSchema .getDefaults (); // {
318- first: ' ' ,
319- last: ' ' ,
320- age: 0
321- }
322- ```
323-
324- #### Schema#addDefaultsToTarget(target[ , overwrite] )
325- ``` js
326- var person = {
327- first: ' John' ,
328- plan: ' premium'
329- };
330-
331- PersonSchema .addDefaultsToTarget (person);
332-
333- person; // {
334- first: ' John' ,
335- last: ' ' ,
336- plan: ' premium'
337- }
338-
339- PersonSchema .addDefaultsToTarget (person, true );
340-
341- person; // {
342- first: ' ' ,
343- last: ' ' ,
344- plan: ' free'
345- }
346- ```
347-
348- #### Schema#stripNonSchemaAttrs(target)
349- ``` js
350- var person = {
351- first: ' John' ,
352- plan: ' premium' ,
353- nonSchema: ' value'
354- };
355-
356- PersonSchema .stripNonSchemaAttrs (person);
357-
358- person; // {
359- first: ' John' ,
360- plan: ' premium'
361- }
362- ```
68+ [ js-data-schema api] ( http://www.js-data.io/docs/js-data-schema )
36369
36470## License
36571[ MIT License] ( https://github.com/js-data/js-data-schema/blob/master/LICENSE )
0 commit comments