You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							175 lines
						
					
					
						
							3.1 KiB
						
					
					
				
			
		
		
	
	
							175 lines
						
					
					
						
							3.1 KiB
						
					
					
				| 'use strict'
 | |
| 
 | |
| const test = require('tap').test
 | |
| const build = require('..')
 | |
| 
 | |
| test('use toJSON method on object types', (t) => {
 | |
|   t.plan(1)
 | |
| 
 | |
|   const stringify = build({
 | |
|     title: 'simple object',
 | |
|     type: 'object',
 | |
|     properties: {
 | |
|       productName: {
 | |
|         type: 'string'
 | |
|       }
 | |
|     }
 | |
|   })
 | |
|   const object = {
 | |
|     product: { name: 'cola' },
 | |
|     toJSON: function () {
 | |
|       return { productName: this.product.name }
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   t.equal('{"productName":"cola"}', stringify(object))
 | |
| })
 | |
| 
 | |
| test('use toJSON method on nested object types', (t) => {
 | |
|   t.plan(1)
 | |
| 
 | |
|   const stringify = build({
 | |
|     title: 'simple array',
 | |
|     type: 'array',
 | |
|     items: {
 | |
|       type: 'object',
 | |
|       properties: {
 | |
|         productName: {
 | |
|           type: 'string'
 | |
|         }
 | |
|       }
 | |
|     }
 | |
|   })
 | |
|   const array = [
 | |
|     {
 | |
|       product: { name: 'cola' },
 | |
|       toJSON: function () {
 | |
|         return { productName: this.product.name }
 | |
|       }
 | |
|     },
 | |
|     {
 | |
|       product: { name: 'sprite' },
 | |
|       toJSON: function () {
 | |
|         return { productName: this.product.name }
 | |
|       }
 | |
|     }
 | |
|   ]
 | |
| 
 | |
|   t.equal('[{"productName":"cola"},{"productName":"sprite"}]', stringify(array))
 | |
| })
 | |
| 
 | |
| test('not use toJSON if does not exist', (t) => {
 | |
|   t.plan(1)
 | |
| 
 | |
|   const stringify = build({
 | |
|     title: 'simple object',
 | |
|     type: 'object',
 | |
|     properties: {
 | |
|       product: {
 | |
|         type: 'object',
 | |
|         properties: {
 | |
|           name: {
 | |
|             type: 'string'
 | |
|           }
 | |
|         }
 | |
|       }
 | |
|     }
 | |
|   })
 | |
|   const object = {
 | |
|     product: { name: 'cola' }
 | |
|   }
 | |
| 
 | |
|   t.equal('{"product":{"name":"cola"}}', stringify(object))
 | |
| })
 | |
| 
 | |
| test('not fail on null object declared nullable', (t) => {
 | |
|   t.plan(1)
 | |
| 
 | |
|   const stringify = build({
 | |
|     title: 'simple object',
 | |
|     type: 'object',
 | |
|     nullable: true,
 | |
|     properties: {
 | |
|       product: {
 | |
|         type: 'object',
 | |
|         properties: {
 | |
|           name: {
 | |
|             type: 'string'
 | |
|           }
 | |
|         }
 | |
|       }
 | |
|     }
 | |
|   })
 | |
|   t.equal('null', stringify(null))
 | |
| })
 | |
| 
 | |
| test('not fail on null sub-object declared nullable', (t) => {
 | |
|   t.plan(1)
 | |
| 
 | |
|   const stringify = build({
 | |
|     title: 'simple object',
 | |
|     type: 'object',
 | |
|     properties: {
 | |
|       product: {
 | |
|         nullable: true,
 | |
|         type: 'object',
 | |
|         properties: {
 | |
|           name: {
 | |
|             type: 'string'
 | |
|           }
 | |
|         }
 | |
|       }
 | |
|     }
 | |
|   })
 | |
|   const object = {
 | |
|     product: null
 | |
|   }
 | |
|   t.equal('{"product":null}', stringify(object))
 | |
| })
 | |
| 
 | |
| test('throw an error on non nullable null sub-object', (t) => {
 | |
|   t.plan(1)
 | |
| 
 | |
|   const stringify = build({
 | |
|     title: 'simple object',
 | |
|     type: 'object',
 | |
|     properties: {
 | |
|       product: {
 | |
|         nullable: false,
 | |
|         type: 'object',
 | |
|         properties: {
 | |
|           name: {
 | |
|             type: 'string'
 | |
|           }
 | |
|         }
 | |
|       }
 | |
|     }
 | |
|   })
 | |
|   const object = {
 | |
|     product: null
 | |
|   }
 | |
|   t.throws(() => { stringify(object) })
 | |
| })
 | |
| 
 | |
| test('throw an error on non nullable null object', (t) => {
 | |
|   t.plan(1)
 | |
| 
 | |
|   const stringify = build({
 | |
|     title: 'simple object',
 | |
|     nullable: false,
 | |
|     type: 'object',
 | |
|     properties: {
 | |
|       product: {
 | |
|         nullable: false,
 | |
|         type: 'object',
 | |
|         properties: {
 | |
|           name: {
 | |
|             type: 'string'
 | |
|           }
 | |
|         }
 | |
|       }
 | |
|     }
 | |
|   })
 | |
|   t.throws(() => { stringify(null) })
 | |
| })
 |